#!/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"
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
OPERATORS = {
- "+" => true,
- "-" => true,
- "*" => true,
- "/" => true,
- "%" => true,
+ "+" => true,
+ "-" => true,
+ "*" => true,
+ "/" => true,
+ "%" => true,
+ "<" => true,
+ ">" => true,
+ "<=" => true,
+ ">=" => true,
+ "==" => true,
+ "!=" => true,
}
def operator?
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()
puts " divi"
when "%"
puts " modi"
+ when "<"
+ puts " lti"
else
raise "not implemented"
end