From: Mike Lowis Date: Mon, 20 Nov 2023 21:32:45 +0000 (-0500) Subject: started adding comparison operators X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=c7ef43e00d9c3b874b0619d62497afd5d82ead61;p=proto%2Faas.git started adding comparison operators --- diff --git a/aas.rb b/aas.rb index 4f69b8e..3ff42f6 100755 --- a/aas.rb +++ b/aas.rb @@ -1,18 +1,18 @@ #!/bin/env ruby # TODO: -# remove tempfile usage -# implement peephole optimization +# local variable loading +# local variable saving +# implement if statements +# # global variable addressing # global variable loading # global variable saving # local variable addressing -# local variable loading -# local variable saving # parameter variable addressing -# parameter variable loading # parameter variable saving -# implement if statements +# remove tempfile usage +# implement peephole optimization require "tempfile" @@ -290,13 +290,28 @@ class Function end ## - # Function Scaffolding: Locals and Returns + # Function Scaffolding: Parameters, Locals, and Returns ## def get_param(num) emit "movq #{8*(num+2)}(%rbp), %rax" emit "pushq %rax" end + def set_param(num) + emit "popq %rax" + emit "movq %rax, #{8*(num+2)}(%rbp)" + end + + def get_local(num) + emit "movq #{-8*(num)}(%rbp), %rax" + emit "pushq %rax" + end + + def set_local(num) + emit "popq %rax" + emit "movq %rax, #{-8*num}(%rbp)" + end + def locals(count) $asm_out.write "subq $#{count * 8}, %rsp\n" end diff --git a/cerise.m b/cerise.m index 8de6003..ba239de 100644 --- a/cerise.m +++ b/cerise.m @@ -1,6 +1,6 @@ sum(a,b) { - return a+b + return a true, - "-" => true, - "*" => true, - "/" => true, - "%" => true, + "+" => true, + "-" => true, + "*" => true, + "/" => true, + "%" => true, + "<" => true, + ">" => true, + "<=" => true, + ">=" => true, + "==" => true, + "!=" => true, } def operator? @@ -665,7 +671,12 @@ class Parser end def expression() - simple_expr() + left = simple_expr() + if matches_any(["==", "!=", "<", "<=", ">", ">="]) + right = simple_expr() + left = IR::Op.new(op.location, nil, op.text, left, right) + end + left end def simple_expr() @@ -785,6 +796,8 @@ module Codegen puts " divi" when "%" puts " modi" + when "<" + puts " lti" else raise "not implemented" end