]> git.mdlowis.com Git - proto/aas.git/commitdiff
started adding comparison operators
authorMike Lowis <mike.lowis@gentex.com>
Mon, 20 Nov 2023 21:32:45 +0000 (16:32 -0500)
committerMike Lowis <mike.lowis@gentex.com>
Mon, 20 Nov 2023 21:32:45 +0000 (16:32 -0500)
aas.rb
cerise.m
cerise.rb

diff --git a/aas.rb b/aas.rb
index 4f69b8ed2b4b22e27624beb0899f26d20ac90b46..3ff42f64e9a2a6d3e6d9087079bdd7774dcaa88e 100755 (executable)
--- 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
index 8de600348c0f9b8d40e0dbd9a14af3c8b437e452..ba239dee46100a7a7ac24a2bc70bce0349a0bdd3 100644 (file)
--- a/cerise.m
+++ b/cerise.m
@@ -1,6 +1,6 @@
 sum(a,b)
 {
-    return a+b
+    return a<b
 }
 
 main()
index 836940f117db711d45395b611b819e1869f7238a..e4e82a7a2a83c760cbd19e0ee7c97fb40a3f1cba 100755 (executable)
--- a/cerise.rb
+++ b/cerise.rb
@@ -562,11 +562,17 @@ class Parser
 
 
   OPERATORS = {
-    "+" => 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