duskos

dusk os fork
git clone git://git.alexwennerberg.com/duskos
Log | Files | Refs | README | LICENSE

commit bc5c151262fcbb75a95019eef5c2093cd9a821ca
parent 3fbb95310a2d46eb0f623cd411ba299fcc2da313
Author: Virgil Dupras <hsoft@hardcoded.net>
Date:   Mon,  5 Dec 2022 21:06:49 -0500

comp/c: implement typecasting

Maybe it doesn't cover all possible legitimate uses of typecasting, but it
covers my immediate one in uxntal.

Diffstat:
Mfs/comp/c/pgen.fs | 2+-
Mfs/comp/c/vm/commonhi.fs | 8++++++++
Mfs/comp/c/vm/forth.fs | 1+
Mfs/comp/c/vm/i386.fs | 1+
Mfs/tests/comp/c/cc.fs | 1+
Mfs/tests/comp/c/test.c | 5+++++
6 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/fs/comp/c/pgen.fs b/fs/comp/c/pgen.fs @@ -275,7 +275,7 @@ MAXLITSZ Stack :new structbind Stack _list \ can be an expression or a typecast nextt dup parseType if ( tok type ) \ TODO: actually process the typecast - nip parseDeclarator drop read) nextt parseFactor + nip parseDeclarator read) nextt parseFactor vmop :typecast else ( tok ) parseExpression read) parsePostfixOp then endof '"' of isChar?^ MAXLITSZ _litarena :[ diff --git a/fs/comp/c/vm/commonhi.fs b/fs/comp/c/vm/commonhi.fs @@ -13,6 +13,14 @@ struct+[ VMOp dup :isconst? if tuck arg swap / swap to arg else VMOp :/n then else 2drop then ; : :+n over if dup :isconst? if to+ arg else VMOp :+n then else 2drop then ; + : :&n dup :isconst? if tuck arg and swap to arg else VMOp :&n then ; + + : :typecast ( type self -- ) + 2dup to type swap typesize case ( self ) + 1 of = $ff swap :&n endof + 2 of = $ffff swap :&n endof + drop + endcase ; ]struct UNOPCNT wordtbl constops ( n -- n ) diff --git a/fs/comp/c/vm/forth.fs b/fs/comp/c/vm/forth.fs @@ -41,6 +41,7 @@ struct+[ VMOp : :*n ( n self -- ) tuck :compile litn compile * :>reg ; : :/n ( n self -- ) tuck :compile litn compile / :>reg ; : :+n ( n self -- ) tuck :compile litn compile + :>reg ; + : :&n ( n self -- ) tuck :compile litn compile and :>reg ; : :>res dup :compile :>reg ; ]struct diff --git a/fs/comp/c/vm/i386.fs b/fs/comp/c/vm/i386.fs @@ -111,6 +111,7 @@ struct+[ VMOp : :>res dup :>reg :>simple ; : :+n ( n self -- ) dup :>res :compile i) add, ; + : :&n ( n self -- ) dup :>res :compile i) and, ; ]struct diff --git a/fs/tests/comp/c/cc.fs b/fs/tests/comp/c/cc.fs @@ -61,6 +61,7 @@ S" foobar" 2 get8b 'o' #eq S" foobar" 4 get8b 'b' #eq S" foobar" dup 2 'X' set8b S" fXobar" #s= S" foobar" dup 6 'X' set8b S" foobaX" #s= +typecast 1 #eq 5 whilesum 15 #eq 5 dowhilesum 15 #eq create mydata 42 , $12345678 , $23456789 , diff --git a/fs/tests/comp/c/test.c b/fs/tests/comp/c/test.c @@ -229,6 +229,11 @@ char get8b(char *s, int i) { void set8b(char *s, int i, char c) { s[i] = c; } +int typecast() { + char x = $ff; + int y = 1; + return x == (char)(y-2); +} void whilesum(int n) { int res = 0; while (n) {