So I'm writing a little interpreter in C at the moment for a language I have created (which is pretty similar to Python). I have written the lexer and parser and currently my program outputs an AST, an now I am attempting to turn this AST into bytecode. Currently my algorithm traverses the AST (depth-first) and can generate bytecode for simple arithmetic, and now I am trying to implement if statements.
I cannot copy all my code in here because it's a pretty large amount of code, but currently the program takes an AST which might look something like
ADD
|-- 1
|-- MUL
|-- 2
|-- 3
and turns this into
LOAD 1 //the real code doesn't put the value here, but a number representing the position of this value in an array
LOAD 2
LOAD 3
MUL
ADD
This is easy for simple expressions but I really don't know how to generate the bytecode for an if statement. I know that I will have to jump to the else clause if the comparison is false, and also jump from the end of every if/else if block, but how do I deal with this if the jump is more than 256 bytes of bytecode?
答案 0 :(得分:1)
您应该阅读SICP,Dragon Book,然后Lisp In Small Pieces。
我希望你能重新设计你的字节码。然后你可以有一些FARJUMP
字节码,然后是四个字节a
,b
,c
,d
(考虑作为uint8_t
无符号整数,每个8位),你将跳转到(a<<24) + (b<<16) + (c<<8) +d
偏移量。
你可能希望能够前后跳跃。要么BACKFARJUMP
要向后跳,要么使用一些签名的偏移...
使用这样的操作码,您将能够跳转到超过40亿个字节码(2 32 )。这可能更容易。
如果40亿字节码偏移量不够,可以概括。
不要忘记您的计算机不可能有超过1TB的RAM(并且这样的计算机比汽车的成本更高)。