Commits

Paweł Wieczorek committed 75823de

Added FrozenHashtbl util

  • Participants
  • Parent commits 66184fd
  • Branches very_simple_while_compiler

Comments (0)

Files changed (4)

source/Compiler/WhileX86Compiler.ml

     let create_symbol_for_exception identifier =
         make_symbol ("WHILE_EXCEPTION_" ^ AST.string_of_identifier identifier)
 
-    let compute_memory_map declarations =
+    let compute_global_variable_table declarations =
         let hashtbl = Hashtbl.create 7 in
 
         let handle_variable variable =
         List.iter from_declaration declarations;
         hashtbl
 
-    let compute_exception_map declarations=
+    let compute_exception_symbol_table declarations=
         let hashtbl = Hashtbl.create 7 in
         let from_identifier identifier =
             let symbol = create_symbol_for_exception identifier in
  * Context
  ********************************************************************************************************************)
 
-module Context = struct
+
+module Context = struct 
+    
+    type scope_block =
+        { local_variable_map       : (AST.variable, location32) FrozenHashtbl.t
+        }
+
+    type frame_description =
+        { scope_blocks              : scope_block list ref
+        ; leave_symbol              : symbol
+        }
 
     type context =
-        { symbolnum     : int ref
-        ; symbol_table  : (AST.identifier, symbol) Hashtbl.t
-        ; memory_map    : (AST.variable, symbol) Hashtbl.t
-        ; exception_map : (AST.identifier, symbol) Hashtbl.t
-        ; frame_leave   : symbol option ref
+        { symbolnum                 : int ref
+        ; symbol_table              : (AST.identifier, symbol) FrozenHashtbl.t
+        ; global_variable_table     : (AST.variable,   symbol) FrozenHashtbl.t
+        ; exception_symbol_table    : (AST.identifier, symbol) FrozenHashtbl.t
+        ; frame_leave               : symbol option ref
         }
 
     let get_all_symbols_from_table context =
-        Hashtbl.fold (fun _ symbol acc -> symbol::acc) context.symbol_table [] 
+        FrozenHashtbl.fold (fun _ symbol acc -> symbol::acc) context.symbol_table [] 
 
     let get_all_symbols_from_mem context = 
-        Hashtbl.fold (fun _ symbol acc -> symbol::acc) context.memory_map [] 
+        FrozenHashtbl.fold (fun _ symbol acc -> symbol::acc) context.global_variable_table [] 
 
     let get_all_exceptions context =
-        Hashtbl.fold (fun identifier symbol acc -> (identifier,symbol)::acc) context.exception_map []
+        FrozenHashtbl.fold (fun identifier symbol acc -> (identifier,symbol)::acc) context.exception_symbol_table []
 
     let get_frame_leave_symbol context =
         match !(context.frame_leave) with
 
     let lookup_for_symbol context identifier =
         try
-            Hashtbl.find context.symbol_table identifier
+            FrozenHashtbl.find context.symbol_table identifier
         with Not_found ->
             Error.internal_error "Invalid symbol table"
 
     let lookup_for_variable_symbol context variable =
         try
-            Hashtbl.find context.memory_map variable
+            FrozenHashtbl.find context.global_variable_table variable
         with Not_found ->
             Error.internal_error "Invalid memory map"
 
     let lookup_for_exception_symbol context identifier =
         try
-            Hashtbl.find context.exception_map identifier
+            FrozenHashtbl.find context.exception_symbol_table identifier
         with Not_found ->
             Error.internal_error "Invalid exception map"
 
     let make_context declarations =
-        { symbolnum     = ref 0
-        ; symbol_table  = Helpers.compute_symbol_table declarations
-        ; memory_map    = Helpers.compute_memory_map declarations
-        ; exception_map = Helpers.compute_exception_map declarations
-        ; frame_leave   = ref None
+        { symbolnum                 = ref 0
+        ; symbol_table              = FrozenHashtbl.freeze (Helpers.compute_symbol_table declarations)
+        ; global_variable_table     = FrozenHashtbl.freeze (Helpers.compute_global_variable_table declarations)
+        ; exception_symbol_table    = FrozenHashtbl.freeze (Helpers.compute_exception_symbol_table declarations)
+        ; frame_leave               = ref None
         } 
 
     let create_fresh_local_symbol ?(suffix="") context = 
             let routine_symbol = Context.lookup_for_symbol context routine in
             call_routine_symbol context routine_symbol arguments
 
+        | AST.AE_UnaryOperator (op, expr1) ->
+            let (computing_code, result_reg) = compile_arithmetic_expression context expr1 in
+
+            let code = List.concat
+                [ computing_code
+                ] in
+
+            (code, result_reg)
+
+
         | AST.AE_BinaryOperator (op, expr1, expr2) ->
             let (code1, reg1) = compile_arithmetic_expression context expr1 in
             let (code2, reg2) = compile_arithmetic_expression context expr2 in
 
     let rec compile_boolean_expression context else_branch = function
         | AST.BE_Constant c  ->
-            let imm32 = if c then 1l else 0l in
-            let code = 
-                [ emit_MOVL (lv32_imm imm32) (loc32_reg EAX)
-                ; emit_CMP  (lv32_imm 0l) (loc32_reg EAX)
-                ; emit_JE (loc32_symbol else_branch)
-                ] in
-            code
+            if c then
+                []
+            else
+                [ emit_JMP (loc32_symbol else_branch)
+                ] 
+
+        | AST.BE_UnaryOperator(AST.BOP_NOT, bexpr) ->
+            let symbol_not = Context.create_fresh_local_symbol ~suffix:"_boolean_not"   context in
+            let jumping_code = compile_boolean_expression context symbol_not bexpr in
+
+            List.concat
+                [ jumping_code
+                ; [emit_JMP (loc32_symbol else_branch)]
+                ; [emit_label symbol_not]
+                ]
+
+        | AST.BE_BinaryOperator(op, bexpr1, expr2) ->
+            [
+            ]
 
         | AST.BE_ArithmeticBinaryOperator (op, expr1, expr2)  ->
             let (code1, reg1) = compile_arithmetic_expression context expr1 in

source/Util.mllib

 Tty
 Util
 Tempfile
+FrozenHashtbl

source/Util/FrozenHashtbl.ml

+(*
+ * Opifex
+ *
+ * Copyrights(C) 2012,2013 by Pawel Wieczorek <wieczyk at gmail>
+ *)
+
+open Batteries
+
+(*********************************************************************************************************************
+ *
+ ********************************************************************************************************************)
+
+type ('k, 'v) t = ('k, 'v) Hashtbl.t
+
+let freeze t = t
+
+let find h k = Hashtbl.find h k
+
+let fold f h a = Hashtbl.fold f h a

source/Util/FrozenHashtbl.mli

+(*
+ * Opifex
+ *
+ * Copyrights(C) 2012,2013 by Pawel Wieczorek <wieczyk at gmail>
+ *)
+
+open Batteries
+
+(*********************************************************************************************************************
+ *
+ ********************************************************************************************************************)
+
+type ('k, 'v) t
+
+val freeze : ('k, 'v) Hashtbl.t -> ('k, 'v) t
+
+val find : ('k, 'v) t -> 'k -> 'v
+
+val fold : ('k -> 'v -> 'a -> 'a) -> ('k, 'v) t -> 'a -> 'a