Skip to content

Commit

Permalink
Code gen done
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielzezze committed Jun 3, 2021
1 parent 849aee5 commit 639f903
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 43 deletions.
49 changes: 30 additions & 19 deletions debug/test_tokens.out
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,33 @@ Token(type='PARENTHESIS_CLOSE', value=')', lineno=28, index=449)
Token(type='EOL', value='.z', lineno=28, index=451)
Token(type='BLOCK_END', value='}', lineno=29, index=458)
Token(type='EOL', value='.z', lineno=29, index=460)
Token(type='PRINT', value='zPrint', lineno=31, index=468)
Token(type='PARENTHESIS_OPEN', value='(', lineno=31, index=474)
Token(type='IDENTIFIER', value='x', lineno=31, index=475)
Token(type='PARENTHESIS_CLOSE', value=')', lineno=31, index=476)
Token(type='EOL', value='.z', lineno=31, index=478)
Token(type='PRINT', value='zPrint', lineno=33, index=486)
Token(type='PARENTHESIS_OPEN', value='(', lineno=33, index=492)
Token(type='IDENTIFIER', value='sub', lineno=33, index=493)
Token(type='PARENTHESIS_OPEN', value='(', lineno=33, index=496)
Token(type='IDENTIFIER', value='x', lineno=33, index=497)
Token(type='IDENTIFIER', value='x', lineno=33, index=500)
Token(type='PARENTHESIS_CLOSE', value=')', lineno=33, index=501)
Token(type='PARENTHESIS_CLOSE', value=')', lineno=33, index=502)
Token(type='EOL', value='.z', lineno=33, index=504)
Token(type='RETURN', value='return', lineno=35, index=512)
Token(type='NUMBER', value='0', lineno=35, index=519)
Token(type='EOL', value='.z', lineno=35, index=521)
Token(type='BLOCK_END', value='}', lineno=36, index=524)
Token(type='EOL', value='.z', lineno=36, index=526)
Token(type='VAR', value='var', lineno=31, index=468)
Token(type='IDENTIFIER', value='y', lineno=31, index=472)
Token(type='ARROW', value='->', lineno=31, index=474)
Token(type='STRING_TYPE', value='charz', lineno=31, index=477)
Token(type='ASSIGNMENT', value='=', lineno=31, index=483)
Token(type='STRING', value="'AQUI'", lineno=31, index=485)
Token(type='EOL', value='.z', lineno=31, index=492)
Token(type='IDENTIFIER', value='y', lineno=33, index=500)
Token(type='ASSIGNMENT', value='=', lineno=33, index=502)
Token(type='STRING', value="'AQUI 2'", lineno=33, index=504)
Token(type='EOL', value='.z', lineno=33, index=513)
Token(type='PRINT', value='zPrint', lineno=35, index=521)
Token(type='PARENTHESIS_OPEN', value='(', lineno=35, index=527)
Token(type='IDENTIFIER', value='x', lineno=35, index=528)
Token(type='PARENTHESIS_CLOSE', value=')', lineno=35, index=529)
Token(type='EOL', value='.z', lineno=35, index=531)
Token(type='PRINT', value='zPrint', lineno=37, index=539)
Token(type='PARENTHESIS_OPEN', value='(', lineno=37, index=545)
Token(type='IDENTIFIER', value='sub', lineno=37, index=546)
Token(type='PARENTHESIS_OPEN', value='(', lineno=37, index=549)
Token(type='IDENTIFIER', value='x', lineno=37, index=550)
Token(type='IDENTIFIER', value='x', lineno=37, index=553)
Token(type='PARENTHESIS_CLOSE', value=')', lineno=37, index=554)
Token(type='PARENTHESIS_CLOSE', value=')', lineno=37, index=555)
Token(type='EOL', value='.z', lineno=37, index=557)
Token(type='RETURN', value='return', lineno=39, index=565)
Token(type='NUMBER', value='0', lineno=39, index=572)
Token(type='EOL', value='.z', lineno=39, index=574)
Token(type='BLOCK_END', value='}', lineno=40, index=577)
Token(type='EOL', value='.z', lineno=40, index=579)
Binary file modified out/output
Binary file not shown.
46 changes: 25 additions & 21 deletions out/output.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,58 @@ entry:
%".4" = call i32 (i8*, ...) @"printf"(i8* %".3", [11 x i8] c"'Pre While'")
%"x.1" = load i8, i8* %"x"
%".5" = icmp slt i8 %"x.1", 6
br i1 %".5", label %"while_86f80181_cfb9_4f26_abd3_64d43f1d35dc", label %"exit_while_86f80181_cfb9_4f26_abd3_64d43f1d35dc"
while_86f80181_cfb9_4f26_abd3_64d43f1d35dc:
br i1 %".5", label %"while_1012060f_d1eb_4d1f_b57b_96e90b64ad0b", label %"exit_while_1012060f_d1eb_4d1f_b57b_96e90b64ad0b"
while_1012060f_d1eb_4d1f_b57b_96e90b64ad0b:
%"x.2" = load i8, i8* %"x"
%".7" = call i8 @"add_2"(i8 %"x.2")
store i8 %".7", i8* %"x"
%"x.3" = load i8, i8* %"x"
%".9" = icmp slt i8 %"x.3", 6
br i1 %".9", label %"while_86f80181_cfb9_4f26_abd3_64d43f1d35dc", label %"exit_while_86f80181_cfb9_4f26_abd3_64d43f1d35dc"
exit_while_86f80181_cfb9_4f26_abd3_64d43f1d35dc:
br i1 %".9", label %"while_1012060f_d1eb_4d1f_b57b_96e90b64ad0b", label %"exit_while_1012060f_d1eb_4d1f_b57b_96e90b64ad0b"
exit_while_1012060f_d1eb_4d1f_b57b_96e90b64ad0b:
%"x.4" = load i8, i8* %"x"
%".11" = call i8 @"add_2"(i8 %"x.4")
%".12" = sub i8 %".11", 2
%".13" = bitcast [5 x i8]* @"fstr" to i8*
%".14" = call i32 (i8*, ...) @"printf"(i8* %".13", i8 %".12")
%"x.5" = load i8, i8* %"x"
%".15" = icmp eq i8 %"x.5", 5
br i1 %".15", label %"exit_while_86f80181_cfb9_4f26_abd3_64d43f1d35dc.if", label %"exit_while_86f80181_cfb9_4f26_abd3_64d43f1d35dc.else"
exit_while_86f80181_cfb9_4f26_abd3_64d43f1d35dc.if:
br i1 %".15", label %"exit_while_1012060f_d1eb_4d1f_b57b_96e90b64ad0b.if", label %"exit_while_1012060f_d1eb_4d1f_b57b_96e90b64ad0b.else"
exit_while_1012060f_d1eb_4d1f_b57b_96e90b64ad0b.if:
%".17" = bitcast [5 x i8]* @"fstr" to i8*
%".18" = call i32 (i8*, ...) @"printf"(i8* %".17", i8 10)
br label %"exit_while_86f80181_cfb9_4f26_abd3_64d43f1d35dc.endif"
exit_while_86f80181_cfb9_4f26_abd3_64d43f1d35dc.else:
br label %"exit_while_1012060f_d1eb_4d1f_b57b_96e90b64ad0b.endif"
exit_while_1012060f_d1eb_4d1f_b57b_96e90b64ad0b.else:
%"x.6" = load i8, i8* %"x"
%"x.7" = load i8, i8* %"x"
%".20" = call i8 @"add_2"(i8 %"x.7")
%".21" = sub i8 %".20", 2
%".22" = icmp sgt i8 %"x.6", %".21"
br i1 %".22", label %"exit_while_86f80181_cfb9_...if", label %"exit_while_86f80181_cfb9_...else"
exit_while_86f80181_cfb9_4f26_abd3_64d43f1d35dc.endif:
br i1 %".22", label %"exit_while_1012060f_d1eb_...if", label %"exit_while_1012060f_d1eb_...else"
exit_while_1012060f_d1eb_4d1f_b57b_96e90b64ad0b.endif:
%"y" = alloca [6 x i8]
store [6 x i8] c"'AQUI'", [6 x i8]* %"y"
%"y.1" = alloca [8 x i8]
store [8 x i8] c"'AQUI 2'", [8 x i8]* %"y.1"
%"x.8" = load i8, i8* %"x"
%".31" = bitcast [5 x i8]* @"fstr" to i8*
%".32" = call i32 (i8*, ...) @"printf"(i8* %".31", i8 %"x.8")
%".33" = bitcast [5 x i8]* @"fstr" to i8*
%".34" = call i32 (i8*, ...) @"printf"(i8* %".33", i8 %"x.8")
%"x.9" = load i8, i8* %"x"
%"x.10" = load i8, i8* %"x"
%".33" = call i8 @"sub"(i8 %"x.9", i8 %"x.10")
%".34" = bitcast [5 x i8]* @"fstr" to i8*
%".35" = call i32 (i8*, ...) @"printf"(i8* %".34", i8 %".33")
%".35" = call i8 @"sub"(i8 %"x.9", i8 %"x.10")
%".36" = bitcast [5 x i8]* @"fstr" to i8*
%".37" = call i32 (i8*, ...) @"printf"(i8* %".36", i8 %".35")
ret i8 0
exit_while_86f80181_cfb9_...if:
exit_while_1012060f_d1eb_...if:
%".24" = bitcast [5 x i8]* @"fstr" to i8*
%".25" = call i32 (i8*, ...) @"printf"(i8* %".24", i8 12)
br label %"exit_while_86f80181_cfb9_...endif"
exit_while_86f80181_cfb9_...else:
br label %"exit_while_1012060f_d1eb_...endif"
exit_while_1012060f_d1eb_...else:
%".27" = bitcast [5 x i8]* @"fstr" to i8*
%".28" = call i32 (i8*, ...) @"printf"(i8* %".27", i8 11)
br label %"exit_while_86f80181_cfb9_...endif"
exit_while_86f80181_cfb9_...endif:
br label %"exit_while_86f80181_cfb9_4f26_abd3_64d43f1d35dc.endif"
br label %"exit_while_1012060f_d1eb_...endif"
exit_while_1012060f_d1eb_...endif:
br label %"exit_while_1012060f_d1eb_4d1f_b57b_96e90b64ad0b.endif"
}

declare i32 @"printf"(i8* %".1", ...)
Expand Down
Binary file modified out/output.o
Binary file not shown.
4 changes: 3 additions & 1 deletion src/Nodes/Identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ def Evaluate(self, symbol_table: SymbolTable):
if self.type is None and symbol_table.get(self.value) is None:
raise ValueError(f'Variable {self.value} was not declared')

if symbol_table.get(self.value) is not None and self.type != TokenTypes.STRING_TYPE:
if symbol_table.get(self.value) is not None:
ir_alloc = symbol_table.get(self.value).get("pointer", None)
if 'x i8]' in str(ir_alloc.type):
ir_alloc = self.builder.alloca(i.type, name=self.value)
else:
ir_alloc = self.builder.alloca(i.type, name=self.value)

Expand Down
2 changes: 0 additions & 2 deletions src/Nodes/Variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ def __init__(self, value: str):
self.value = value

def Evaluate(self, symbol_table: SymbolTable):
print(symbol_table.table)
print(self.value)
symbol_table_entry = symbol_table.get(self.value)
alloc_ptr = symbol_table_entry.get("pointer", None)
i = self.builder.load(alloc_ptr, name=self.value)
Expand Down
4 changes: 4 additions & 0 deletions tests/showcase.z
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ numz func main() {
zPrint(11) .z
} .z

var y -> charz = 'AQUI' .z

y = 'AQUI 2' .z

zPrint(x) .z

zPrint(sub(x, x)) .z
Expand Down

0 comments on commit 639f903

Please sign in to comment.