Commits

Paweł Wieczorek committed ac88f2f

Routine calls in WHILE in Aexprs, without parser alignment

Comments (0)

Files changed (2)

source/Compiler/WhileX86Compiler.ml

 module AstCompiler = struct
 
     (* Call routine with the standard C calling conversion on x86 *)
-    let call_routine loc32_routine lv32_args = 
+    let call_routine_lv32_args loc32_routine lv32_args = 
         let passing_arguments_code =
             List.rev_map emit_PUSHL lv32_args
             in
             ; calling_code
             ]
 
-    let call_routine_symbol routine_symbol lv32_args = 
-        call_routine (loc32_symbol routine_symbol) lv32_args
+    let call_routine_symbol_lv32_args routine_symbol lv32_args = 
+        call_routine_lv32_args (loc32_symbol routine_symbol) lv32_args
+
 
     let compile_arithmetic_binary_operator reg1 reg2 aop =
         let compile emit rreg =
             | AST.AOP_MUL -> compile emit_IMULL reg2
             | _ -> Error.not_yet_implemented "arith bin op"
 
-    let rec compile_arithmetic_expression context = function
+    let rec call_routine context loc32_routine arguments =
+
+        let pass_argument arithmetic_expression =
+            let (computing_code, result_reg) = compile_arithmetic_expression context arithmetic_expression in
+            List.concat
+                [ computing_code
+                ; [emit_PUSHL (lv32_reg result_reg)]
+                ] in
+
+        let passing_arguments_code =
+            List.rev_map pass_argument arguments
+            in
+        
+        let calling_code = 
+            let allocated_stack_size = Int32.of_int (4 * List.length arguments) in
+            [ emit_CALL loc32_routine 
+            ; emit_ADDL (lv32_imm allocated_stack_size) (loc32_reg ESP)
+            ] in
+
+        let code = List.concat
+            [ List.concat passing_arguments_code
+            ; calling_code
+            ] in
+
+        let result_reg = EAX in
+
+        (code, result_reg)
+
+    and call_routine_symbol context routine_symbol arguments =
+        call_routine context (loc32_symbol routine_symbol) arguments
+
+    and compile_arithmetic_expression context = function
         | AST.AE_Constant i ->
             let imm32 = Int32.of_int i in
             let code = 
             | AST.BOP_GT -> compile emit_JNG
             | AST.BOP_GEQ -> compile emit_JNGE
 
+
     let rec compile_boolean_expression context else_branch = function
         | AST.BE_Constant c  ->
             let imm32 = if c then 1l else 0l in
 
         | AST.CMD_Call (routine, args) ->
             let routine_symbol = Context.lookup_for_symbol context routine in
-            call_routine_symbol routine_symbol []
+            call_routine_symbol_lv32_args routine_symbol []
 
         | AST.CMD_Assign (variable, arithmetic_expression) ->
             let (expr_code, expr_reg)   = compile_arithmetic_expression context arithmetic_expression in
 
         | AST.CMD_Write variable ->
             let variable_symbol = Context.lookup_for_variable_symbol context variable in
-            call_routine_symbol Runtime.write_procedure_symbol [lv32_symbol variable_symbol]
+            call_routine_symbol_lv32_args Runtime.write_procedure_symbol [lv32_symbol variable_symbol]
 
         | AST.CMD_While (cond, body) ->
             let symbol_loop_begin = Context.create_fresh_local_symbol ~suffix:"_loop_begin" context in
                 ]
 
         | AST.CMD_Abort ->
-            call_routine_symbol Runtime.abort_procedure_symbol []
+            call_routine_symbol_lv32_args Runtime.abort_procedure_symbol []
 
         | AST.CMD_Compose (c1, c2) ->
             List.concat
         | AST.CMD_Throw (exception_identifier) ->
             let exception_symbol = Context.lookup_for_exception_symbol context exception_identifier in
                 let calling_code =
-                    call_routine_symbol Runtime.get_exception_handler_symbol
+                    call_routine_symbol_lv32_args Runtime.get_exception_handler_symbol
                         [lv32_symboladdr exception_symbol
                         ]
                         in
             let exception_symbol = Context.lookup_for_exception_symbol context exception_identifier in
             
             let try_open =
-                call_routine_symbol Runtime.register_exception_handler_symbol
+                call_routine_symbol_lv32_args Runtime.register_exception_handler_symbol
                     [ lv32_symboladdr exception_symbol
                     ; lv32_symboladdr symbol_catch
                     ]

source/Lang/While/AST.ml

 type arithmetic_expression
     = AE_Constant of int
     | AE_Variable of variable
+    | AE_Call of identifier * arithmetic_expression list
     | AE_BinaryOperator of arithmetic_binary_operator * arithmetic_expression * arithmetic_expression
     | AE_UnaryOperator of arithmetic_unary_operator * arithmetic_expression