diff --git a/lib/pinc_bytecode/instruction.ml b/lib/pinc_bytecode/instruction.ml index 9b35d02..1d971d5 100644 --- a/lib/pinc_bytecode/instruction.ml +++ b/lib/pinc_bytecode/instruction.ml @@ -25,8 +25,7 @@ type t = | I_Set_Global of int | I_Get_Global of int | I_Concat - | I_Dynamic_Array - | I_Array of int + | I_Array | I_Record of int | I_Index | I_Dot_Index @@ -71,7 +70,7 @@ let byte = function | I_Set_Global _ -> 0x17 | I_Get_Global _ -> 0x18 | I_Concat -> 0x19 - | I_Array _ -> 0x1A + | I_Array -> 0x1A | I_Record _ -> 0x1B | I_Index -> 0x1C | I_Dot_Index -> 0x1D @@ -82,7 +81,6 @@ let byte = function | I_Set_Local _ -> 0x22 | I_Get_Local _ -> 0x23 | I_Length -> 0x24 - | I_Dynamic_Array -> 0x25 | I_Get_Builtin _ -> 0x26 | I_Closure _ -> 0x27 | I_Get_Free _ -> 0x28 @@ -97,7 +95,6 @@ let operands_length = function | I_Jump_If_False _ | I_Set_Global _ | I_Get_Global _ - | I_Array _ | I_Record _ | I_Set_Local _ | I_Get_Local _ @@ -132,7 +129,7 @@ let operands_length = function | I_Range_Inclusive | I_Return | I_Length - | I_Dynamic_Array + | I_Array | I_Current_Closure | I_Halt | I_Debug_Print_Stack -> 0 @@ -180,9 +177,7 @@ let decode bytes offset = let offset, addr = Int32.read_bytes bytes offset in (offset, I_Get_Global (Int32.to_int addr)) | 0x19 -> (offset, I_Concat) - | 0x1A -> - let offset, length = Int32.read_bytes bytes offset in - (offset, I_Array (Int32.to_int length)) + | 0x1A -> (offset, I_Array) | 0x1B -> let offset, length = Int32.read_bytes bytes offset in (offset, I_Record (Int32.to_int length)) @@ -201,7 +196,6 @@ let decode bytes offset = let offset, addr = Int32.read_bytes bytes offset in (offset, I_Get_Local (Int32.to_int addr)) | 0x24 -> (offset, I_Length) - | 0x25 -> (offset, I_Dynamic_Array) | 0x26 -> let offset, addr = Int32.read_bytes bytes offset in (offset, I_Get_Builtin (Int32.to_int addr)) @@ -246,7 +240,7 @@ let pp fmt = function | I_Null -> Format.fprintf fmt "I_Null" | I_Get_Global addr -> Format.fprintf fmt "I_Get_Global 0x%08X (%08i)" addr addr | I_Set_Global addr -> Format.fprintf fmt "I_Set_Global 0x%08X (%08i)" addr addr - | I_Array length -> Format.fprintf fmt "I_Array %i" length + | I_Array -> Format.fprintf fmt "I_Array" | I_Concat -> Format.fprintf fmt "I_Concat" | I_Record length -> Format.fprintf fmt "I_Record %i" length | I_Index -> Format.fprintf fmt "I_Index" @@ -258,7 +252,6 @@ let pp fmt = function | I_Get_Local addr -> Format.fprintf fmt "I_Get_Local 0x%08X (%08i)" addr addr | I_Set_Local addr -> Format.fprintf fmt "I_Set_Local 0x%08X (%08i)" addr addr | I_Length -> Format.fprintf fmt "I_Length" - | I_Dynamic_Array -> Format.fprintf fmt "I_Dynamic_Array" | I_Get_Builtin addr -> Format.fprintf fmt "I_Get_Builtin 0x%08X (%08i)" addr addr | I_Get_Free addr -> Format.fprintf fmt "I_Get_Free 0x%08X (%08i)" addr addr | I_Closure (fn_addr, free_variables) -> @@ -290,7 +283,6 @@ let to_bytes t = | I_Jump_If_False op | I_Set_Global op | I_Get_Global op - | I_Array op | I_Record op | I_Set_Local op | I_Get_Local op @@ -327,7 +319,7 @@ let to_bytes t = | I_Range_Inclusive | I_Return | I_Length - | I_Dynamic_Array + | I_Array | I_Current_Closure | I_Halt | I_Debug_Print_Stack -> () diff --git a/lib/pinc_compiler/compiler.ml b/lib/pinc_compiler/compiler.ml index 7cc951e..e3ef76d 100644 --- a/lib/pinc_compiler/compiler.ml +++ b/lib/pinc_compiler/compiler.ml @@ -220,7 +220,8 @@ let rec compile_expr t (expr : Pinc_Types.Ast.expression) = | UppercaseIdentifierExpression _ -> raise_notrace TODO | Array a -> let t = Array.fold_left compile_expr t a in - emit t @@ Pinc_Bytecode.Instruction.I_Array (Array.length a) + let t = emit_constant t @@ Pinc_Bytecode.Value.Int (Array.length a) in + emit t @@ Pinc_Bytecode.Instruction.I_Array | Record map -> let bindings = StringMap.bindings map in let keys, values = List.split bindings in @@ -488,7 +489,7 @@ and compile_loop_expression t ~index ~iterator ~reverse:_ ~iterable ~body = let t = emit t @@ Pinc_Bytecode.Instruction.I_Jump_If_False jump_address in (* Create array with values left on stack *) let t = emit_get_symbol t length_symbol in - let t = emit t @@ Pinc_Bytecode.Instruction.I_Dynamic_Array in + let t = emit t @@ Pinc_Bytecode.Instruction.I_Array in t and compile_stmt t (stmt : Pinc_Types.Ast.statement) = diff --git a/lib/pinc_vm/vm.ml b/lib/pinc_vm/vm.ml index af4e26a..145c63d 100644 --- a/lib/pinc_vm/vm.ml +++ b/lib/pinc_vm/vm.ml @@ -854,7 +854,7 @@ let execute_get_local addr = call_next_instruction t ;; -let execute_dynamic_array t = +let execute_array t = let length = match Stack.peek_tag t.stack 0 with | Tag_Int -> Stack.pop_int t.stack @@ -866,14 +866,6 @@ let execute_dynamic_array t = call_next_instruction t ;; -let execute_array length = - fun t -> - let elements = Stack.pop_n t.stack length in - let value = Value.Array elements in - Stack.push_value t.stack value; - call_next_instruction t -;; - let execute_record length = fun t -> let values = Array.to_list @@ Stack.pop_n t.stack length in @@ -938,7 +930,6 @@ let resolve_instructions instructions = | Instruction.I_Get_Free addr -> execute_get_free addr | Instruction.I_Range -> execute_binary_range ~inclusive:false | Instruction.I_Range_Inclusive -> execute_binary_range ~inclusive:true - | Instruction.I_Array length -> execute_array length | Instruction.I_Record length -> execute_record length | Instruction.I_Closure (fn_addr, num_free_variables) -> execute_closure fn_addr num_free_variables @@ -968,7 +959,7 @@ let resolve_instructions instructions = | Instruction.I_Minus -> execute_unary_minus | Instruction.I_Not -> execute_unary_not | Instruction.I_Null -> execute_null - | Instruction.I_Dynamic_Array -> execute_dynamic_array + | Instruction.I_Array -> execute_array | Instruction.I_Return -> execute_return | Instruction.I_Length -> execute_length | Instruction.I_Current_Closure -> execute_current_closure diff --git a/test/vm/array_instructions.t b/test/vm/array_instructions.t index 0e7b0b1..e7da9d2 100644 --- a/test/vm/array_instructions.t +++ b/test/vm/array_instructions.t @@ -1,7 +1,11 @@ $ NO_COLOR="1" print_instructions . EmptyArray + [CONSTANTS] + 0x00000000 (00000000) : 0 + [INSTRUCTIONS] - 0000 I_Array 0 - 0005 I_Pop + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Array + 0006 I_Pop $ NO_COLOR="1" print_instructions . Array @@ -11,6 +15,7 @@ 0x00000002 (00000002) : 3 0x00000003 (00000003) : 4 0x00000004 (00000004) : 5 + 0x00000005 (00000005) : 5 [INSTRUCTIONS] 0000 I_Constant 0x00000000 (00000000) @@ -18,8 +23,9 @@ 0010 I_Constant 0x00000002 (00000002) 0015 I_Constant 0x00000003 (00000003) 0020 I_Constant 0x00000004 (00000004) - 0025 I_Array 5 - 0030 I_Pop + 0025 I_Constant 0x00000005 (00000005) + 0030 I_Array + 0031 I_Pop $ NO_COLOR="1" print_instructions . ExpressionArray @@ -32,6 +38,7 @@ 0x00000005 (00000005) : 6 0x00000006 (00000006) : 7 0x00000007 (00000007) : 8 + 0x00000008 (00000008) : 4 [INSTRUCTIONS] 0000 I_Constant 0x00000000 (00000000) @@ -46,8 +53,9 @@ 0033 I_Constant 0x00000006 (00000006) 0038 I_Constant 0x00000007 (00000007) 0043 I_Div - 0044 I_Array 4 - 0049 I_Pop + 0044 I_Constant 0x00000008 (00000008) + 0049 I_Array + 0050 I_Pop $ NO_COLOR="1" print_instructions . ArrayAccess @@ -60,8 +68,9 @@ 0x00000005 (00000005) : 6 0x00000006 (00000006) : 7 0x00000007 (00000007) : 8 - 0x00000008 (00000008) : 3 - 0x00000009 (00000009) : 2 + 0x00000008 (00000008) : 4 + 0x00000009 (00000009) : 3 + 0x0000000A (00000010) : 2 [INSTRUCTIONS] 0000 I_Constant 0x00000000 (00000000) @@ -76,12 +85,13 @@ 0033 I_Constant 0x00000006 (00000006) 0038 I_Constant 0x00000007 (00000007) 0043 I_Div - 0044 I_Array 4 - 0049 I_Constant 0x00000008 (00000008) - 0054 I_Constant 0x00000009 (00000009) - 0059 I_Sub - 0060 I_Index - 0061 I_Pop + 0044 I_Constant 0x00000008 (00000008) + 0049 I_Array + 0050 I_Constant 0x00000009 (00000009) + 0055 I_Constant 0x0000000A (00000010) + 0060 I_Sub + 0061 I_Index + 0062 I_Pop $ NO_COLOR="1" print_instructions . ArrayRange diff --git a/test/vm/builtin_functions_instructions.t b/test/vm/builtin_functions_instructions.t index 092ac28..a195791 100644 --- a/test/vm/builtin_functions_instructions.t +++ b/test/vm/builtin_functions_instructions.t @@ -16,6 +16,7 @@ 0x00000001 (00000001) : 2 0x00000002 (00000002) : 3 0x00000003 (00000003) : 4 + 0x00000004 (00000004) : 4 [INSTRUCTIONS] 0000 I_Get_Builtin 0x00000000 (00000000) @@ -27,9 +28,10 @@ 0022 I_Constant 0x00000001 (00000001) 0027 I_Constant 0x00000002 (00000002) 0032 I_Constant 0x00000003 (00000003) - 0037 I_Array 4 - 0042 I_Call 1 - 0047 I_Pop + 0037 I_Constant 0x00000004 (00000004) + 0042 I_Array + 0043 I_Call 1 + 0048 I_Pop $ NO_COLOR="1" print_instructions . BuiltinFunctionStringLength [CONSTANTS] diff --git a/test/vm/loop_instructions.t b/test/vm/loop_instructions.t index 70fc33f..0b687b4 100644 --- a/test/vm/loop_instructions.t +++ b/test/vm/loop_instructions.t @@ -5,8 +5,9 @@ 0x00000002 (00000002) : 3 0x00000003 (00000003) : 4 0x00000004 (00000004) : 5 - 0x00000005 (00000005) : 0 - 0x00000006 (00000006) : 1 + 0x00000005 (00000005) : 5 + 0x00000006 (00000006) : 0 + 0x00000007 (00000007) : 1 [INSTRUCTIONS] 0000 I_Constant 0x00000000 (00000000) @@ -14,32 +15,33 @@ 0010 I_Constant 0x00000002 (00000002) 0015 I_Constant 0x00000003 (00000003) 0020 I_Constant 0x00000004 (00000004) - 0025 I_Array 5 - 0030 I_Set_Global 0x00000000 (00000000) - 0035 I_Null - 0036 I_Pop - 0037 I_Constant 0x00000005 (00000005) - 0042 I_Set_Global 0x00000003 (00000003) - 0047 I_Get_Global 0x00000000 (00000000) - 0052 I_Set_Global 0x00000004 (00000004) - 0057 I_Get_Global 0x00000004 (00000004) - 0062 I_Length - 0063 I_Set_Global 0x00000002 (00000002) - 0068 I_Get_Global 0x00000004 (00000004) - 0073 I_Get_Global 0x00000003 (00000003) - 0078 I_Index - 0079 I_Set_Global 0x00000001 (00000001) - 0084 I_Get_Global 0x00000001 (00000001) - 0089 I_Get_Global 0x00000003 (00000003) - 0094 I_Mul - 0095 I_Constant 0x00000006 (00000006) - 0100 I_Get_Global 0x00000003 (00000003) - 0105 I_Add - 0106 I_Set_Global 0x00000003 (00000003) - 0111 I_Get_Global 0x00000003 (00000003) - 0116 I_Get_Global 0x00000002 (00000002) - 0121 I_Greater_Equal - 0122 I_Jump_If_False 0x00000010 (00000016) - 0127 I_Get_Global 0x00000002 (00000002) - 0132 I_Dynamic_Array - 0133 I_Pop + 0025 I_Constant 0x00000005 (00000005) + 0030 I_Array + 0031 I_Set_Global 0x00000000 (00000000) + 0036 I_Null + 0037 I_Pop + 0038 I_Constant 0x00000006 (00000006) + 0043 I_Set_Global 0x00000003 (00000003) + 0048 I_Get_Global 0x00000000 (00000000) + 0053 I_Set_Global 0x00000004 (00000004) + 0058 I_Get_Global 0x00000004 (00000004) + 0063 I_Length + 0064 I_Set_Global 0x00000002 (00000002) + 0069 I_Get_Global 0x00000004 (00000004) + 0074 I_Get_Global 0x00000003 (00000003) + 0079 I_Index + 0080 I_Set_Global 0x00000001 (00000001) + 0085 I_Get_Global 0x00000001 (00000001) + 0090 I_Get_Global 0x00000003 (00000003) + 0095 I_Mul + 0096 I_Constant 0x00000007 (00000007) + 0101 I_Get_Global 0x00000003 (00000003) + 0106 I_Add + 0107 I_Set_Global 0x00000003 (00000003) + 0112 I_Get_Global 0x00000003 (00000003) + 0117 I_Get_Global 0x00000002 (00000002) + 0122 I_Greater_Equal + 0123 I_Jump_If_False 0x00000011 (00000017) + 0128 I_Get_Global 0x00000002 (00000002) + 0133 I_Array + 0134 I_Pop diff --git a/test/vm/record_instructions.t b/test/vm/record_instructions.t index 348e9c6..ec5c8f2 100644 --- a/test/vm/record_instructions.t +++ b/test/vm/record_instructions.t @@ -48,6 +48,7 @@ 0x00000010 (00000016) : 6 0x00000011 (00000017) : 7 0x00000012 (00000018) : 8 + 0x00000013 (00000019) : 8 [INSTRUCTIONS] 0000 I_Constant 0x00000000 (00000000) @@ -69,11 +70,12 @@ 0080 I_Constant 0x00000010 (00000016) 0085 I_Constant 0x00000011 (00000017) 0090 I_Constant 0x00000012 (00000018) - 0095 I_Array 8 - 0100 I_Record 3 - 0105 I_True - 0106 I_Record 4 - 0111 I_Pop + 0095 I_Constant 0x00000013 (00000019) + 0100 I_Array + 0101 I_Record 3 + 0106 I_True + 0107 I_Record 4 + 0112 I_Pop $ NO_COLOR="1" print_instructions . RecordNested @@ -97,6 +99,7 @@ 0x00000010 (00000016) : 6 0x00000011 (00000017) : 7 0x00000012 (00000018) : 8 + 0x00000013 (00000019) : 8 [INSTRUCTIONS] 0000 I_Constant 0x00000000 (00000000) @@ -118,11 +121,12 @@ 0080 I_Constant 0x00000010 (00000016) 0085 I_Constant 0x00000011 (00000017) 0090 I_Constant 0x00000012 (00000018) - 0095 I_Array 8 - 0100 I_Record 3 - 0105 I_True - 0106 I_Record 4 - 0111 I_Pop + 0095 I_Constant 0x00000013 (00000019) + 0100 I_Array + 0101 I_Record 3 + 0106 I_True + 0107 I_Record 4 + 0112 I_Pop $ NO_COLOR="1" print_instructions . RecordAccessDot