commit e1c1fdc502fb2108bde525fcf34d1879f595fa74
parent 0be7c577f8c2a971bf0a2aeab011f5a044bd6291
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 17 Oct 2022 08:00:59 -0400
cc: fix pointer arithmetics bug
It wouldn't be applied with -> and .
I'm not super happy with the code involved, but I don't see better options. I
hope I can simplify this later.
Diffstat:
3 files changed, 23 insertions(+), 6 deletions(-)
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -30,6 +30,8 @@ create symbols 0 , 0 c, \ this is a dict link
\ Unary operators
7 const UOPSCNT
UOPSCNT stringlist UOPTlist "-" "~" "!" "&" "*" "++" "--"
+3 const UOP&
+4 const UOP*
: uopid ( tok -- opid? f )
UOPTlist sfind dup 0< if drop 0 else 1 then ;
@@ -179,6 +181,12 @@ extends ASTNode struct[ Op
: :new ( opid id -- node ) ASTNode :new swap _pad :, ;
]struct
+extends ASTNode struct[ Arrow
+ SZ &+ name
+
+ : :new AST_ARROW ASTNode :new swap _pad :s, ;
+]struct
+
struct+[ ASTNode
\ Multiply the value of "node" by a factor of "n"
: :*=n ( n self -- ) >r ( n )
@@ -189,6 +197,16 @@ struct+[ ASTNode
: :type dup id case ( self )
AST_IDENT of =
Ident :finddecl ?dup _assert Declare ctype CType :type endof
+ AST_UNARYOP of =
+ dup firstchild :type swap Op opid case ( type )
+ UOP& of = type*lvl+ endof
+ UOP* of = dup type*lvl if type*lvl- then endof
+ endcase endof
+ AST_ARROW of =
+ dup Arrow name swap firstchild :type ( name type )
+ dup type*lvl 1 = _assert ( name type )
+ ctype' dup CType :struct? _assert ( name ctype )
+ CType :find ( field-ctype ) CType :type endof
drop TYPE_INT endcase ;
\ Return the "pointer arithmetic size" of "node".
@@ -227,12 +245,6 @@ extends ASTNode struct[ StrLit
: :new AST_STRLIT ASTNode :new ;
]struct
-extends ASTNode struct[ Arrow
- SZ &+ name
-
- : :new AST_ARROW ASTNode :new swap _pad :s, ;
-]struct
-
\ Macro shortcuts
: c]# Constant :new ]# ;
: i]# Ident :new ]# ;
diff --git a/fs/tests/cc/cc.fs b/fs/tests/cc/cc.fs
@@ -75,6 +75,7 @@ binop7 0 #eq
structop1 44 #eq
structop2 45 #eq
structop3 42 #eq
+structop4 globdata 12 + #eq
opwidth1 42 #eq
opwidth2 42 #eq
opwidth3 $129 #eq
diff --git a/fs/tests/cc/test.c b/fs/tests/cc/test.c
@@ -233,6 +233,7 @@ struct MyStruct {
int foo;
short bar;
MyType baz[2];
+ int array[2];
};
MyType structget(MyStruct *s) {
@@ -318,6 +319,9 @@ unsigned char structop3() {
globdata.bar = 1;
return globdata.baz[globdata.bar];
}
+int structop4() {
+ return &globdata.array[1];
+}
// we used to leak VM ops in condition blocks without {}
void cond1() {
int x = 42;