Browse Source

first completely working x86 jit and working win32 compile target

mntmn 8 years ago
parent
commit
129d568e4d
10 changed files with 42 additions and 29 deletions
  1. 2 0
      .gitignore
  2. 1 0
      devices/posixfs.c
  3. 10 2
      notes/i386-ops.s
  4. 3 1
      sledge/build_win32.sh
  5. 9 8
      sledge/compiler_new.c
  6. 7 6
      sledge/jit_x86.c
  7. 1 1
      sledge/os/gfx.l
  8. 4 8
      sledge/os/lib.l
  9. 4 2
      sledge/os/shell.l
  10. 1 1
      sledge/writer.c

+ 2 - 0
.gitignore

@@ -16,3 +16,5 @@ build/386iso/boot/interim.386
 build/test
 build/interim.amiga
 sledge.exe
+SDL2.dll
+sledge-win32

+ 1 - 0
devices/posixfs.c

@@ -52,6 +52,7 @@ Cell* posixfs_open(Cell* cpath) {
         
         do {
           if ((dp = readdir(dirp)) != NULL) {
+            printf("dp: |%s|\r\n",dp->d_name);
             _file_cell = alloc_concat(_file_cell,alloc_concat(alloc_string_copy(dp->d_name),nl));
           }
         } while (dp != NULL);

+ 10 - 2
notes/i386-ops.s

@@ -31,8 +31,16 @@ cmovs %edi, %esi
 cmovs %esi, %edx
 cmovs %esi, %esi
 
-cmove %eax, %edi
-cmovne %eax, %esi
+cmove %eax, %eax
+cmove %eax, %ecx
+cmove %eax, %edx
+cmove %ecx, %eax
+cmove %edx, %eax
+cmovne %eax, %eax
+cmovne %eax, %ecx
+cmovne %eax, %edx
+cmovne %ecx, %eax
+cmovne %edx, %eax
 
 mov %esi, %edi
 mov (%esi), %edi

+ 3 - 1
sledge/build_win32.sh

@@ -1,4 +1,6 @@
 #!/bin/sh
 
-i686-w64-mingw32-gcc -m32 -g -o sledge.exe --std=gnu99 -I. ${CFLAGS} sledge.c reader.c writer.c alloc.c strmap.c stream.c ../devices/posixfs.c -lm -DCPU_X86 -DDEV_POSIXFS
+LIBS="-I /home/mntmn/code/sdlcross/SDL2-2.0.3/i686-w64-mingw32/include -L /home/mntmn/code/sdlcross/SDL2-2.0.3/i686-w64-mingw32/lib"
+
+i686-w64-mingw32-gcc -m32 -g -o sledge.exe --std=gnu99 -I. ${CFLAGS} sledge.c reader.c writer.c alloc.c strmap.c stream.c ../devices/posixfs.c ../devices/sdl2.c -lm $LIBS -lSDL2 -DCPU_X86 -DDEV_POSIXFS -DDEV_SDL
 

+ 9 - 8
sledge/compiler_new.c

@@ -673,18 +673,19 @@ int compile_expr(Cell* expr, Frame* frame, int return_type) {
       break;
     }
     case BUILTIN_EQ: {
-      load_int(ARGR0, argdefs[0], frame);
+      load_int(R1, argdefs[0], frame);
       load_int(R2, argdefs[1], frame);
+      jit_movi(R0,0);
       jit_movi(R3,1);
-      jit_subr(ARGR0,R2);
-      jit_movi(R2,0);
-      jit_cmpi(ARGR0,0);
-      jit_moveq(ARGR0,R3);
-      jit_movne(ARGR0,R2);
-      if (return_type == TAG_ANY) jit_call(alloc_int, "alloc_int");
+      jit_cmpr(R1,R2);
+      jit_moveq(R0,R3);
+      if (return_type == TAG_ANY) {
+        jit_movr(ARGR0,R0);
+        jit_call(alloc_int, "alloc_int");
+      }
       else {
         compiled_type = TAG_INT;
-        jit_movr(R0,ARGR0);
+        // int is in R0 already
       }
       break;
     }

+ 7 - 6
sledge/jit_x86.c

@@ -118,25 +118,25 @@ void jit_movi(int reg, int imm) {
 void jit_movr(int dreg, int sreg) {
   if (dreg == sreg) return;
   code[code_idx++] = 0x89;
-  code[code_idx++] = 0xc0 + (regi[sreg]<<3) + regi[dreg];
+  code[code_idx++] = 0xc0 | (regi[sreg]<<3) | regi[dreg];
 }
 
 void jit_movneg(int dreg, int sreg) {
   code[code_idx++] = 0x0f;
   code[code_idx++] = 0x48;
-  code[code_idx++] = 0xc0 + (regi[dreg]<<3) + (regi[sreg]<<4);
+  code[code_idx++] = 0xc0 | (regi[dreg]<<3) | regi[sreg];
 }
 
 void jit_movne(int dreg, int sreg) {
   code[code_idx++] = 0x0f;
   code[code_idx++] = 0x45;
-  code[code_idx++] = 0xc0 + (regi[dreg]<<3) + (regi[sreg]<<4);
+  code[code_idx++] = 0xc0 | (regi[dreg]<<3) | regi[sreg];
 }
 
 void jit_moveq(int dreg, int sreg) {
   code[code_idx++] = 0x0f;
   code[code_idx++] = 0x44;
-  code[code_idx++] = 0xc0 + (regi[dreg]<<3) + (regi[sreg]<<4);
+  code[code_idx++] = 0xc0 | (regi[dreg]<<3) | regi[sreg];
 }
 
 void jit_lea(int reg, void* addr) {
@@ -223,11 +223,12 @@ void jit_subr(int dreg, int sreg) {
 void jit_mulr(int dreg, int sreg) {
   code[code_idx++] = 0x0f;
   code[code_idx++] = 0xaf;
-  code[code_idx++] = 0xc0 | (regi[sreg]<<3) | regi[dreg];
+  code[code_idx++] = 0xc0 | (regi[dreg]<<3) | regi[sreg];
 }
 
 void jit_divr(int dreg, int sreg) {
   jit_movr(R0, dreg);
+  code[code_idx++] = 0x99; // sign-extend rax to edx:rax (cdq)
   code[code_idx++] = 0xf7;
   code[code_idx++] = 0xf8 | regi[sreg]; // idiv goes to %rax
   jit_movr(dreg, R0);
@@ -321,7 +322,7 @@ int inline_mod(int a, int b) {
 void jit_modr(int dreg, int sreg) {
   jit_movr(ARGR0,dreg);
   jit_movr(ARGR1,sreg);
-  jit_call(inline_mod,"mod");
+  jit_call2(inline_mod,"mod");
   if (dreg!=0) jit_movr(dreg,0);
 }
 

+ 1 - 1
sledge/os/gfx.l

@@ -28,7 +28,7 @@
   (let ya (car (cdr a)))
   (let xb (car b))
   (let yb (car (cdr b)))
-  
+
   (let dx (abs (- xb xa)))
   (let dy (abs (- yb ya)))
   (let sx (if (lt xa xb) 1 -1))

+ 4 - 8
sledge/os/lib.l

@@ -25,7 +25,6 @@
 )))
 
 (def split (fn str sepstr (do
-  (print (list "split" str sepstr))
   (let sep (get sepstr 0))
   (let result (quote ()))
   (let sz (size str))
@@ -36,7 +35,10 @@
   (let partsize 0)
   
   (while (gt i -2) (do
-    (if (or (eq (get str i) sep) (eq i -1)) (do
+    (if (lt i 0)
+      (let c 0)
+      (let c (get str i)))
+    (if (or (eq c sep) (eq i -1)) (do
       (let partsize (- (- last-i i) 1))
   
       (if (gt partsize -1)
@@ -51,12 +53,9 @@
 (def copy (fn buf from to num (do
   (let i 0)
   (let c 0)
-
-  (print (list "copy: " buf from to num))
   
   (if (lt from to)
     (do
-      (print "mode1")
       (let i (- num 1)) 
       (while (gt i -1) (do
         (let c (get buf (+ from i)))
@@ -64,7 +63,6 @@
         (let i (- i 1))
       )) 0)
     (do
-      (print "mode2")
       (let i 0)
       (while (lt i num) (do
         (let c (get buf (+ from i)))
@@ -81,9 +79,7 @@
   (let p (+ pos 0))
   (let from (+ pos 1))
   (let num (- (size buf) pos))
-  (print (list "remove" buf pos p from (- num 1)))
   (copy buf from p num)
-  (print "copied")
   (put buf (- (size buf) 1) 0)
   buf
 )))

+ 4 - 2
sledge/os/shell.l

@@ -3,6 +3,8 @@
   (split (load "/sd/") [0a])
 )))
 
+(print (list "screen" screen-width screen-height screen-bpp))
+
 (def draw-logo (fn ox oy (do
   (def stroke-color 0xff8e)
   (line (pt (+ ox 16) (- oy 38)) (pt (+ ox 16) (- oy 102)))
@@ -20,6 +22,8 @@
 (draw-logo (- (/ screen-width 2) 140) (+ 1 (/ screen-height 2)))
 (send scr 0)
 
+(def keyboard (open "/keyboard"))
+
 (blit-str "Welcome to Interim OS." 32 32)
 (send scr 0)
 
@@ -29,8 +33,6 @@
   (blit-str evbuf x y)
 )))
 
-(def keyboard (open "/keyboard"))
-
 (def term-x minx)
 (def term-y (+ miny 32))
 

+ 1 - 1
sledge/writer.c

@@ -2,7 +2,7 @@
 #include "stream.h"
 #include <stdio.h>
 
-#define TMP_BUF_SIZE 256
+#define TMP_BUF_SIZE 512
 #define INTFORMAT "%ld"
 
 char* tag_to_str(int tag) {