From 1619c4995dddb9e9a440b04f69e7e6c6ff368ff0 Mon Sep 17 00:00:00 2001 From: Peter Ferrie Date: Mon, 29 May 2023 18:06:11 -0700 Subject: [PATCH 01/10] shave some bytes --- sectorc.s | 76 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/sectorc.s b/sectorc.s index b803d3f..8167a2c 100644 --- a/sectorc.s +++ b/sectorc.s @@ -98,26 +98,34 @@ execute: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; compile statements (optionally advancing tokens beforehand) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +_is_call: + mov al,0xe8 ; emit "call" instruction + stosb + + mov ax,[bx] ; load function offset from symbol-table + sub ax,di ; compute relative to this location: "dest - cur - 2" + dec ax + dec ax + stosw ; emit target + ;; [fall-through] ; loop to compile next statement + compile_stmts_tok_next2: call tok_next compile_stmts_tok_next: call tok_next + db 0x81 ; mask following call + ;; [fall-through] +_not_while: + call compile_assign ; handle an assignment statement + ;; [fall-through] ; loop to compile next statement + compile_stmts: - mov ax,bx + xchg bx,ax cmp ax,TOK_BLK_END ; if we reach '}' then return je return test dh,dh ; if dh is 0, it's not a call - je _not_call - mov al,0xe8 ; emit "call" instruction - stosb - - mov ax,[bx] ; load function offset from symbol-table - sub ax,di ; compute relative to this location: "dest - cur - 2" - sub ax,2 - stosw ; emit target - - jmp compile_stmts_tok_next2 ; loop to compile next statement + jne _is_call _not_call: cmp ax,TOK_ASM ; check for "asm" @@ -137,18 +145,15 @@ _not_if: jne _not_while push di ; save loop start location call _control_flow_block ; compile control-flow block - jmp _patch_back ; patch up backward and forward jumps of while-stmt - -_not_while: - call compile_assign ; handle an assignment statement - jmp compile_stmts ; loop to compile next statement + ;; [fall-through] ; patch up backward and forward jumps of while-stmt _patch_back: mov al,0xe9 ; emit "jmp" instruction (backwards) stosb pop ax ; restore loop start location sub ax,di ; compute relative to this location: "dest - cur - 2" - sub ax,2 + dec ax + dec ax stosw ; emit target ;; [fall-through] _patch_fwd: @@ -180,7 +185,7 @@ return: ; this label gives us a way to do conditional re compile_assign: cmp ax,TOK_DEREF ; check for "*(int*)" jne _not_deref_store - call tok_next ; consome "*(int*)" + call tok_next ; consume "*(int*)" call save_var_and_compile_expr ; compile rhs first ;; [fall-through] @@ -207,7 +212,7 @@ compile_store: jmp emit_var ; [tail-call] save_var_and_compile_expr: - mov bp,bx ; save dest to bp + xchg bp,ax ; save dest to bp call tok_next ; consume dest ;; [fall-through] ; fall-through will consume "=" before compiling expr @@ -219,22 +224,18 @@ compile_expr_tok_next: compile_expr: call compile_unary ; compile left-hand side - push ds ; need to swap out 'ds' to scan the table with lodsw - push cs - pop ds - + push ds + push cs ; cannot use cs override! + pop ds ; because ';' here must be retained separately mov si,binary_oper_tbl - 2 ; load ptr to operator table (biased backwards) _check_next: lodsw ; discard 16-bit of machine-code lodsw ; load 16-bit token value - cmp ax,bx ; matches token? - je _found test ax,ax ; end of table? + je _not_found + cmp ax,bx ; matches token? jne _check_next - pop ds - ret ; all-done, not found - _found: lodsw ; load 16-bit of machine-code push ax ; save it to the stack @@ -258,8 +259,10 @@ emit_cmp_op: ;; [fall-through] emit_op: - mov ax,bx + xchg bx,ax stosw ; emit machine code for op + +_not_found: pop ds ret @@ -305,10 +308,10 @@ emit_var: ;; [fall-through] emit_tok: - mov ax,bx + xchg bx,ax stosw ; emit token value - jmp tok_next ; [tail-call] - + db 0x81 ; mask following call [tail-call] + ;; [fall-through] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; get next token, setting the following: ;;; ax: token @@ -334,7 +337,7 @@ _nextch: cmp al,32 jle _done ; if char is space then break - shl cx,8 + mov ch,cl mov cl,al ; shift this char into cx imul bx,10 @@ -345,7 +348,7 @@ _nextch: jmp _nextch ; [loop] _done: - mov ax,cx + xchg cx,ax cmp ax,0x2f2f ; check for single-line comment "//" je _comment_double_slash cmp ax,0x2f2a ; check for multi-line comment "/*" @@ -381,7 +384,7 @@ getch: getch_tryagain: mov ax,0x0200 - xor dx,dx + cwd int 0x14 ; get a char from serial (bios function) and ah,0x80 ; check for failure and clear ah as a side-effect @@ -389,8 +392,7 @@ getch_tryagain: cmp al,59 ; check for ';' jne getch_done ; if not ';' return it - mov [si],ax ; save the ';' - xor ax,ax ; return 0 instead, treated as whitespcae + xchg [si],ax ; save the ';', return 0 instead, treated as whitespcae getch_done: pop dx From dc4ae49aa7de0a8d26987a0477afcdbf13799e40 Mon Sep 17 00:00:00 2001 From: Peter Ferrie Date: Mon, 29 May 2023 18:16:57 -0700 Subject: [PATCH 02/10] one more --- sectorc.s | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sectorc.s b/sectorc.s index 8167a2c..4d0c5f7 100644 --- a/sectorc.s +++ b/sectorc.s @@ -212,7 +212,7 @@ compile_store: jmp emit_var ; [tail-call] save_var_and_compile_expr: - xchg bp,ax ; save dest to bp + xchg bp,ax ; save dest to bp call tok_next ; consume dest ;; [fall-through] ; fall-through will consume "=" before compiling expr @@ -227,17 +227,16 @@ compile_expr: push ds push cs ; cannot use cs override! pop ds ; because ';' here must be retained separately - mov si,binary_oper_tbl - 2 ; load ptr to operator table (biased backwards) + mov si,binary_oper_tbl ; load ptr to operator table (biased backwards) _check_next: - lodsw ; discard 16-bit of machine-code lodsw ; load 16-bit token value test ax,ax ; end of table? je _not_found cmp ax,bx ; matches token? + lodsw ; load 16-bit of machine-code jne _check_next _found: - lodsw ; load 16-bit of machine-code push ax ; save it to the stack mov al,0x50 ; code for "push ax" stosb ; emit From 08a21e0a4cd92c93a47366ab49aee515ccf1660d Mon Sep 17 00:00:00 2001 From: Peter Ferrie Date: Mon, 29 May 2023 18:31:57 -0700 Subject: [PATCH 03/10] one more --- sectorc.s | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sectorc.s b/sectorc.s index 4d0c5f7..a1d6d66 100644 --- a/sectorc.s +++ b/sectorc.s @@ -62,6 +62,12 @@ entry: xor di,di ; codegen index, zero'd ;; [fall-through] + db 0x81 ; mask following call + ;; [fall-through] +_is_int: + call tok_next2 ; consume "int" and + ;; [fall-through] + ;; main loop for parsing all decls compile: ;; advance to either "int" or "void" @@ -69,9 +75,7 @@ compile: ;; if "int" then skip a variable cmp ax,TOK_INT - jne compile_function - call tok_next2 ; consume "int" and - jmp compile + je _is_int compile_function: ; parse and compile a function decl call tok_next ; consume "void" From a6e3c9de8751fe8e09a7579fcdfdd804c8b47061 Mon Sep 17 00:00:00 2001 From: Peter Ferrie Date: Mon, 29 May 2023 20:15:02 -0700 Subject: [PATCH 04/10] one more --- sectorc.s | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sectorc.s b/sectorc.s index a1d6d66..81a602a 100644 --- a/sectorc.s +++ b/sectorc.s @@ -295,14 +295,15 @@ _not_paren: _not_addr: test dl,dl ; check for tok_is_num + mov ax,0x068b ; code for "mov ax,[imm]" je _not_int mov al,0xb8 ; code for "mov ax,imm" stosb ; emit - jmp emit_tok ; [tail-call] to emit imm - + db 0xf6 ; mask following stosw and add bx,bx + ; [tail-call] to emit imm + ;; [fall-through] _not_int: ;; compile var - mov ax,0x068b ; code for "mov ax,[imm]" ;; [fall-through] emit_var: From f5a520fc49f8667f9594207fda5dd657e35fad6f Mon Sep 17 00:00:00 2001 From: Peter Ferrie Date: Tue, 30 May 2023 11:43:16 -0700 Subject: [PATCH 05/10] restore call target --- sectorc.s | 1 + 1 file changed, 1 insertion(+) diff --git a/sectorc.s b/sectorc.s index 81a602a..26137e9 100644 --- a/sectorc.s +++ b/sectorc.s @@ -103,6 +103,7 @@ execute: ;;; compile statements (optionally advancing tokens beforehand) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; _is_call: + xchg bx,ax mov al,0xe8 ; emit "call" instruction stosb From acf1d6612bebdc2ec19d73dcd98f1c0d6c83fe1d Mon Sep 17 00:00:00 2001 From: Peter Ferrie Date: Tue, 30 May 2023 19:40:24 -0700 Subject: [PATCH 06/10] a few more --- sectorc.s | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/sectorc.s b/sectorc.s index 26137e9..e345b67 100644 --- a/sectorc.s +++ b/sectorc.s @@ -195,8 +195,6 @@ compile_assign: ;; [fall-through] compile_store_deref: - mov bx,bp ; restore dest var token - mov ax,0x0489 ; code for "mov [si],ax" ;; [fall-through] emit_common_ptr_op: @@ -212,8 +210,7 @@ _not_deref_store: ;; [fall-through] compile_store: - mov bx,bp ; restore dest var token - mov ax,0x0689 ; code for "mov [imm],ax" + mov ah,0x06 ; code for "mov [imm],ax" jmp emit_var ; [tail-call] save_var_and_compile_expr: @@ -232,14 +229,14 @@ compile_expr: push ds push cs ; cannot use cs override! pop ds ; because ';' here must be retained separately + mov cx,(binary_oper_tbl_e-binary_oper_tbl)/4 mov si,binary_oper_tbl ; load ptr to operator table (biased backwards) _check_next: lodsw ; load 16-bit token value - test ax,ax ; end of table? - je _not_found cmp ax,bx ; matches token? lodsw ; load 16-bit of machine-code - jne _check_next + loopne _check_next ; lodsw does not affect flags + jne _not_found _found: push ax ; save it to the stack @@ -268,6 +265,8 @@ emit_op: _not_found: pop ds + mov bx,bp ; restore dest var token + mov ax,0x0489 ; code for "mov [si],ax" ret ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -297,7 +296,7 @@ _not_paren: _not_addr: test dl,dl ; check for tok_is_num mov ax,0x068b ; code for "mov ax,[imm]" - je _not_int + je _not_int ; mov does not affect flags mov al,0xb8 ; code for "mov ax,imm" stosb ; emit db 0xf6 ; mask following stosw and add bx,bx @@ -421,7 +420,6 @@ binary_oper_tbl: dw TOK_GT,0xc09f ; setg al dw TOK_LE,0xc09e ; setle al dw TOK_GE,0xc09d ; setge al - dw 0 ; [sentinel] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; boot signature From cc163e50ea1aa8567b6c74405364647a40ab981b Mon Sep 17 00:00:00 2001 From: Peter Ferrie Date: Tue, 30 May 2023 19:55:36 -0700 Subject: [PATCH 07/10] add missed label --- sectorc.s | 1 + 1 file changed, 1 insertion(+) diff --git a/sectorc.s b/sectorc.s index e345b67..07c8e95 100644 --- a/sectorc.s +++ b/sectorc.s @@ -420,6 +420,7 @@ binary_oper_tbl: dw TOK_GT,0xc09f ; setg al dw TOK_LE,0xc09e ; setle al dw TOK_GE,0xc09d ; setge al +binary_oper_tbl_e: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; boot signature From 92effcbb0725bcc63811d9565d51ddec1c3f7b6d Mon Sep 17 00:00:00 2001 From: Peter Ferrie Date: Tue, 30 May 2023 20:11:22 -0700 Subject: [PATCH 08/10] one more --- sectorc.s | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sectorc.s b/sectorc.s index 07c8e95..f23ace3 100644 --- a/sectorc.s +++ b/sectorc.s @@ -141,9 +141,7 @@ _not_call: _not_asm: cmp ax,TOK_IF_BEGIN ; check for "if" - jne _not_if - call _control_flow_block ; compile control-flow block - jmp _patch_fwd ; patch up forward jump of if-stmt + je _is_if _not_if: cmp ax,TOK_WHILE_BEGIN ; check for "while" @@ -161,6 +159,11 @@ _patch_back: dec ax stosw ; emit target ;; [fall-through] + db 0x81 ; mask following call +_is_if: + call _control_flow_block ; compile control-flow block + ;; [fall-through] ; patch up forward jump of if-stmt + _patch_fwd: mov ax,di ; compute relative fwd jump to this location: "dest - src" sub ax,si From 74e7df7317d4429eddb3233f47f10949c37245a5 Mon Sep 17 00:00:00 2001 From: Peter Ferrie Date: Tue, 30 May 2023 20:17:52 -0700 Subject: [PATCH 09/10] one more --- sectorc.s | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sectorc.s b/sectorc.s index f23ace3..c0b4ab2 100644 --- a/sectorc.s +++ b/sectorc.s @@ -232,7 +232,7 @@ compile_expr: push ds push cs ; cannot use cs override! pop ds ; because ';' here must be retained separately - mov cx,(binary_oper_tbl_e-binary_oper_tbl)/4 + mov cl,(binary_oper_tbl_e-binary_oper_tbl)/4 mov si,binary_oper_tbl ; load ptr to operator table (biased backwards) _check_next: lodsw ; load 16-bit token value From 9217f699f9474b26b40011dce4cc5130a804a388 Mon Sep 17 00:00:00 2001 From: Peter Ferrie Date: Tue, 30 May 2023 20:25:01 -0700 Subject: [PATCH 10/10] and one more --- sectorc.s | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sectorc.s b/sectorc.s index c0b4ab2..2db97c1 100644 --- a/sectorc.s +++ b/sectorc.s @@ -341,9 +341,6 @@ tok_next: setle dl ; tok_is_num = (al <= '9') _nextch: - cmp al,32 - jle _done ; if char is space then break - mov ch,cl mov cl,al ; shift this char into cx @@ -352,7 +349,8 @@ _nextch: add bx,ax ; atoi computation: bx = 10 * bx + (ax - '0') call getch - jmp _nextch ; [loop] + cmp al,32 + jg _nextch ; loop if char is not space _done: xchg cx,ax