commit 95ae86970da0e8d79149f0bdd44fb093be1bdff9
parent be6ca9234b5e6fdd7118f16d45176f9953bcf5a5
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 11 Jul 2022 11:24:55 -0400
cc: add & ^ | binops
Diffstat:
10 files changed, 30 insertions(+), 10 deletions(-)
diff --git a/fs/asm.fs b/fs/asm.fs
@@ -108,7 +108,7 @@ $e9 4 op jmp, $e8 2 op call,
dup 1+ c@ ( immreg ) swap 2 + c@ ( immreg immop ) modrm<imm, else
c@ src swap ( reg regop ) modrm2, then ;
$81 0 $01 op add, $81 7 $39 op cmp, $81 5 $29 op sub, $f7 0 $85 op test,
-$81 4 $21 op and, $81 1 $09 op or,
+$81 4 $21 op and, $81 1 $09 op or, $81 6 $33 op xor,
\ tgt or-ed in
: op ( op -- ) doer c, does> ( a -- ) c@ tgtid or c, asm$ ;
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -23,13 +23,13 @@ POPSCNT stringlist POPTlist "++" "--"
: poptoken ( opid -- tok ) POPTlist slistiter ;
\ Binary operators
-13 const BOPSCNT
+16 const BOPSCNT
BOPSCNT stringlist BOPTlist
-"+" "-" "*" "/" "<" ">" "<=" ">=" "==" "!=" "&&" "||" "="
+"+" "-" "*" "/" "<" ">" "<=" ">=" "==" "!=" "&" "^" "|" "&&" "||" "="
\ binary ops precedence. lower means more precedence
create bopsprectbl 1 c, 1 c, 0 c, 0 c, 2 c, 2 c, 2 c, 2 c,
- 3 c, 3 c, 4 c, 4 c, 5 c,
+ 3 c, 3 c, 4 c, 4 c, 4 c, 5 c, 5 c, 6 c,
: bopid ( tok -- opid? f )
BOPTlist sfind dup 0< if drop 0 else 1 then ;
diff --git a/fs/cc/cc.fs b/fs/cc/cc.fs
@@ -10,4 +10,4 @@ require sys/xhere.fs
xhere$ xhere[ parseast curunit _debug if dup printast nl> then ]xhere
gennode ;
-: cc1<< ( -- ) word fopen dup [f<] to stdin cc1, fclose ;
+: cc<< ( -- ) word fopen dup [f<] to stdin cc1, fclose ;
diff --git a/fs/cc/gen.fs b/fs/cc/gen.fs
@@ -117,6 +117,9 @@ BOPSCNT wordtbl bopgentblpre ( node -- node )
'w noop ( >= )
'w noop ( == )
'w noop ( != )
+'w noop ( & )
+'w noop ( ^ )
+'w noop ( | )
'w noop ( && )
'w noop ( || )
'w noop ( = )
@@ -132,6 +135,9 @@ BOPSCNT wordtbl bopgentblpost ( -- )
:w ( >= ) abort" TODO" ;
'w vm==, ( == )
:w ( != ) abort" TODO" ;
+:w ( & ) vm&, ;
+:w ( ^ ) vm^, ;
+:w ( | ) vm|, ;
'w vm&&, ( && )
'w vm||, ( || )
:w ( = ) vmmov, ;
diff --git a/fs/cc/tok.fs b/fs/cc/tok.fs
@@ -24,9 +24,9 @@
\ with a symbol that is also a 1 char symbol and all 3 chars symbols begin with
\ 2 chars that are also a 2 chars symbol.
\ list of 1 char symbols
-create symbols1 ," +-*/~&<>=[](){}.%^?:;," '"' c,
+create symbols1 ," +-*/~&<>=[](){}.%^?:;,|^" '"' c,
-: isSym1? ( c -- f ) symbols1 23 [c]? 0>= ;
+: isSym1? ( c -- f ) symbols1 25 [c]? 0>= ;
\ list of 2 chars symbols
create symbols2 ," <=>===!=&&||++---><<>>+=-=*=/=%=&=^=|=/**///"
diff --git a/fs/cc/vm.fs b/fs/cc/vm.fs
@@ -222,6 +222,9 @@ operands value 'curop
selop2 hasop# opAsm ;
: vmadd, binopprep add, opdeinit ;
: vmsub, binopprep sub, opdeinit ;
+: vm&, binopprep and, opdeinit ;
+: vm|, binopprep or, opdeinit ;
+: vm^, binopprep xor, opdeinit ;
\ mul is special and cannot use binopprep for two reasons: its target operand
\ is hardcoded to EAX and also, EDX gets written by the op, so we need to save
\ EDX if in use.
diff --git a/fs/lib/crc.c b/fs/lib/crc.c
@@ -1,6 +1,5 @@
/* TODO: make the C compiler compile this. So far, what is missing is:
* for loops
- * unsigned integers
* xor
* and
* shift-assign
diff --git a/fs/lib/crc.fs b/fs/lib/crc.fs
@@ -1,7 +1,7 @@
\ CRC implementations
?f<< cc/cc.fs
-cc1<< lib/crc.c
+cc<< lib/crc.c
\ Computes CRC32 over range "a u".
: crc32[] ( a u -- crc ) A>r >r >A $ffffffff begin ( n ) Ac@+ crc32 next r>A ;
diff --git a/fs/tests/cc/cc.fs b/fs/tests/cc/cc.fs
@@ -2,11 +2,14 @@
?f<< cc/cc.fs
testbegin
\ Tests for the C compiler
-cc1<< tests/cc/test.c
+cc<< tests/cc/test.c
retconst 42 #eq
neg -42 #eq
bwnot $ffffffd5 #eq
exprbinops 7 #eq
+binopand 42 #eq
+binopor 42 #eq
+binopxor 42 #eq
boolops 0 #eq
variables 82 #eq
funcall 42 #eq
diff --git a/fs/tests/cc/test.c b/fs/tests/cc/test.c
@@ -12,6 +12,15 @@ int bwnot() {
int exprbinops() {
return 1 + 2 * 3;
}
+int binopand() {
+ return $ff & 42;
+}
+int binopor() {
+ return 40 | 2;
+}
+int binopxor() {
+ return 43 ^ 1;
+}
int boolops() {
return 66 < 54 && 2 == 2;
}