Implement add_ptr instruction

This commit is contained in:
2026-04-04 22:35:42 +02:00
parent 8514fb4fa0
commit 7c37f7f1c7
2 changed files with 74 additions and 19 deletions

View File

@@ -49,13 +49,13 @@ type
ElnaTypePointer = record ElnaTypePointer = record
kind: ElnaTypeKind; kind: ElnaTypeKind;
size: Word; size: Word;
base: Word base: ^ElnaType
end; end;
ElnaTypeArray = record ElnaTypeArray = record
kind: ElnaTypeKind; kind: ElnaTypeKind;
size: Word; size: Word;
base: Word; base: Word;
length: Word length: ^ElnaType
end; end;
(* Abstract syntax tree nodes. *) (* Abstract syntax tree nodes. *)
@@ -135,8 +135,8 @@ type
ElnaTreeBinaryExpression = record ElnaTreeBinaryExpression = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
type_decoration: ^ElnaType; type_decoration: ^ElnaType;
lhs: Word; lhs: ^ElnaTreeExpression;
rhs: Word; rhs: ^ElnaTreeExpression;
operator: Word operator: Word
end; end;
ElnaTreeUnaryExpression = record ElnaTreeUnaryExpression = record
@@ -2232,9 +2232,30 @@ var
rhs_type: Word; rhs_type: Word;
rhs_value: Word; rhs_value: Word;
rhs_length: Word; rhs_length: Word;
pointer_type: ^ElnaTypePointer;
begin begin
if parser_node^.kind <> ElnaTreeKind.binary_expression then if parser_node^.kind <> ElnaTreeKind.binary_expression then
first_instruction := elna_tac_unary_expression(parser_node, symbol_table, operand_type, operand_value, operand_length) first_instruction := elna_tac_unary_expression(parser_node, symbol_table, operand_type, operand_value, operand_length)
elsif parser_node^.operator = ElnaLexerKind.pipe then
first_instruction := elna_tac_unary_expression(parser_node^.lhs, symbol_table, @lhs_type, @lhs_value, @lhs_length);
instruction := elna_tac_unary_expression(parser_node^.rhs, symbol_table, @rhs_type, @rhs_value, @rhs_length);
first_instruction := elna_instruction_list_concatenate(first_instruction, instruction);
elna_tac_generate_pseudo(operand_type, operand_value, operand_length, symbol_table);
pointer_type := parser_node^.lhs^.type_decoration;
instruction := elna_tac_instruction_create(ElnaTacOperator.multiply);
elna_tac_instruction_set_operand(instruction, 1, operand_type^, operand_value^, operand_length^);
elna_tac_instruction_set_operand(instruction, 2, rhs_type, rhs_value, rhs_length);
elna_tac_instruction_set_operand(instruction, 3, ElnaTacKind.immediate, pointer_type^.base^.size, 0);
first_instruction := elna_instruction_list_concatenate(first_instruction, instruction);
instruction := elna_tac_instruction_create(ElnaTacOperator.add);
elna_tac_instruction_set_operand(instruction, 1, operand_type^, operand_value^, operand_length^);
elna_tac_instruction_set_operand(instruction, 2, operand_type^, operand_value^, operand_length^);
elna_tac_instruction_set_operand(instruction, 3, lhs_type, lhs_value, lhs_length);
first_instruction := elna_instruction_list_concatenate(first_instruction, instruction)
else else
first_instruction := elna_tac_unary_expression(parser_node^.lhs, symbol_table, @lhs_type, @lhs_value, @lhs_length); first_instruction := elna_tac_unary_expression(parser_node^.lhs, symbol_table, @lhs_type, @lhs_value, @lhs_length);
@@ -2245,8 +2266,6 @@ begin
instruction := elna_tac_instruction_create(ElnaTacOperator.add) instruction := elna_tac_instruction_create(ElnaTacOperator.add)
elsif parser_node^.operator = ElnaLexerKind.minus then elsif parser_node^.operator = ElnaLexerKind.minus then
instruction := elna_tac_instruction_create(ElnaTacOperator.subtract) instruction := elna_tac_instruction_create(ElnaTacOperator.subtract)
elsif parser_node^.operator = ElnaLexerKind.pipe then
instruction := elna_tac_instruction_create(ElnaTacOperator.add)
elsif parser_node^.operator = ElnaLexerKind.multiplication then elsif parser_node^.operator = ElnaLexerKind.multiplication then
instruction := elna_tac_instruction_create(ElnaTacOperator.multiply) instruction := elna_tac_instruction_create(ElnaTacOperator.multiply)
elsif parser_node^.operator = ElnaLexerKind.and then elsif parser_node^.operator = ElnaLexerKind.and then

View File

@@ -64,13 +64,13 @@ type
kind: ElnaTypeKind; kind: ElnaTypeKind;
size: Word; size: Word;
alignment: Word; alignment: Word;
base: Word base: ^ElnaType
end; end;
ElnaTypeArray = record ElnaTypeArray = record
kind: ElnaTypeKind; kind: ElnaTypeKind;
size: Word; size: Word;
alignment: Word; alignment: Word;
base: Word; base: ^ElnaType;
length: Word length: Word
end; end;
@@ -151,8 +151,8 @@ type
ElnaTreeBinaryExpression = record ElnaTreeBinaryExpression = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
type_decoration: ^ElnaType; type_decoration: ^ElnaType;
lhs: Word; lhs: ^ElnaTreeExpression;
rhs: Word; rhs: ^ElnaTreeExpression;
operator: Word operator: Word
end; end;
ElnaTreeUnaryExpression = record ElnaTreeUnaryExpression = record
@@ -515,7 +515,7 @@ type
ElnaTacInstruction = record ElnaTacInstruction = record
next: Word; next: Word;
operator: ElnaTacOperator; operator: ElnaTacOperator;
operands: [3]ElnaTacOperand operands: [4]ElnaTacOperand
end; end;
ElnaTacProcedure = record ElnaTacProcedure = record
next: Word; next: Word;
@@ -988,6 +988,37 @@ begin
return result return result
end; end;
proc elna_rtl_add_ptr(instructions: ^ElnaList, tac_instruction: ^ElnaTacInstruction, variable_map: ^ElnaSymbolTable);
var
result: ^ElnaRtlInstruction;
rtl_operand: ElnaRtlOperand;
begin
result := elna_rtl_instruction_create(ElnaRtlOperator.li);
elna_rtl_instruction_set_operand(result, 1, ElnaRtlKind.pseudo,
tac_instruction^.operands[4].value, tac_instruction^.operands[4].length, 0);
elna_rtl_instruction_set_operand(result, 2, ElnaRtlKind.immediate, tac_instruction^.operands[3].value, 0, 0);
elna_list_append(instructions, result);
result := elna_rtl_instruction_create(ElnaRtlOperator.mul);
elna_rtl_instruction_set_operand(result, 1, ElnaRtlKind.pseudo,
tac_instruction^.operands[4].value, tac_instruction^.operands[4].length, 0);
elna_rtl_instruction_set_operand(result, 2, ElnaRtlKind.pseudo,
tac_instruction^.operands[4].value, tac_instruction^.operands[4].length, 0);
elna_rtl_operand_value(instructions, @tac_instruction^.operands[2], variable_map, @rtl_operand);
elna_rtl_instruction_set_operand(result, 3, rtl_operand.kind, rtl_operand.value, rtl_operand.length, 0);
elna_list_append(instructions, result);
result := elna_rtl_instruction_create(ElnaRtlOperator.add);
elna_rtl_instruction_set_operand(result, 1, ElnaRtlKind.pseudo,
tac_instruction^.operands[4].value, tac_instruction^.operands[4].length, 0);
elna_rtl_operand_value(instructions, @tac_instruction^.operands[1], variable_map, @rtl_operand);
elna_rtl_instruction_set_operand(result, 2, rtl_operand.kind, rtl_operand.value, rtl_operand.length, 0);
elna_rtl_instruction_set_operand(result, 3, ElnaRtlKind.pseudo,
tac_instruction^.operands[4].value, tac_instruction^.operands[4].length, 0);
elna_list_append(instructions, result)
end;
proc elna_rtl_binary_equality(instructions: ^ElnaList, tac_instruction: ^ElnaTacInstruction, proc elna_rtl_binary_equality(instructions: ^ElnaList, tac_instruction: ^ElnaTacInstruction,
instruction_kind: ElnaRtlOperator, variable_map: ^ElnaSymbolTable); instruction_kind: ElnaRtlOperator, variable_map: ^ElnaSymbolTable);
var var
@@ -1124,7 +1155,7 @@ begin
argument_count := argument_count - 1; argument_count := argument_count - 1;
elna_rtl_load_operand_value(instructions, current_argument, ElnaRtlRegister.a0 + current_register, variable_map); elna_rtl_load_operand_value(instructions, current_argument, ElnaRtlRegister.a0 + current_register, variable_map);
current_argument := current_argument + #size(ElnaTacOperand); current_argument := current_argument | 1;
current_register := current_register + 1; current_register := current_register + 1;
goto elna_rtl_call_loop goto elna_rtl_call_loop
@@ -1251,6 +1282,8 @@ begin
elna_rtl_get_address(instructions, tac_instruction, variable_map) elna_rtl_get_address(instructions, tac_instruction, variable_map)
elsif tac_instruction^.operator = ElnaTacOperator.add then elsif tac_instruction^.operator = ElnaTacOperator.add then
elna_rtl_binary_arithmetic(instructions, tac_instruction, ElnaRtlOperator.add, variable_map) elna_rtl_binary_arithmetic(instructions, tac_instruction, ElnaRtlOperator.add, variable_map)
elsif tac_instruction^.operator = ElnaTacOperator.add_ptr then
elna_rtl_add_ptr(instructions, tac_instruction, variable_map)
elsif tac_instruction^.operator = ElnaTacOperator.load then elsif tac_instruction^.operator = ElnaTacOperator.load then
elna_rtl_load(instructions, tac_instruction, variable_map) elna_rtl_load(instructions, tac_instruction, variable_map)
elsif tac_instruction^.operator = ElnaTacOperator.copy_from_offset then elsif tac_instruction^.operator = ElnaTacOperator.copy_from_offset then
@@ -2410,17 +2443,20 @@ begin
return result return result
end; end;
proc elna_tac_sum_create(operator: ElnaTacOperator, lhs: ^ElnaTacOperand, rhs: ^ElnaTacOperand, proc elna_tac_sum_create(parser_node: ^ElnaTreeBinaryExpression, lhs: ^ElnaTacOperand,
symbol_table: ^ElnaSymbolTable, operand: ^ElnaTacOperand) -> ^ElnaTacInstruction; rhs: ^ElnaTacOperand, symbol_table: ^ElnaSymbolTable, operand: ^ElnaTacOperand) -> ^ElnaTacInstruction;
var var
instruction: ^ElnaTacInstruction; instruction: ^ElnaTacInstruction;
pointer_type: ^ElnaTypePointer;
begin begin
instruction := elna_tac_instruction_create(operator); pointer_type := parser_node^.lhs^.type_decoration;
instruction := elna_tac_instruction_create(ElnaTacOperator.add_ptr);
elna_tac_make_variable(@operand^.kind, @operand^.value, @operand^.length, symbol_table); elna_tac_make_variable(@operand^.kind, @operand^.value, @operand^.length, symbol_table);
elna_tac_instruction_set_operand(instruction, 1, operand^.kind, operand^.value, operand^.length); elna_tac_instruction_set_operand(instruction, 1, lhs^.kind, lhs^.value, lhs^.length);
elna_tac_instruction_set_operand(instruction, 2, lhs^.kind, lhs^.value, lhs^.length); elna_tac_instruction_set_operand(instruction, 2, rhs^.kind, rhs^.value, rhs^.length);
elna_tac_instruction_set_operand(instruction, 3, rhs^.kind, rhs^.value, rhs^.length); elna_tac_instruction_set_operand(instruction, 3, ElnaTacKind.constant, pointer_type^.base^.size, 0);
elna_tac_instruction_set_operand(instruction, 4, operand^.kind, operand^.value, operand^.length);
return instruction return instruction
end; end;
@@ -2458,7 +2494,7 @@ begin
elsif parser_node^.operator = ElnaLexerKind.minus then elsif parser_node^.operator = ElnaLexerKind.minus then
instruction := elna_tac_binary_create(ElnaTacOperator.subtract, @lhs, @rhs, symbol_table, operand) instruction := elna_tac_binary_create(ElnaTacOperator.subtract, @lhs, @rhs, symbol_table, operand)
elsif parser_node^.operator = ElnaLexerKind.pipe then elsif parser_node^.operator = ElnaLexerKind.pipe then
instruction := elna_tac_sum_create(ElnaTacOperator.add, @lhs, @rhs, symbol_table, operand) instruction := elna_tac_sum_create(parser_node, @lhs, @rhs, symbol_table, operand)
elsif parser_node^.operator = ElnaLexerKind.multiplication then elsif parser_node^.operator = ElnaLexerKind.multiplication then
instruction := elna_tac_binary_create(ElnaTacOperator.multiply, @lhs, @rhs, symbol_table, operand) instruction := elna_tac_binary_create(ElnaTacOperator.multiply, @lhs, @rhs, symbol_table, operand)
elsif parser_node^.operator = ElnaLexerKind.and then elsif parser_node^.operator = ElnaLexerKind.and then