Skip to content

Commit

Permalink
Add a struct construct to the lasm.
Browse files Browse the repository at this point in the history
Structs produces offsets for use, but no output.
  • Loading branch information
atrodo committed Oct 30, 2010
1 parent 1c5e8c5 commit b9f6ad6
Showing 1 changed file with 96 additions and 3 deletions.
99 changes: 96 additions & 3 deletions lasm.pl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

=for grammar
#<goal> -> ( <code> | <data> )*
#<goal> -> ( <code> | <data> | <struct> )*
#<code> -> .sub <str> <stmt>* .end;
#<data> -> .data <str> <data_stmt>* .end;
#<struct> -> .struct <str> <struct_stmt>* .end;
#<stmt> -> <label>? <dest>? <regtype>? <opcode> ( <lhs> ( ,? | , <rhs> ) )? ( : ( <imm> | <offset> | <jmp> ) )? ;
#<data_stmt> -> <label>? <const> ;
#<struct_stmt> -> <label>? size <int> ;
#<label> -> <id>? :
#<dest> -> <reg> =
#<regtype> -> ( INT | NUM | STR | PMC )
Expand Down Expand Up @@ -128,7 +130,7 @@ sub max
#warn "$max\n";
}

#<goal> -> ( <code> | <data> )*
#<goal> -> ( <code> | <data> | <struct> )*
sub goal
{
my $str = shift;
Expand All @@ -152,6 +154,12 @@ sub goal
push @$result, $data;
next;
}
my $struct = &struct($str);
if (defined $struct)
{
push @$result, $struct;
next;
}
last;
}

Expand Down Expand Up @@ -211,7 +219,7 @@ sub data
my $pos = pos $$str;
my $result = {};

if ($$str !~ m/$som [.]data /ixmsgc)
if ($$str !~ m/$som [.] data /ixmsgc)
{
max($str, $pos);
return;
Expand Down Expand Up @@ -242,6 +250,44 @@ sub data
return $result;
}

#<struct> -> .struct <str> <struct_stmt>* .end;
sub struct
{
my $str = shift;
my $pos = pos $$str;
my $result = {};

if ($$str !~ m/$som [.] struct /ixmsgc)
{
max($str, $pos);
return;
}

$result->{typed} = "struct";

my $named = &str($str);
if (!defined $named)
{
max($str, $pos);
return;
}
$result->{named} = $named;

$result->{datas} = [];
while (my $struct_stmt = &struct_stmt($str))
{
push @{$result->{datas}}, $struct_stmt;
}

if ($$str !~ m/$som [.]end; /ixmsgc)
{
max($str, $pos);
return;
}

return $result;
}

#<stmt> -> <label>? <dest>? <regtype>? <opcode> ( <lhs> ( ,? | , <rhs> ) )? ( : ( <imm> | <offset> | <jmp> ) )? ;
sub stmt
{
Expand Down Expand Up @@ -339,6 +385,39 @@ sub data_stmt
return $result;
}

#<struct_stmt> -> <label>? size <int> ;
sub struct_stmt
{
my $str = shift;
my $pos = pos $$str;
my $result = {};

$result->{label} = label($str);

if ($$str !~ m/$som size /ixmsgc)
{
max($str, $pos);
return;
}

my $size = &int($str);
if (!defined $size)
{
max($str, $pos);
return;
}

$result->{size} = $size;

if ($$str !~ m/$som ; /ixmsgc)
{
max($str, $pos);
return;
}

return $result;
}

#<label> -> <id>? :
sub label
{
Expand Down Expand Up @@ -657,6 +736,20 @@ sub gen_op

foreach my $seg (@$ast)
{
if ($seg->{typed} eq "struct")
{
$data{$seg->{named}} = {};
my $current = $data{$seg->{named}};
my $curr_size = 0;
foreach my $data (@{$seg->{datas}})
{
if (defined $data->{label})
{
$current->{$data->{label}} = $curr_size;
}
$curr_size += $data->{size};
}
}
if ($seg->{typed} eq "data")
{
$data{$seg->{named}} = {};
Expand Down

0 comments on commit b9f6ad6

Please sign in to comment.