Use custom 3-address code operands

This commit is contained in:
2025-11-14 22:57:00 +01:00
parent cc3acdfd12
commit 8da9241233

View File

@@ -403,7 +403,7 @@ type
start, start,
ret ret
); );
ElnaTacOperand = (register, immediate, symbol, offset, stack); ElnaTacOperand = (temporary, immediate, symbol, stack);
ElnaRtlOperand = (register, immediate, symbol, offset); ElnaRtlOperand = (register, immediate, symbol, offset);
ElnaRtlRegister = ( ElnaRtlRegister = (
zero, zero,
@@ -854,7 +854,7 @@ begin
elna_rtl_instruction_set_operand(next_instruction, 2, ElnaRtlOperand.offset, into, 0); elna_rtl_instruction_set_operand(next_instruction, 2, ElnaRtlOperand.offset, into, 0);
ElnaInstructionList_set_next(result, next_instruction) ElnaInstructionList_set_next(result, next_instruction)
elsif operand_type = ElnaTacOperand.register then elsif operand_type = ElnaTacOperand.temporary then
result := elna_rtl_instruction_create(ElnaRtlOperator.move); result := elna_rtl_instruction_create(ElnaRtlOperator.move);
elna_rtl_instruction_set_operand(result, 1, ElnaRtlOperand.register, into, 0); elna_rtl_instruction_set_operand(result, 1, ElnaRtlOperand.register, into, 0);
elna_rtl_instruction_set_operand(result, 2, ElnaRtlOperand.register, operand_value, 0) elna_rtl_instruction_set_operand(result, 2, ElnaRtlOperand.register, operand_value, 0)
@@ -928,10 +928,11 @@ begin
return lhs return lhs
end; end;
proc elna_rtl_binary_equality(tac_instruction: Word, binary_result: Word, next_instruction: Word); proc elna_rtl_binary_equality(tac_instruction: Word, instruction_kind: Word, next_instruction: Word);
var var
operands: Word; operands: Word;
intermediate_instruction: Word; intermediate_instruction: Word;
binary_result: Word;
begin begin
operands := elna_rtl_binary_operands(tac_instruction, next_instruction); operands := elna_rtl_binary_operands(tac_instruction, next_instruction);
intermediate_instruction := elna_rtl_instruction_create(ElnaRtlOperator._xor); intermediate_instruction := elna_rtl_instruction_create(ElnaRtlOperator._xor);
@@ -942,6 +943,8 @@ begin
ElnaInstructionList_set_next(next_instruction^, intermediate_instruction); ElnaInstructionList_set_next(next_instruction^, intermediate_instruction);
next_instruction^ := intermediate_instruction; next_instruction^ := intermediate_instruction;
binary_result := elna_rtl_instruction_create(instruction_kind);
elna_rtl_copy_operand(tac_instruction, 1, binary_result);
elna_rtl_instruction_set_operand(binary_result, 2, ElnaRtlOperand.register, ElnaRtlRegister.t2, 0); elna_rtl_instruction_set_operand(binary_result, 2, ElnaRtlOperand.register, ElnaRtlRegister.t2, 0);
ElnaInstructionList_set_next(next_instruction^, binary_result); ElnaInstructionList_set_next(next_instruction^, binary_result);
next_instruction^ := binary_result; next_instruction^ := binary_result;
@@ -975,7 +978,6 @@ begin
instruction_size := elna_rtl_instruction_size(); instruction_size := elna_rtl_instruction_size();
result := malloc(instruction_size); result := malloc(instruction_size);
instruction_kind := _elna_tac_instruction_get_kind(tac_instruction); instruction_kind := _elna_tac_instruction_get_kind(tac_instruction);
memcpy(result + 4, tac_instruction + 4, instruction_size - 4);
next_instruction^ := 0; next_instruction^ := 0;
if instruction_kind = ElnaTacOperator.get_address then if instruction_kind = ElnaTacOperator.get_address then
@@ -993,7 +995,7 @@ begin
elna_rtl_copy_operand(tac_instruction, 1, result); elna_rtl_copy_operand(tac_instruction, 1, result);
elna_rtl_copy_operand(tac_instruction, 2, result) elna_rtl_copy_operand(tac_instruction, 2, result)
elsif operand_type = ElnaTacOperand.register then elsif operand_type = ElnaTacOperand.temporary then
result := elna_rtl_instruction_create(ElnaRtlOperator.move); result := elna_rtl_instruction_create(ElnaRtlOperator.move);
elna_rtl_copy_operand(tac_instruction, 1, result); elna_rtl_copy_operand(tac_instruction, 1, result);
@@ -1005,6 +1007,8 @@ begin
operand_type := _elna_tac_instruction_get_operand_type(tac_instruction, 2); operand_type := _elna_tac_instruction_get_operand_type(tac_instruction, 2);
if operand_type = ElnaTacOperand.stack then if operand_type = ElnaTacOperand.stack then
result := elna_rtl_instruction_create(ElnaRtlOperator.load_word);
operand_value := _elna_tac_instruction_get_operand_value(tac_instruction, 1); operand_value := _elna_tac_instruction_get_operand_value(tac_instruction, 1);
next_instruction^ := elna_rtl_instruction_create(ElnaRtlOperator.load_word); next_instruction^ := elna_rtl_instruction_create(ElnaRtlOperator.load_word);
elna_rtl_instruction_set_operand(next_instruction^, 1, ElnaRtlOperand.register, operand_value, 0); elna_rtl_instruction_set_operand(next_instruction^, 1, ElnaRtlOperand.register, operand_value, 0);
@@ -1013,24 +1017,37 @@ begin
operand_value := _elna_tac_instruction_get_operand_value(tac_instruction, 2); operand_value := _elna_tac_instruction_get_operand_value(tac_instruction, 2);
elna_rtl_instruction_set_operand(result, 2, ElnaRtlOperand.offset, ElnaRtlRegister.sp, operand_value); elna_rtl_instruction_set_operand(result, 2, ElnaRtlOperand.offset, ElnaRtlRegister.sp, operand_value);
ElnaInstructionList_set_next(result, next_instruction^) ElnaInstructionList_set_next(result, next_instruction^)
end; elsif operand_type = ElnaTacOperand.temporary then
elna_rtl_instruction_set_kind(result, ElnaRtlOperator.load_word) result := elna_rtl_instruction_create(ElnaRtlOperator.load_word);
elna_rtl_copy_operand(tac_instruction, 1, result);
operand_value := _elna_tac_instruction_get_operand_value(tac_instruction, 2);
elna_rtl_instruction_set_operand(result, 2, ElnaRtlOperand.offset, operand_value, 0)
end
elsif instruction_kind = ElnaTacOperator.store_word then elsif instruction_kind = ElnaTacOperator.store_word then
operands := elna_rtl_load_operand_value(tac_instruction, 1, ElnaRtlRegister.t0); operands := elna_rtl_load_operand_value(tac_instruction, 1, ElnaRtlRegister.t0);
next_instruction^ := result; next_instruction^ := elna_rtl_instruction_create(ElnaRtlOperator.store_word);
result := operands; result := operands;
operands := ElnaInstructionList_get_next(result); operands := ElnaInstructionList_get_next(result);
elna_rtl_instruction_set_kind(next_instruction^, ElnaRtlOperator.store_word);
elna_rtl_instruction_set_operand(next_instruction^, 1, ElnaRtlOperand.register, ElnaRtlRegister.t0, 0); elna_rtl_instruction_set_operand(next_instruction^, 1, ElnaRtlOperand.register, ElnaRtlRegister.t0, 0);
operand_type := _elna_tac_instruction_get_operand_type(tac_instruction, 2);
if operand_type = ElnaTacOperand.stack then
operand_value := _elna_tac_instruction_get_operand_value(tac_instruction, 2);
elna_rtl_instruction_set_operand(next_instruction^, 2, ElnaRtlOperand.offset, ElnaRtlRegister.sp, operand_value)
elsif operand_type = ElnaTacOperand.temporary then
operand_value := _elna_tac_instruction_get_operand_value(tac_instruction, 2);
elna_rtl_instruction_set_operand(next_instruction^, 2, ElnaRtlOperand.offset, operand_value, 0)
end;
if operands = 0 then if operands = 0 then
ElnaInstructionList_set_next(result, next_instruction^) ElnaInstructionList_set_next(result, next_instruction^)
else else
ElnaInstructionList_set_next(operands, next_instruction^) ElnaInstructionList_set_next(operands, next_instruction^)
end end
elsif instruction_kind = ElnaTacOperator.jal then elsif instruction_kind = ElnaTacOperator.jal then
elna_rtl_instruction_set_kind(result, ElnaRtlOperator.jal) result := elna_rtl_instruction_create(ElnaRtlOperator.jal);
elna_rtl_copy_operand(tac_instruction, 1, result)
elsif instruction_kind = ElnaTacOperator.sub then elsif instruction_kind = ElnaTacOperator.sub then
result := elna_rtl_binary_arithmetic(tac_instruction, next_instruction, ElnaRtlOperator.sub) result := elna_rtl_binary_arithmetic(tac_instruction, next_instruction, ElnaRtlOperator.sub)
elsif instruction_kind = ElnaTacOperator.mul then elsif instruction_kind = ElnaTacOperator.mul then
@@ -1049,6 +1066,9 @@ begin
result := elna_rtl_binary_arithmetic(tac_instruction, next_instruction, ElnaRtlOperator.slt) result := elna_rtl_binary_arithmetic(tac_instruction, next_instruction, ElnaRtlOperator.slt)
elsif instruction_kind = ElnaTacOperator.sgt then elsif instruction_kind = ElnaTacOperator.sgt then
operands := elna_rtl_binary_operands(tac_instruction, next_instruction); operands := elna_rtl_binary_operands(tac_instruction, next_instruction);
result := elna_rtl_instruction_create(ElnaRtlOperator.slt);
elna_rtl_copy_operand(tac_instruction, 1, result);
elna_rtl_instruction_set_operand(result, 2, ElnaRtlOperand.register, ElnaRtlRegister.t3, 0); elna_rtl_instruction_set_operand(result, 2, ElnaRtlOperand.register, ElnaRtlRegister.t3, 0);
elna_rtl_instruction_set_operand(result, 3, ElnaRtlOperand.register, ElnaRtlRegister.t2, 0); elna_rtl_instruction_set_operand(result, 3, ElnaRtlOperand.register, ElnaRtlRegister.t2, 0);
@@ -1067,6 +1087,8 @@ begin
ElnaInstructionList_set_next(next_instruction^, intermediate_instruction); ElnaInstructionList_set_next(next_instruction^, intermediate_instruction);
next_instruction^ := intermediate_instruction; next_instruction^ := intermediate_instruction;
result := elna_rtl_instruction_create(ElnaRtlOperator.xor_immediate);
elna_rtl_copy_operand(tac_instruction, 1, result);
elna_rtl_instruction_set_operand(result, 2, ElnaRtlOperand.register, ElnaRtlRegister.t2, 0); elna_rtl_instruction_set_operand(result, 2, ElnaRtlOperand.register, ElnaRtlRegister.t2, 0);
elna_rtl_instruction_set_operand(result, 3, ElnaRtlOperand.immediate, 1, 0); elna_rtl_instruction_set_operand(result, 3, ElnaRtlOperand.immediate, 1, 0);
ElnaInstructionList_set_next(next_instruction^, result); ElnaInstructionList_set_next(next_instruction^, result);
@@ -1084,6 +1106,8 @@ begin
ElnaInstructionList_set_next(next_instruction^, intermediate_instruction); ElnaInstructionList_set_next(next_instruction^, intermediate_instruction);
next_instruction^ := intermediate_instruction; next_instruction^ := intermediate_instruction;
result := elna_rtl_instruction_create(ElnaRtlOperator.xor_immediate);
elna_rtl_copy_operand(tac_instruction, 1, result);
elna_rtl_instruction_set_operand(result, 2, ElnaRtlOperand.register, ElnaRtlRegister.t2, 0); elna_rtl_instruction_set_operand(result, 2, ElnaRtlOperand.register, ElnaRtlRegister.t2, 0);
elna_rtl_instruction_set_operand(result, 3, ElnaRtlOperand.immediate, 1, 0); elna_rtl_instruction_set_operand(result, 3, ElnaRtlOperand.immediate, 1, 0);
ElnaInstructionList_set_next(next_instruction^, result); ElnaInstructionList_set_next(next_instruction^, result);
@@ -1092,31 +1116,36 @@ begin
result := operands; result := operands;
elna_rtl_instruction_set_kind(next_instruction^, ElnaRtlOperator.xor_immediate) elna_rtl_instruction_set_kind(next_instruction^, ElnaRtlOperator.xor_immediate)
elsif instruction_kind = ElnaTacOperator.seq then elsif instruction_kind = ElnaTacOperator.seq then
result := elna_rtl_binary_equality(tac_instruction, result, next_instruction); result := elna_rtl_binary_equality(tac_instruction, ElnaRtlOperator.seqz, next_instruction)
elna_rtl_instruction_set_kind(next_instruction^, ElnaRtlOperator.seqz)
elsif instruction_kind = ElnaTacOperator.sne then elsif instruction_kind = ElnaTacOperator.sne then
result := elna_rtl_binary_equality(tac_instruction, result, next_instruction); result := elna_rtl_binary_equality(tac_instruction, ElnaRtlOperator.snez, next_instruction)
elna_rtl_instruction_set_kind(next_instruction^, ElnaRtlOperator.snez)
elsif instruction_kind = ElnaTacOperator.neg then elsif instruction_kind = ElnaTacOperator.neg then
elna_rtl_instruction_set_kind(result, ElnaRtlOperator.neg) result := elna_rtl_instruction_create(ElnaRtlOperator.neg);
elna_rtl_copy_operand(tac_instruction, 1, result);
elna_rtl_copy_operand(tac_instruction, 2, result)
elsif instruction_kind = ElnaTacOperator.not then elsif instruction_kind = ElnaTacOperator.not then
elna_rtl_instruction_set_kind(result, ElnaRtlOperator.not) result := elna_rtl_instruction_create(ElnaRtlOperator.not);
elna_rtl_copy_operand(tac_instruction, 1, result);
elna_rtl_copy_operand(tac_instruction, 2, result)
elsif instruction_kind = ElnaTacOperator.jump then elsif instruction_kind = ElnaTacOperator.jump then
elna_rtl_instruction_set_kind(result, ElnaRtlOperator.jump) result := elna_rtl_instruction_create(ElnaRtlOperator.jump);
elna_rtl_copy_operand(tac_instruction, 1, result)
elsif instruction_kind = ElnaTacOperator.beqz then elsif instruction_kind = ElnaTacOperator.beqz then
elna_rtl_instruction_set_kind(result, ElnaRtlOperator.beqz) result := elna_rtl_instruction_create(ElnaRtlOperator.beqz);
elna_rtl_copy_operand(tac_instruction, 1, result);
elna_rtl_copy_operand(tac_instruction, 2, result)
elsif instruction_kind = ElnaTacOperator.start then elsif instruction_kind = ElnaTacOperator.start then
elna_rtl_instruction_set_kind(result, ElnaRtlOperator.start) result := elna_rtl_instruction_create(ElnaRtlOperator.start)
elsif instruction_kind = ElnaTacOperator.ret then elsif instruction_kind = ElnaTacOperator.ret then
elna_rtl_instruction_set_kind(result, ElnaRtlOperator.ret) result := elna_rtl_instruction_create(ElnaRtlOperator.ret)
elsif instruction_kind = ElnaTacOperator.label then elsif instruction_kind = ElnaTacOperator.label then
elna_rtl_instruction_set_kind(result, ElnaRtlOperator.label) result := elna_rtl_instruction_create(ElnaRtlOperator.label);
elna_rtl_copy_operand(tac_instruction, 1, result)
elsif instruction_kind = ElnaTacOperator.assign then elsif instruction_kind = ElnaTacOperator.assign then
free(result);
operand_type := _elna_tac_instruction_get_operand_type(tac_instruction, 1); operand_type := _elna_tac_instruction_get_operand_type(tac_instruction, 1);
operand_value := _elna_tac_instruction_get_operand_value(tac_instruction, 1); operand_value := _elna_tac_instruction_get_operand_value(tac_instruction, 1);
if operand_type = ElnaTacOperand.register then if operand_type = ElnaTacOperand.temporary then
result := elna_rtl_load_operand_value(tac_instruction, 2, operand_value); result := elna_rtl_load_operand_value(tac_instruction, 2, operand_value);
next_instruction^ := ElnaInstructionList_get_next(result) next_instruction^ := ElnaInstructionList_get_next(result)
elsif operand_type = ElnaTacOperand.stack then elsif operand_type = ElnaTacOperand.stack then
@@ -1133,6 +1162,10 @@ begin
else else
ElnaInstructionList_set_next(operands, next_instruction^) ElnaInstructionList_set_next(operands, next_instruction^)
end end
(* Debug. Error stream output.
_syscall(2, name_pointer, name_length, 0, 0, 0, 64);
printf("# here\n\0");
fflush(0); *)
end end
end; end;
if next_instruction^ = 0 then if next_instruction^ = 0 then
@@ -1153,41 +1186,6 @@ begin
return result return result
end; end;
proc _elna_tac_jump(source_symbol: Word, symbol_length: Word);
var
result: Word;
begin
result := _elna_tac_instruction_create(ElnaTacOperator.jump);
_elna_tac_instruction_set_operand(result, 1, ElnaTacOperand.symbol, source_symbol, symbol_length);
return result
end;
proc _elna_tac_load_word(target: Word, register: Word, offset: Word);
var
result: Word;
begin
result := _elna_tac_instruction_create(ElnaTacOperator.load_word);
_elna_tac_instruction_set_operand(result, 1, ElnaTacOperand.register, target, 0);
_elna_tac_instruction_set_operand(result, 2, ElnaTacOperand.offset, register, offset);
return result
end;
proc _elna_tac_store_word(target: Word, register: Word, offset: Word);
var
result: Word;
begin
result := _elna_tac_instruction_create(ElnaTacOperator.store_word);
_elna_tac_instruction_set_operand(result, 1, ElnaTacOperand.register, target, 0);
_elna_tac_instruction_set_operand(result, 2, ElnaTacOperand.offset, register, offset);
return result
end;
proc _elna_tac_label(counter: Word, length: Word); proc _elna_tac_label(counter: Word, length: Word);
var var
result: Word; result: Word;
@@ -1634,17 +1632,17 @@ begin
offset := _add_string(token_start); offset := _add_string(token_start);
first_instruction := _elna_tac_instruction_create(ElnaTacOperator.get_address); first_instruction := _elna_tac_instruction_create(ElnaTacOperator.get_address);
_elna_tac_instruction_set_operand(first_instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(first_instruction, 1, ElnaTacOperand.temporary, 6, 0);
_elna_tac_instruction_set_operand(first_instruction, 2, ElnaTacOperand.symbol, "strings", 7); _elna_tac_instruction_set_operand(first_instruction, 2, ElnaTacOperand.symbol, "strings", 7);
(* Add offset to the string block pointer. *) (* Add offset to the string block pointer. *)
next_instruction := _elna_tac_instruction_create(ElnaTacOperator.add); next_instruction := _elna_tac_instruction_create(ElnaTacOperator.add);
_elna_tac_instruction_set_operand(next_instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(next_instruction, 1, ElnaTacOperand.temporary, 6, 0);
_elna_tac_instruction_set_operand(next_instruction, 2, ElnaTacOperand.register, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(next_instruction, 2, ElnaTacOperand.temporary, 6, 0);
_elna_tac_instruction_set_operand(next_instruction, 3, ElnaTacOperand.immediate, offset, 0); _elna_tac_instruction_set_operand(next_instruction, 3, ElnaTacOperand.immediate, offset, 0);
operand_type^ := ElnaTacOperand.register; operand_type^ := ElnaTacOperand.temporary;
operand_value^ := ElnaRtlRegister.t0; operand_value^ := 6;
operand_length^ := 0; operand_length^ := 0;
return elna_instruction_list_concatenate(first_instruction, next_instruction) return elna_instruction_list_concatenate(first_instruction, next_instruction)
@@ -1703,12 +1701,11 @@ begin
return simple_expression return simple_expression
end; end;
proc _elna_tac_simple_expression(parser_node: Word, symbol_table: Word, is_address: Word, operand_type: Word, operand_value: Word, operand_length: Word); proc _elna_tac_simple_expression(parser_node: Word, symbol_table: Word, operand_type: Word, operand_value: Word, operand_length: Word);
var var
node_kind: Word; node_kind: Word;
instruction: Word; instruction: Word;
begin begin
is_address^ := 0;
node_kind := _node_get_kind(parser_node); node_kind := _node_get_kind(parser_node);
if node_kind = NodeKind.character_literal then if node_kind = NodeKind.character_literal then
@@ -1718,8 +1715,7 @@ begin
elsif node_kind = NodeKind.integer_literal then elsif node_kind = NodeKind.integer_literal then
instruction := _elna_tac_integer_literal(parser_node, operand_type, operand_value, operand_length) instruction := _elna_tac_integer_literal(parser_node, operand_type, operand_value, operand_length)
else else
instruction := _elna_tac_variable_expression(parser_node, symbol_table, operand_type, operand_value, operand_length); instruction := _elna_tac_variable_expression(parser_node, symbol_table, operand_type, operand_value, operand_length)
is_address^ := 1
end; end;
return instruction return instruction
end; end;
@@ -1786,11 +1782,11 @@ begin
first_instruction := _elna_tac_designator(operand, symbol_table, @is_address, operand_type, operand_value, operand_length); first_instruction := _elna_tac_designator(operand, symbol_table, @is_address, operand_type, operand_value, operand_length);
instruction := _elna_tac_instruction_create(ElnaTacOperator.get_address); instruction := _elna_tac_instruction_create(ElnaTacOperator.get_address);
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.temporary, 6, 0);
_elna_tac_instruction_set_operand(instruction, 2, operand_type^, operand_value^, operand_length^); _elna_tac_instruction_set_operand(instruction, 2, operand_type^, operand_value^, operand_length^);
operand_type^ := ElnaTacOperand.register; operand_type^ := ElnaTacOperand.temporary;
operand_value^ := ElnaRtlRegister.t0; operand_value^ := 6;
operand_length^ := 0; operand_length^ := 0;
first_instruction := elna_instruction_list_concatenate(first_instruction, instruction) first_instruction := elna_instruction_list_concatenate(first_instruction, instruction)
@@ -1799,36 +1795,37 @@ begin
first_instruction := _elna_tac_designator(operand, symbol_table, @is_address, operand_type, operand_value, operand_length); first_instruction := _elna_tac_designator(operand, symbol_table, @is_address, operand_type, operand_value, operand_length);
operand := operand_type^; operand := operand_type^;
if first_instruction = 0 then
first_instruction := _elna_tac_instruction_create(ElnaTacOperator.get_address);
_elna_tac_instruction_set_operand(first_instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.t0, 0);
_elna_tac_instruction_set_operand(first_instruction, 2, operand_type^, operand_value^, operand_length^);
operand_type^ := ElnaTacOperand.register;
operand_value^ := ElnaRtlRegister.t0;
operand_length^ := 0
end;
if is_address then if is_address then
instruction := _elna_tac_instruction_create(ElnaTacOperator.load_word); instruction := _elna_tac_instruction_create(ElnaTacOperator.load_word);
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.temporary, 6, 0);
_elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.offset, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(instruction, 2, operand_type^, operand_value^, operand_length^);
operand_type^ := ElnaTacOperand.register; operand_type^ := ElnaTacOperand.temporary;
operand_value^ := ElnaRtlRegister.t0; operand_value^ := 6;
operand_length^ := 0; operand_length^ := 0;
elna_instruction_list_concatenate(first_instruction, instruction) elna_instruction_list_concatenate(first_instruction, instruction)
else
instruction := _elna_tac_instruction_create(ElnaTacOperator.assign);
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.temporary, 6, 0);
_elna_tac_instruction_set_operand(instruction, 2, operand_type^, operand_value^, operand_length^);
operand_type^ := ElnaTacOperand.temporary;
operand_value^ := 6;
operand_length^ := 0;
first_instruction := elna_instruction_list_concatenate(first_instruction, instruction)
end end
end; end;
if operator = '-' then if operator = '-' then
instruction := _elna_tac_instruction_create(ElnaTacOperator.neg); instruction := _elna_tac_instruction_create(ElnaTacOperator.neg);
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.temporary, 6, 0);
_elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.register, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.temporary, 6, 0);
elna_instruction_list_concatenate(first_instruction, instruction) elna_instruction_list_concatenate(first_instruction, instruction)
elsif operator = '~' then elsif operator = '~' then
instruction := _elna_tac_instruction_create(ElnaTacOperator.not); instruction := _elna_tac_instruction_create(ElnaTacOperator.not);
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.temporary, 6, 0);
_elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.register, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.temporary, 6, 0);
elna_instruction_list_concatenate(first_instruction, instruction) elna_instruction_list_concatenate(first_instruction, instruction)
end; end;
return first_instruction return first_instruction
@@ -1944,20 +1941,11 @@ begin
lhs_length := 0; lhs_length := 0;
instruction := _elna_tac_unary_expression(operand_node, symbol_table, @lhs_type, @lhs_value, @lhs_length); instruction := _elna_tac_unary_expression(operand_node, symbol_table, @lhs_type, @lhs_value, @lhs_length);
(* Debug. Error stream output.
_syscall(2, name_pointer, name_length, 0, 0, 0, 64);
if lhs_length = 0 then
printf("# here %i\n\0", lhs_value);
else
printf("# here %.*s\n\0", lhs_length, lhs_value);
end;
fflush(0); *)
current_instruction := elna_instruction_list_concatenate(current_instruction, instruction); current_instruction := elna_instruction_list_concatenate(current_instruction, instruction);
(* Load the left expression from the stack; *) (* Load the left expression from the stack; *)
instruction := _elna_tac_instruction_create(ElnaTacOperator.assign); instruction := _elna_tac_instruction_create(ElnaTacOperator.assign);
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.t1, 0); _elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.temporary, 7, 0);
_elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.stack, 72, 0); _elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.stack, 72, 0);
elna_instruction_list_concatenate(current_instruction, instruction); elna_instruction_list_concatenate(current_instruction, instruction);
@@ -1992,14 +1980,14 @@ begin
elsif token_kind = ElnaLexerKind.not_equal then elsif token_kind = ElnaLexerKind.not_equal then
instruction := _elna_tac_instruction_create(ElnaTacOperator.sne) instruction := _elna_tac_instruction_create(ElnaTacOperator.sne)
end; end;
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.temporary, 6, 0);
_elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.stack, 72, 0); _elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.stack, 72, 0);
_elna_tac_instruction_set_operand(instruction, 3, lhs_type, lhs_value, lhs_length); _elna_tac_instruction_set_operand(instruction, 3, lhs_type, lhs_value, lhs_length);
elna_instruction_list_concatenate(current_instruction, instruction); elna_instruction_list_concatenate(current_instruction, instruction);
operand_type^ := ElnaTacOperand.register; operand_type^ := ElnaTacOperand.temporary;
operand_value^ := ElnaRtlRegister.t0; operand_value^ := 6;
operand_length^ := 0 operand_length^ := 0
end; end;
return first_instruction return first_instruction
@@ -2123,7 +2111,7 @@ begin
instruction := _elna_tac_instruction_create(ElnaTacOperator.store_word); instruction := _elna_tac_instruction_create(ElnaTacOperator.store_word);
_elna_tac_instruction_set_operand(instruction, 1, operand_type, operand_value, operand_length); _elna_tac_instruction_set_operand(instruction, 1, operand_type, operand_value, operand_length);
_elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.offset, ElnaRtlRegister.sp, 132 - stack_offset); _elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.stack, 132 - stack_offset, 0);
if first_instruction = 0 then if first_instruction = 0 then
first_instruction := instruction first_instruction := instruction
else else
@@ -2143,8 +2131,9 @@ begin
stack_offset := argument_count * 4; stack_offset := argument_count * 4;
(* Calculate the stack offset: 132 - (4 * argument_counter) *) (* Calculate the stack offset: 132 - (4 * argument_counter) *)
instruction := _elna_tac_load_word(ElnaRtlRegister.a0 + argument_count, instruction := _elna_tac_instruction_create(ElnaTacOperator.assign);
ElnaRtlRegister.sp, 132 - stack_offset); _elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.temporary, 11 + argument_count, 0);
_elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.stack, 132 - stack_offset, 0);
elna_instruction_list_concatenate(current_instruction, instruction); elna_instruction_list_concatenate(current_instruction, instruction);
current_instruction := instruction; current_instruction := instruction;
@@ -2198,7 +2187,9 @@ begin
_store_byte('.', label_with_dot); _store_byte('.', label_with_dot);
memcpy(label_with_dot + 1, label_name, label_length); memcpy(label_with_dot + 1, label_name, label_length);
return _elna_tac_jump(label_with_dot, label_length + 1) instruction := _elna_tac_instruction_create(ElnaTacOperator.jump);
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.symbol, label_with_dot, label_length + 1);
return instruction
end; end;
proc _elna_parser_label_declaration(); proc _elna_parser_label_declaration();
@@ -2322,16 +2313,17 @@ begin
if node_kind = NodeKind.dereference_expression then if node_kind = NodeKind.dereference_expression then
parser_node := _dereference_expression_get_pointer(parser_node); parser_node := _dereference_expression_get_pointer(parser_node);
first_instruction := _elna_tac_simple_expression(parser_node, symbol_table, is_address, operand_type, operand_value, operand_length); first_instruction := _elna_tac_simple_expression(parser_node, symbol_table, operand_type, operand_value, operand_length);
last_instruction := _elna_tac_instruction_create(ElnaTacOperator.assign); last_instruction := _elna_tac_instruction_create(ElnaTacOperator.assign);
_elna_tac_instruction_set_operand(last_instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(last_instruction, 1, ElnaTacOperand.temporary, 6, 0);
_elna_tac_instruction_set_operand(last_instruction, 2, operand_type^, operand_value^, operand_length^); _elna_tac_instruction_set_operand(last_instruction, 2, operand_type^, operand_value^, operand_length^);
operand_type^ := ElnaTacOperand.register; operand_type^ := ElnaTacOperand.temporary;
operand_value^ := ElnaRtlRegister.t0; operand_value^ := 6;
operand_length^ := 0; operand_length^ := 0;
is_address^ := 2;
first_instruction := elna_instruction_list_concatenate(first_instruction, last_instruction) first_instruction := elna_instruction_list_concatenate(first_instruction, last_instruction)
elsif node_kind = NodeKind.field_access_expression then elsif node_kind = NodeKind.field_access_expression then
first_instruction := _elna_tac_enumeration_value(parser_node, operand_type, operand_value, operand_length); first_instruction := _elna_tac_enumeration_value(parser_node, operand_type, operand_value, operand_length);
@@ -2340,17 +2332,18 @@ begin
first_instruction := _elna_tac_call(parser_node, symbol_table); first_instruction := _elna_tac_call(parser_node, symbol_table);
last_instruction := _elna_tac_instruction_create(ElnaTacOperator.assign); last_instruction := _elna_tac_instruction_create(ElnaTacOperator.assign);
_elna_tac_instruction_set_operand(last_instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(last_instruction, 1, ElnaTacOperand.temporary, 6, 0);
_elna_tac_instruction_set_operand(last_instruction, 2, ElnaTacOperand.register, ElnaRtlRegister.a0, 0); _elna_tac_instruction_set_operand(last_instruction, 2, ElnaTacOperand.temporary, 11, 0);
operand_type^ := ElnaTacOperand.register; operand_type^ := ElnaTacOperand.temporary;
operand_value^ := ElnaRtlRegister.t0; operand_value^ := 6;
operand_length^ := 0; operand_length^ := 0;
elna_instruction_list_concatenate(first_instruction, last_instruction); elna_instruction_list_concatenate(first_instruction, last_instruction);
is_address^ := 0 is_address^ := 0
else else
first_instruction := _elna_tac_simple_expression(parser_node, symbol_table, is_address, operand_type, operand_value, operand_length) first_instruction := _elna_tac_simple_expression(parser_node, symbol_table, operand_type, operand_value, operand_length);
is_address^ := 0
end; end;
return first_instruction return first_instruction
end; end;
@@ -2395,12 +2388,14 @@ begin
first_instruction := _elna_tac_designator(current_expression, symbol_table, @is_address, @operand_type, @operand_value, @operand_length); first_instruction := _elna_tac_designator(current_expression, symbol_table, @is_address, @operand_type, @operand_value, @operand_length);
current_instruction := _elna_tac_instruction_create(ElnaTacOperator.get_address); current_instruction := _elna_tac_instruction_create(ElnaTacOperator.get_address);
_elna_tac_instruction_set_operand(current_instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.t0, 0); _elna_tac_instruction_set_operand(current_instruction, 1, ElnaTacOperand.temporary, 6, 0);
_elna_tac_instruction_set_operand(current_instruction, 2, operand_type, operand_value, operand_length); _elna_tac_instruction_set_operand(current_instruction, 2, operand_type, operand_value, operand_length);
first_instruction := elna_instruction_list_concatenate(first_instruction, current_instruction); first_instruction := elna_instruction_list_concatenate(first_instruction, current_instruction);
(* Save the assignee address on the stack. *) (* Save the assignee address on the stack. *)
current_instruction := _elna_tac_store_word(ElnaRtlRegister.t0, ElnaRtlRegister.sp, 76); current_instruction := _elna_tac_instruction_create(ElnaTacOperator.store_word);
_elna_tac_instruction_set_operand(current_instruction, 1, ElnaTacOperand.temporary, 6, 0);
_elna_tac_instruction_set_operand(current_instruction, 2, ElnaTacOperand.stack, 76, 0);
elna_instruction_list_concatenate(first_instruction, current_instruction); elna_instruction_list_concatenate(first_instruction, current_instruction);
(* Compile the assignment. *) (* Compile the assignment. *)
@@ -2415,13 +2410,16 @@ begin
current_instruction := instruction current_instruction := instruction
end; end;
instruction := _elna_tac_load_word(ElnaRtlRegister.t1, ElnaRtlRegister.sp, 76); instruction := _elna_tac_instruction_create(ElnaTacOperator.assign);
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.temporary, 7, 0);
_elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.stack, 76, 0);
elna_instruction_list_concatenate(current_instruction, instruction); elna_instruction_list_concatenate(current_instruction, instruction);
current_instruction := instruction; current_instruction := instruction;
instruction := _elna_tac_instruction_create(ElnaTacOperator.store_word); instruction := _elna_tac_instruction_create(ElnaTacOperator.store_word);
_elna_tac_instruction_set_operand(instruction, 1, operand_type, operand_value, operand_length); _elna_tac_instruction_set_operand(instruction, 1, operand_type, operand_value, operand_length);
_elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.offset, ElnaRtlRegister.t1, 0); _elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.temporary, 7, 0);
elna_instruction_list_concatenate(current_instruction, instruction); elna_instruction_list_concatenate(current_instruction, instruction);
@@ -2465,7 +2463,7 @@ begin
first_instruction := _elna_tac_binary_expression(return_expression, symbol_table, @operand_type, @operand_value, @operand_length); first_instruction := _elna_tac_binary_expression(return_expression, symbol_table, @operand_type, @operand_value, @operand_length);
instruction := _elna_tac_instruction_create(ElnaTacOperator.assign); instruction := _elna_tac_instruction_create(ElnaTacOperator.assign);
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.a0, 0); _elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.temporary, 11, 0);
_elna_tac_instruction_set_operand(instruction, 2, operand_type, operand_value, operand_length); _elna_tac_instruction_set_operand(instruction, 2, operand_type, operand_value, operand_length);
return elna_instruction_list_concatenate(first_instruction, instruction) return elna_instruction_list_concatenate(first_instruction, instruction)
@@ -2553,7 +2551,8 @@ begin
current_instruction := instruction current_instruction := instruction
end; end;
instruction := _elna_tac_jump(after_end_label, 0); instruction := _elna_tac_instruction_create(ElnaTacOperator.jump);
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.symbol, after_end_label, 0);
elna_instruction_list_concatenate(current_instruction, instruction); elna_instruction_list_concatenate(current_instruction, instruction);
current_instruction := _elna_tac_label(condition_label, 0); current_instruction := _elna_tac_label(condition_label, 0);
@@ -3267,7 +3266,9 @@ begin
symbol_info := _parameter_info_get_offset(symbol_info); symbol_info := _parameter_info_get_offset(symbol_info);
instruction := _elna_tac_store_word(ElnaRtlRegister.a0 + parameter_counter, ElnaRtlRegister.sp, symbol_info); instruction := _elna_tac_instruction_create(ElnaTacOperator.store_word);
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.temporary, 11 + parameter_counter, 0);
_elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.stack, symbol_info, 0);
if first_instruction = 0 then if first_instruction = 0 then
first_instruction := instruction first_instruction := instruction
else else
@@ -3677,8 +3678,8 @@ begin
instruction := _elna_tac_instruction_create(ElnaTacOperator.add); instruction := _elna_tac_instruction_create(ElnaTacOperator.add);
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.a0, 0); _elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.temporary, 11, 0);
_elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.register, ElnaRtlRegister.a0, 0); _elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.temporary, 11, 0);
_elna_tac_instruction_set_operand(instruction, 3, ElnaTacOperand.immediate, field_offset, 0); _elna_tac_instruction_set_operand(instruction, 3, ElnaTacOperand.immediate, field_offset, 0);
return instruction return instruction
@@ -3693,15 +3694,15 @@ var
begin begin
instruction := _elna_tac_accessor(name_pointer, name_length, field_pointer, field_offset, @first_result, "_get_"); instruction := _elna_tac_accessor(name_pointer, name_length, field_pointer, field_offset, @first_result, "_get_");
next_instruction := _elna_tac_instruction_create(ElnaTacOperator.load_word); next_instruction := _elna_tac_instruction_create(ElnaTacOperator.load_word);
_elna_tac_instruction_set_operand(next_instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.a0, 0); _elna_tac_instruction_set_operand(next_instruction, 1, ElnaTacOperand.temporary, 11, 0);
_elna_tac_instruction_set_operand(next_instruction, 2, ElnaTacOperand.offset, ElnaRtlRegister.a0, 0); _elna_tac_instruction_set_operand(next_instruction, 2, ElnaTacOperand.temporary, 11, 0);
elna_instruction_list_concatenate(instruction, next_instruction); elna_instruction_list_concatenate(instruction, next_instruction);
ElnaInstructionDeclaration_set_body(first_result, instruction); ElnaInstructionDeclaration_set_body(first_result, instruction);
instruction := _elna_tac_accessor(name_pointer, name_length, field_pointer, field_offset, @second_result, "_set_"); instruction := _elna_tac_accessor(name_pointer, name_length, field_pointer, field_offset, @second_result, "_set_");
next_instruction := _elna_tac_instruction_create(ElnaTacOperator.store_word); next_instruction := _elna_tac_instruction_create(ElnaTacOperator.store_word);
_elna_tac_instruction_set_operand(next_instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.a1, 0); _elna_tac_instruction_set_operand(next_instruction, 1, ElnaTacOperand.temporary, 12, 0);
_elna_tac_instruction_set_operand(next_instruction, 2, ElnaTacOperand.offset, ElnaRtlRegister.a0, 0); _elna_tac_instruction_set_operand(next_instruction, 2, ElnaTacOperand.temporary, 11, 0);
elna_instruction_list_concatenate(instruction, next_instruction); elna_instruction_list_concatenate(instruction, next_instruction);
ElnaInstructionDeclaration_set_body(second_result, instruction); ElnaInstructionDeclaration_set_body(second_result, instruction);
@@ -3736,7 +3737,7 @@ begin
ElnaInstructionDeclaration_set_length(first_result, new_length); ElnaInstructionDeclaration_set_length(first_result, new_length);
instruction := _elna_tac_instruction_create(ElnaTacOperator.assign); instruction := _elna_tac_instruction_create(ElnaTacOperator.assign);
_elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.register, ElnaRtlRegister.a0, 0); _elna_tac_instruction_set_operand(instruction, 1, ElnaTacOperand.temporary, 11, 0);
_elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.immediate, type_size, 0); _elna_tac_instruction_set_operand(instruction, 2, ElnaTacOperand.immediate, type_size, 0);
ElnaInstructionDeclaration_set_body(first_result, instruction); ElnaInstructionDeclaration_set_body(first_result, instruction);