Rename RTL symbol to data

This commit is contained in:
2026-04-11 23:02:41 +02:00
parent 2e27f78cff
commit a54285c71c
2 changed files with 57 additions and 38 deletions

View File

@@ -36,22 +36,25 @@ end
desc 'Convert previous stage language into the current stage language'
task :convert do
File.open('boot/stage21/cl.elna', 'w') do |current_stage|
File.readlines('boot/stage20/cl.elna').each do |line|
File.open('boot/stage22/cl.elna', 'w') do |current_stage|
seen_proc = false
File.readlines('boot/stage21/cl.elna').each do |line|
seen_proc = true if line.start_with? 'proc'
seen_proc = false if line.start_with? 'end'
if line.start_with?('begin') && !seen_proc
current_stage << <<~FUN
proc f();
var
x: ElnaLocation;
y: ElnaLocation;
begin
x := y;
end;
FUN
end
current_stage << line
end
# current_stage << <<~FUN
#
# proc f(m: ElnaInstructionModule);
# begin
# end;
#
# proc g();
# var
# x: ElnaInstructionModule;
# begin
# f(x)
# end;
# FUN
end
end

View File

@@ -566,7 +566,7 @@ type
ret,
nop
);
ElnaRtlKind = (register, immediate, symbol, pseudo, memory, pseudo_mem);
ElnaRtlKind = (register, immediate, data, pseudo, memory, pseudo_mem);
ElnaRtlOperand = record
kind: ElnaRtlKind;
value: Word;
@@ -917,7 +917,7 @@ begin
if pseudo_symbol = nil then
instruction := elna_rtl_instruction_create(ElnaRtlOperator.la);
elna_rtl_instruction_set_operand(instruction, 1, ElnaRtlKind.register, into, 0, 0);
elna_rtl_instruction_set_operand(instruction, 2, ElnaRtlKind.symbol, operand^.value, operand^.length, 0);
elna_rtl_instruction_set_operand(instruction, 2, ElnaRtlKind.data, operand^.value, operand^.length, 0);
elna_list_append(instructions, instruction);
instruction := elna_rtl_instruction_create(ElnaRtlOperator.lw);
@@ -932,11 +932,14 @@ begin
end
end;
proc elna_rtl_operand_value(instructions: ^ElnaList, tac_operand: ^ElnaTacOperand, variable_map: ^ElnaSymbolTable, rtl_operand: ^ElnaRtlOperand);
proc elna_rtl_operand_value(instructions: ^ElnaList, tac_operand: ^ElnaTacOperand, variable_map: ^ElnaSymbolTable,
rtl_operand: ^ElnaRtlOperand) -> ^ElnaRtlInfo;
var
instruction: ^ElnaRtlInstruction;
pseudo_symbol: ^ElnaRtlInfo;
begin
pseudo_symbol := nil;
if tac_operand^.kind = ElnaTacKind.constant then
elna_rtl_generate_pseudo(rtl_operand, variable_map);
@@ -951,20 +954,27 @@ begin
instruction := elna_rtl_instruction_create(ElnaRtlOperator.la);
elna_rtl_instruction_set_operand(instruction, 1, rtl_operand^.kind, rtl_operand^.value, rtl_operand^.length, 0);
elna_rtl_instruction_set_operand(instruction, 2, ElnaRtlKind.symbol, tac_operand^.value, tac_operand^.length, 0);
elna_rtl_instruction_set_operand(instruction, 2, ElnaRtlKind.data, tac_operand^.value, tac_operand^.length, 0);
elna_list_append(instructions, instruction);
instruction := elna_rtl_instruction_create(ElnaRtlOperator.lw);
elna_rtl_instruction_set_operand(instruction, 1, rtl_operand^.kind, rtl_operand^.value, rtl_operand^.length, 0);
elna_rtl_instruction_set_operand(instruction, 2, rtl_operand^.kind, rtl_operand^.value, rtl_operand^.length, 0);
elna_list_append(instructions, instruction)
elsif pseudo_symbol^.rtl_type^.kind = ElnaRtlTypeKind.byte_array then
rtl_operand^.kind := ElnaRtlKind.pseudo_mem;
rtl_operand^.value := tac_operand^.value;
rtl_operand^.length := tac_operand^.length;
rtl_operand^.offset := 0
else
rtl_operand^.kind := ElnaRtlKind.pseudo;
rtl_operand^.value := tac_operand^.value;
rtl_operand^.length := tac_operand^.length
end
rtl_operand^.length := tac_operand^.length;
rtl_operand^.offset := 0
end
end;
return pseudo_symbol
end;
proc elna_rtl_binary_arithmetic(instructions: ^ElnaList, tac_instruction: ^ElnaTacInstruction,
operation: ElnaRtlOperator, variable_map: ^ElnaSymbolTable) -> ^ElnaRtlInstruction;
@@ -1081,9 +1091,14 @@ begin
elna_rtl_instruction_set_operand(instruction, 1, target_operand^.kind,
target_operand^.value, target_operand^.length, 0);
if pseudo_symbol = nil then
elna_rtl_instruction_set_operand(instruction, 2, ElnaRtlKind.symbol,
elna_rtl_instruction_set_operand(instruction, 2, ElnaRtlKind.data,
addressable^.value, addressable^.length, 0)
elsif pseudo_symbol^.rtl_type^.kind = ElnaRtlTypeKind.byte_array then
elna_rtl_instruction_set_operand(instruction, 2, ElnaRtlKind.pseudo_mem,
addressable^.value, addressable^.length, 0)
else
(* Debug
printf("# %.*s, %i\n\0", addressable^.length, addressable^.value, pseudo_symbol^.rtl_type^.kind); *)
elna_rtl_instruction_set_operand(instruction, 2, ElnaRtlKind.pseudo,
addressable^.value, addressable^.length, 0)
end;
@@ -1112,7 +1127,7 @@ begin
instruction := elna_rtl_instruction_create(condition);
elna_rtl_operand_value(instructions, @tac_instruction^.operands[1], variable_map, @rtl_operand);
elna_rtl_instruction_set_operand(instruction, 1, rtl_operand.kind, rtl_operand.value, rtl_operand.length, 0);
elna_rtl_instruction_set_operand(instruction, 2, ElnaRtlKind.symbol,
elna_rtl_instruction_set_operand(instruction, 2, ElnaRtlKind.data,
tac_instruction^.operands[2].value, tac_instruction^.operands[2].length, 0);
elna_list_append(instructions, instruction)
@@ -1157,7 +1172,7 @@ begin
goto elna_rtl_call_loop
end;
instruction := elna_rtl_instruction_create(ElnaRtlOperator.jal);
elna_rtl_instruction_set_operand(instruction, 1, ElnaRtlKind.symbol,
elna_rtl_instruction_set_operand(instruction, 1, ElnaRtlKind.data,
tac_instruction^.operands[1].value, tac_instruction^.operands[1].length, 0);
elna_list_append(instructions, instruction);
@@ -1174,8 +1189,8 @@ var
rtl_operand: ElnaRtlOperand;
begin
elna_rtl_operand_value(instructions, @tac_instruction^.operands[1], variable_map, @rtl_operand);
instruction := elna_rtl_instruction_create(ElnaRtlOperator.sw);
instruction := elna_rtl_instruction_create(ElnaRtlOperator.sw);
elna_rtl_instruction_set_operand(instruction, 1, rtl_operand.kind, rtl_operand.value, rtl_operand.length, 0);
elna_rtl_instruction_set_operand(instruction, 2, ElnaRtlKind.pseudo,
tac_instruction^.operands[2].value, tac_instruction^.operands[2].length, 0);
@@ -1250,7 +1265,7 @@ begin
instruction := elna_rtl_instruction_create(ElnaRtlOperator.la);
elna_rtl_instruction_set_operand(instruction, 1, target_operand.kind, target_operand.value, target_operand.length, 0);
elna_rtl_instruction_set_operand(instruction, 2, ElnaRtlKind.symbol,
elna_rtl_instruction_set_operand(instruction, 2, ElnaRtlKind.data,
tac_instruction^.operands[2].value, tac_instruction^.operands[2].length, 0);
elna_list_append(instructions, instruction);
@@ -1320,7 +1335,7 @@ begin
elna_rtl_unary(instructions, tac_instruction, ElnaRtlOperator.not, variable_map)
elsif tac_instruction^.operator = ElnaTacOperator.jump then
instruction := elna_rtl_instruction_create(ElnaRtlOperator.j);
elna_rtl_instruction_set_operand(instruction, 1, ElnaRtlKind.symbol,
elna_rtl_instruction_set_operand(instruction, 1, ElnaRtlKind.data,
tac_instruction^.operands[1].value, tac_instruction^.operands[1].length, 0);
elna_list_append(instructions, instruction)
elsif tac_instruction^.operator = ElnaTacOperator.jump_if_zero then
@@ -1331,7 +1346,7 @@ begin
elna_rtl_load_operand_value(instructions, @tac_instruction^.operands[1], ElnaRtlRegister.a0, variable_map)
elsif tac_instruction^.operator = ElnaTacOperator.label then
instruction := elna_rtl_instruction_create(ElnaRtlOperator.label);
elna_rtl_instruction_set_operand(instruction, 1, ElnaRtlKind.symbol,
elna_rtl_instruction_set_operand(instruction, 1, ElnaRtlKind.data,
tac_instruction^.operands[1].value, tac_instruction^.operands[1].length, 0);
elna_list_append(instructions, instruction)
elsif tac_instruction^.operator = ElnaTacOperator.copy then
@@ -1455,7 +1470,7 @@ begin
_write_c('(');
elna_riscv_register(instruction^.operands[n].value);
_write_c(')')
elsif operand_type = ElnaRtlKind.symbol then
elsif operand_type = ElnaRtlKind.data then
if instruction^.operands[n].length = 0 then
_write_label(instruction^.operands[n].value, 0)
else
@@ -1524,7 +1539,8 @@ proc elna_alloc_load_address(instructions: ^ElnaList, instruction: ^ElnaRtlInstr
var
pseudo_symbol: ^ElnaRtlInfo;
begin
if instruction^.operands[2].kind = ElnaRtlKind.pseudo then
(* pseudo or pseudo_mem. *)
if instruction^.operands[2].kind <> ElnaRtlKind.data then
pseudo_symbol := elna_alloc_variable(instruction^.operands[2].value, instruction^.operands[2].length,
variable_map);
instruction^.operator := ElnaRtlOperator.addi;
@@ -1969,7 +1985,7 @@ begin
_write_c('\n');
end;
proc elna_parser_integer_literal(cursor: ^ElnaLexerCursor);
proc elna_parser_integer_literal(cursor: ^ElnaLexerCursor) -> ^ElnaTreeIntegerLiteral;
var
token: ^ElnaLexerToken;
result: ^ElnaTreeIntegerLiteral;
@@ -1989,7 +2005,7 @@ begin
return result
end;
proc elna_parser_boolean_literal(cursor: ^ElnaLexerCursor);
proc elna_parser_boolean_literal(cursor: ^ElnaLexerCursor) -> ^ElnaTreeBooleanLiteral;
var
result: ^ElnaTreeBooleanLiteral;
begin
@@ -2038,7 +2054,7 @@ begin
operand^.length := 0
end;
proc elna_parser_character_literal(cursor: ^ElnaLexerCursor);
proc elna_parser_character_literal(cursor: ^ElnaLexerCursor) -> ^ElnaTreeCharacterLiteral;
var
result: ^ElnaTreeCharacterLiteral;
token: ^ElnaLexerToken;
@@ -2084,7 +2100,7 @@ begin
operand^.length := variable_expression^.length
end;
proc elna_parser_string_literal(cursor: ^ElnaLexerCursor);
proc elna_parser_string_literal(cursor: ^ElnaLexerCursor) -> ^ElnaTreeStringLiteral;
var
result: ^ElnaTreeStringLiteral;
token: ^ElnaLexerToken;
@@ -2123,7 +2139,7 @@ begin
elna_list_append(instructions, instruction)
end;
proc elna_parser_trait_expression(cursor: ^ElnaLexerCursor);
proc elna_parser_trait_expression(cursor: ^ElnaLexerCursor) -> ^ElnaTreeTraitExpression;
var
result: ^ElnaTreeTraitExpression;
token: ^ElnaLexerToken;
@@ -2144,10 +2160,10 @@ begin
return result
end;
proc elna_parser_simple_expression(cursor: ^ElnaLexerCursor);
proc elna_parser_simple_expression(cursor: ^ElnaLexerCursor) -> ^ElnaTreeExpression;
var
current_character: Word;
parser_node: Word;
parser_node: ^ElnaTreeExpression;
token: ^ElnaLexerToken;
begin
parser_node := 0;
@@ -3871,7 +3887,7 @@ begin
return result
end;
proc elna_tac_procedure_declaration(parser_node: ElnaTreeProcedureDeclaration);
proc elna_tac_procedure_declaration(parser_node: ^ElnaTreeProcedureDeclaration);
var
symbol_info: ^ElnaSymbolProcedureInfo;
result: ^ElnaTacProcedure;