Calculate record field sizes

This commit is contained in:
2026-01-07 13:55:55 +01:00
parent e4257b08be
commit 396c329b63
2 changed files with 76 additions and 79 deletions

View File

@@ -67,29 +67,15 @@ file 'build/build.ninja' => ['build'] do |t|
rule link2 rule link2
command = ld -o $out --dynamic-linker /lib32/ld-linux-riscv32-ilp32d.so.1 /usr/lib/crt1.o /usr/lib/crti.o -lc $in /usr/lib/crtn.o command = ld -o $out --dynamic-linker /lib32/ld-linux-riscv32-ilp32d.so.1 /usr/lib/crt1.o /usr/lib/crti.o -lc $in /usr/lib/crtn.o
rule boot1 rule bootstrap
command = build/boot/stage1/cl < \$in > \$out command = $bootstrap < \$in > \$out
rule valid1
command = build/valid/stage1/cl < \$in > \$out
NINJA NINJA
STAGES.each do |stage|
stage_number = stage.delete_prefix('stage').to_i
f << <<~NINJA
rule valid#{stage_number}
command = build/valid/stage#{stage_number}/cl < \$in > \$out
rule boot#{stage_number}
command = build/boot/stage#{stage_number}/cl < \$in > \$out
NINJA
end
f << <<~NINJA f << <<~NINJA
build build/boot/stage1/cl: cc boot/stage1.s build build/boot/stage1/cl: cc boot/stage1.s
build build/valid/stage1/cl.s: bootstrap boot/stage1.s | build/boot/stage1/cl
bootstrap = build/boot/stage1/cl
build build/valid/stage1/cl.s: boot1 boot/stage1.s | build/boot/stage1/cl
build build/valid/stage1/cl.o: as build/valid/stage1/cl.s build build/valid/stage1/cl.o: as build/valid/stage1/cl.s
build build/valid/stage1/cl: link1 build/valid/stage1/cl.o build build/valid/stage1/cl: link1 build/valid/stage1/cl.o
NINJA NINJA
@@ -106,11 +92,15 @@ file 'build/build.ninja' => ['build'] do |t|
valid_stage = "build/valid/stage#{stage_number}" valid_stage = "build/valid/stage#{stage_number}"
f << <<~NINJA f << <<~NINJA
build #{boot_stage}/cl.s: valid#{stage_number.pred} boot/stage#{stage_number}/cl.elna | build/valid/stage#{stage_number.pred}/cl build #{boot_stage}/cl.s: bootstrap boot/stage#{stage_number}/cl.elna | build/valid/stage#{stage_number.pred}/cl
bootstrap = build/valid/stage#{stage_number.pred}/cl
build #{boot_stage}/cl.o: as #{boot_stage}/cl.s build #{boot_stage}/cl.o: as #{boot_stage}/cl.s
build #{boot_stage}/cl: #{link} #{boot_stage}/cl.o build #{boot_stage}/cl: #{link} #{boot_stage}/cl.o
build #{valid_stage}/cl.s: boot#{stage_number} boot/stage#{stage_number}/cl.elna | #{boot_stage}/cl build #{valid_stage}/cl.s: bootstrap boot/stage#{stage_number}/cl.elna | #{boot_stage}/cl
bootstrap = build/boot/stage#{stage_number}/cl
build #{valid_stage}/cl.o: as #{valid_stage}/cl.s build #{valid_stage}/cl.o: as #{valid_stage}/cl.s
build #{valid_stage}/cl: #{link} #{valid_stage}/cl.o build #{valid_stage}/cl: #{link} #{valid_stage}/cl.o
NINJA NINJA

View File

@@ -7,6 +7,8 @@ program;
(* Stage 18 compiler. *) (* Stage 18 compiler. *)
(* - Record fields can be aggregates themselves *)
type type
(** (**
* List of intermediate representation items. * List of intermediate representation items.
@@ -27,6 +29,43 @@ type
parameters: Word; parameters: Word;
count: Word count: Word
end; end;
(* Type representation. *)
ElnaTypeKind = (primitive, enumeration, _record, pointer, array);
ElnaType = record
kind: ElnaTypeKind;
size: Word
end;
ElnaTypeEnumeration = record
kind: ElnaTypeKind;
size: Word;
members: Word;
length: Word
end;
ElnaTypeField = record
name: ElnaTypeKind;
length: Word;
field_type: ^ElnaType
end;
ElnaTypeRecord = record
kind: ElnaTypeKind;
size: Word;
members: Word;
length: Word
end;
ElnaTypePointer = record
kind: ElnaTypeKind;
size: Word;
base: Word
end;
ElnaTypeArray = record
kind: ElnaTypeKind;
size: Word;
base: Word;
length: Word
end;
(* Abstract syntax tree nodes. *)
ElnaTreeKind = ( ElnaTreeKind = (
integer_literal, integer_literal,
string_literal, string_literal,
@@ -60,56 +99,56 @@ type
end; end;
ElnaTreeExpression = record ElnaTreeExpression = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
type_decoration: Word type_decoration: ^ElnaType
end; end;
ElnaTreeIntegerLiteral = record ElnaTreeIntegerLiteral = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
type_decoration: Word; type_decoration: ^ElnaType;
value: Word; value: Word;
length: Word length: Word
end; end;
ElnaTreeCharacterLiteral = record ElnaTreeCharacterLiteral = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
type_decoration: Word; type_decoration: ^ElnaType;
value: Word; value: Word;
length: Word length: Word
end; end;
ElnaTreeNilLiteral = record ElnaTreeNilLiteral = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
type_decoration: Word type_decoration: ^ElnaType
end; end;
ElnaTreeBooleanLiteral = record ElnaTreeBooleanLiteral = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
type_decoration: Word; type_decoration: ^ElnaType;
value: Word value: Word
end; end;
ElnaTreeVariableExpression = record ElnaTreeVariableExpression = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
type_decoration: Word; type_decoration: ^ElnaType;
name: Word; name: Word;
length: Word length: Word
end; end;
ElnaTreeStringLiteral = record ElnaTreeStringLiteral = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
type_decoration: Word; type_decoration: ^ElnaType;
value: Word; value: Word;
length: Word length: Word
end; end;
ElnaTreeDereferenceExpression = record ElnaTreeDereferenceExpression = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
type_decoration: Word; type_decoration: ^ElnaType;
pointer: Word pointer: Word
end; end;
ElnaTreeBinaryExpression = record ElnaTreeBinaryExpression = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
type_decoration: Word; type_decoration: ^ElnaType;
lhs: Word; lhs: Word;
rhs: Word; rhs: Word;
operator: Word operator: Word
end; end;
ElnaTreeUnaryExpression = record ElnaTreeUnaryExpression = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
type_decoration: Word; type_decoration: ^ElnaType;
operand: Word; operand: Word;
operator: Word operator: Word
end; end;
@@ -126,7 +165,7 @@ type
kind: ElnaTreeKind; kind: ElnaTreeKind;
next: Word; next: Word;
conditionals: Word; conditionals: Word;
_else: Word _else: ^ElnaTreeStatement
end; end;
ElnaTreeGotoStatement = record ElnaTreeGotoStatement = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
@@ -138,12 +177,12 @@ type
kind: ElnaTreeKind; kind: ElnaTreeKind;
next: Word; next: Word;
assignee: Word; assignee: Word;
assignment: Word assignment: ^ElnaTreeExpression
end; end;
ElnaTreeReturnStatement = record ElnaTreeReturnStatement = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
next: Word; next: Word;
returned: Word returned: ^ElnaTreeExpression
end; end;
ElnaTreeLabelDeclaration = record ElnaTreeLabelDeclaration = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
@@ -153,7 +192,7 @@ type
end; end;
ElnaTreeFieldAccessExpression = record ElnaTreeFieldAccessExpression = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
type_decoration: Word; type_decoration: ^ElnaType;
aggregate: Word; aggregate: Word;
field: Word; field: Word;
length: Word length: Word
@@ -175,11 +214,11 @@ type
end; end;
ElnaTreePointerTypeExpression = record ElnaTreePointerTypeExpression = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
base: Word base: ^ElnaTreeNode
end; end;
ElnaTreeArrayTypeExpression = record ElnaTreeArrayTypeExpression = record
kind: ElnaTreeKind; kind: ElnaTreeKind;
base: Word; base: ^ElnaTreeNode;
length: Word length: Word
end; end;
ElnaTreeTraitExpression = record ElnaTreeTraitExpression = record
@@ -193,8 +232,8 @@ type
* Used for example to represent if and elsif blocks with beloning statements. * Used for example to represent if and elsif blocks with beloning statements.
*) *)
ElnaTreeConditionalStatements = record ElnaTreeConditionalStatements = record
condition: Word; condition: ^ElnaTreeExpression;
statements: Word; statements: ^ElnaTreeStatement;
next: Word next: Word
end; end;
ElnaTreeDeclaration = record ElnaTreeDeclaration = record
@@ -233,47 +272,14 @@ type
_type: Word _type: Word
end; end;
(* Type representation. *) (* Symbol table information. *)
ElnaSymbolInfoKind = (type_info, parameter_info, temporary_info, procedure_info); ElnaSymbolInfoKind = (type_info, parameter_info, temporary_info, procedure_info);
ElnaTypeKind = (primitive, enumeration, _record, pointer, array);
ElnaType = record
kind: ElnaTypeKind;
size: Word
end;
ElnaTypeEnumeration = record
kind: ElnaTypeKind;
size: Word;
members: Word;
length: Word
end;
ElnaTypeField = record
name: ElnaTypeKind;
length: Word;
field_type: Word
end;
ElnaTypeRecord = record
kind: ElnaTypeKind;
size: Word;
members: Word;
length: Word
end;
ElnaTypePointer = record
kind: ElnaTypeKind;
size: Word;
base: Word
end;
ElnaTypeArray = record
kind: ElnaTypeKind;
size: Word;
base: Word;
length: Word
end;
ElnaSymbolInfo = record ElnaSymbolInfo = record
kind: ElnaSymbolInfoKind kind: ElnaSymbolInfoKind
end; end;
ElnaSymbolTypeInfo = record ElnaSymbolTypeInfo = record
kind: ElnaSymbolInfoKind; kind: ElnaSymbolInfoKind;
_type: Word _type: ^ElnaType
end; end;
ElnaSymbolTemporaryInfo = record ElnaSymbolTemporaryInfo = record
kind: ElnaSymbolInfoKind; kind: ElnaSymbolInfoKind;
@@ -1959,7 +1965,7 @@ begin
result^.kind := ElnaTreeKind.variable_expression; result^.kind := ElnaTreeKind.variable_expression;
result^.name := name_pointer; result^.name := name_pointer;
result^.length := name_length; result^.length := name_length;
result^.type_decoration := nil; result^.type_decoration := nil;
return result return result
end; end;
@@ -2679,7 +2685,7 @@ proc elna_tac_designator(parser_node: ^ElnaTreeExpression, symbol_table: Word, i
var var
field_access_expression: ^ElnaTreeFieldAccessExpression; field_access_expression: ^ElnaTreeFieldAccessExpression;
dereference_expression: ^ElnaTreeDereferenceExpression; dereference_expression: ^ElnaTreeDereferenceExpression;
expression_type: ^ElnaType; field_type: ^ElnaType;
first_instruction: Word; first_instruction: Word;
last_instruction: Word; last_instruction: Word;
designator_base: ^ElnaTreeExpression; designator_base: ^ElnaTreeExpression;
@@ -2694,7 +2700,6 @@ begin
is_address^ := 1 is_address^ := 1
elsif parser_node^.kind = ElnaTreeKind.field_access_expression then elsif parser_node^.kind = ElnaTreeKind.field_access_expression then
field_access_expression := parser_node; field_access_expression := parser_node;
expression_type := field_access_expression^.type_decoration;
designator_base := field_access_expression^.aggregate; designator_base := field_access_expression^.aggregate;
aggregate_type := designator_base^.type_decoration; aggregate_type := designator_base^.type_decoration;
@@ -2711,9 +2716,10 @@ begin
.elna_tac_designator_field; .elna_tac_designator_field;
if string_compare(field_access_expression^.field, field_access_expression^.length, current_field^.name, current_field^.length) = 0 then if string_compare(field_access_expression^.field, field_access_expression^.length, current_field^.name, current_field^.length) = 0 then
field_type := current_field^.field_type;
field_count := field_count - 1; field_count := field_count - 1;
current_field := current_field + #size(ElnaTypeField); current_field := current_field + #size(ElnaTypeField);
field_offset := field_offset + 4; field_offset := field_offset + field_type^.size;
goto elna_tac_designator_field goto elna_tac_designator_field
end; end;
last_instruction := _elna_tac_instruction_create(ElnaTacOperator.add); last_instruction := _elna_tac_instruction_create(ElnaTacOperator.add);
@@ -3388,9 +3394,10 @@ var
member_count: Word; member_count: Word;
member_array_start: Word; member_array_start: Word;
member_array_current: ^ElnaTypeField; member_array_current: ^ElnaTypeField;
field_type: Word; field_type: ^ElnaType;
begin begin
result := malloc(#size(ElnaTypeRecord)); result := malloc(#size(ElnaTypeRecord));
result^.size := 0;
memory_start := parser_node^.members; memory_start := parser_node^.members;
member_count := parser_node^.length; member_count := parser_node^.length;
@@ -3407,6 +3414,7 @@ begin
memory_start := memory_start + 4; memory_start := memory_start + 4;
field_type := elna_name_type_expression(memory_start^); field_type := elna_name_type_expression(memory_start^);
result^.size := result^.size + field_type^.size;
member_array_current^.field_type := field_type; member_array_current^.field_type := field_type;
member_array_current := member_array_current + #size(ElnaTypeField); member_array_current := member_array_current + #size(ElnaTypeField);
@@ -3418,7 +3426,6 @@ begin
end; end;
result^.kind := ElnaTypeKind._record; result^.kind := ElnaTypeKind._record;
result^.size := parser_node^.length * 4;
result^.members := member_array_start; result^.members := member_array_start;
result^.length := parser_node^.length; result^.length := parser_node^.length;