@@ -65,6 +65,7 @@ class Intrinsic(Enum):
6565 FORTH_LOAD64 = auto ()
6666 FORTH_STORE64 = auto ()
6767 CAST_PTR = auto ()
68+ CAST_INT = auto ()
6869 ARGC = auto ()
6970 ARGV = auto ()
7071 HERE = auto ()
@@ -201,7 +202,7 @@ def simulate_little_endian_linux(program: Program, argv: List[str]):
201202 else :
202203 ip += 1
203204 elif op .typ == OpType .INTRINSIC :
204- assert len (Intrinsic ) == 41 , "Exhaustive handling of intrinsic in simulate_little_endian_linux()"
205+ assert len (Intrinsic ) == 42 , "Exhaustive handling of intrinsic in simulate_little_endian_linux()"
205206 if op .operand == Intrinsic .PLUS :
206207 a = stack .pop ()
207208 b = stack .pop ()
@@ -385,6 +386,9 @@ def simulate_little_endian_linux(program: Program, argv: List[str]):
385386 elif op .operand == Intrinsic .CAST_PTR :
386387 # Ignore the type casting. It's only useful for type_check_program() phase
387388 ip += 1
389+ elif op .operand == Intrinsic .CAST_INT :
390+ # Ignore the type casting. It's only useful for type_check_program() phase
391+ ip += 1
388392 elif op .operand == Intrinsic .SYSCALL0 :
389393 syscall_number = stack .pop ();
390394 if syscall_number == 39 : # SYS_getpid
@@ -538,7 +542,7 @@ def type_check_program(program: Program):
538542 stack .append ((DataType .INT , op .token ))
539543 stack .append ((DataType .PTR , op .token ))
540544 elif op .typ == OpType .INTRINSIC :
541- assert len (Intrinsic ) == 41 , "Exhaustive intrinsic handling in type_check_program()"
545+ assert len (Intrinsic ) == 42 , "Exhaustive intrinsic handling in type_check_program()"
542546 assert isinstance (op .operand , Intrinsic ), "This could be a bug in compilation step"
543547 if op .operand == Intrinsic .PLUS :
544548 assert len (DataType ) == 3 , "Exhaustive type handling in PLUS intrinsic"
@@ -914,6 +918,14 @@ def type_check_program(program: Program):
914918 a_type , a_token = stack .pop ()
915919
916920 stack .append ((DataType .PTR , a_token ))
921+ elif op .operand == Intrinsic .CAST_INT :
922+ if len (stack ) < 1 :
923+ not_enough_arguments (op )
924+ exit (1 )
925+
926+ a_type , a_token = stack .pop ()
927+
928+ stack .append ((DataType .INT , a_token ))
917929 elif op .operand == Intrinsic .ARGC :
918930 stack .append ((DataType .INT , op .token ))
919931 elif op .operand == Intrinsic .ARGV :
@@ -1122,7 +1134,7 @@ def generate_nasm_linux_x86_64(program: Program, out_file_path: str):
11221134 assert isinstance (op .operand , int ), "This could be a bug in the compilation step"
11231135 out .write (" jz addr_%d\n " % op .operand )
11241136 elif op .typ == OpType .INTRINSIC :
1125- assert len (Intrinsic ) == 41 , "Exhaustive intrinsic handling in generate_nasm_linux_x86_64()"
1137+ assert len (Intrinsic ) == 42 , "Exhaustive intrinsic handling in generate_nasm_linux_x86_64()"
11261138 if op .operand == Intrinsic .PLUS :
11271139 out .write (" ;; -- plus --\n " )
11281140 out .write (" pop rax\n " )
@@ -1332,6 +1344,8 @@ def generate_nasm_linux_x86_64(program: Program, out_file_path: str):
13321344 out .write (" mov [rax], rbx\n " );
13331345 elif op .operand == Intrinsic .CAST_PTR :
13341346 out .write (" ;; -- cast(ptr) --\n " )
1347+ elif op .operand == Intrinsic .CAST_INT :
1348+ out .write (" ;; -- cast(int) --\n " )
13351349 elif op .operand == Intrinsic .SYSCALL0 :
13361350 out .write (" ;; -- syscall0 --\n " )
13371351 out .write (" pop rax\n " )
@@ -1415,7 +1429,7 @@ def generate_nasm_linux_x86_64(program: Program, out_file_path: str):
14151429 'include' : Keyword .INCLUDE ,
14161430}
14171431
1418- assert len (Intrinsic ) == 41 , "Exhaustive INTRINSIC_BY_NAMES definition"
1432+ assert len (Intrinsic ) == 42 , "Exhaustive INTRINSIC_BY_NAMES definition"
14191433INTRINSIC_BY_NAMES = {
14201434 '+' : Intrinsic .PLUS ,
14211435 '-' : Intrinsic .MINUS ,
@@ -1448,6 +1462,7 @@ def generate_nasm_linux_x86_64(program: Program, out_file_path: str):
14481462 '!64' : Intrinsic .FORTH_STORE64 ,
14491463 '@64' : Intrinsic .FORTH_LOAD64 ,
14501464 'cast(ptr)' : Intrinsic .CAST_PTR ,
1465+ 'cast(int)' : Intrinsic .CAST_INT ,
14511466 'argc' : Intrinsic .ARGC ,
14521467 'argv' : Intrinsic .ARGV ,
14531468 'here' : Intrinsic .HERE ,
0 commit comments