-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen_symboltable.pl
executable file
·100 lines (83 loc) · 2.11 KB
/
gen_symboltable.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/perl -w
# vim: set noexpandtab:ts=8:sw=8
# Copyright (c) 2009, Gerard Lledo Vives <[email protected]>
# This program is open source. For license terms, see the LICENSE file.
use 5.010;
use strict;
my %symbols;
my %comp_symbols;
my %all_symbols;
while (<ops/*.s>) {
open IN, $_ or die "Can't open ops.s: $!\n";
while(<IN>) {
next if !/\.global\s+(\S+).+\"(.*)\"/;
# Maps symbols to symbol names
$symbols{$1} = $2;
}
close IN;
}
open IN, "compile.s" or die "Can't open compile.s: $!\n";
while(<IN>) {
next if !/\.global\s+(\S+).+\"(.*)\"/;
$comp_symbols{$1} = $2;
}
close IN;
%all_symbols = ( %symbols, %comp_symbols );
open OUT, ">/dev/stdout" or die "$!\n";
say OUT " .include \"regdefs.asi\"";
say OUT " .section .rodata";
say OUT " .align 2";
for my $sym ( keys %symbols ) {
(my $SYM = $sym) =~ tr/a-z/A-Z/;
say OUT ".LC$SYM:";
say OUT " .asciz \"$symbols{$sym}\"";
}
for my $sym ( keys %comp_symbols ) {
(my $SYM = $sym) =~ tr/a-z/A-Z/;
say OUT ".LC$SYM:";
say OUT " .asciz \"$comp_symbols{$sym}\"";
}
say OUT " .text";
say OUT " .align 2";
say OUT " .global init_symbols";
say OUT "";
say OUT "init_sym:";
say OUT " push {lr}";
say OUT " mov r0, r2";
say OUT " bl symtable_set_fun";
say OUT " mov r0, r1";
say OUT " mov r1, #32";
say OUT " bl symtable_set_name";
say OUT " pop {lr}";
say OUT " bx lr";
say OUT "";
say OUT "init_symbols:";
say OUT " push {lr}";
for my $sym ( keys %symbols ) {
(my $SYM = $sym) =~ tr/a-z/A-Z/;
say OUT " ldr r1, .L$SYM"."_ID";
say OUT " ldr r2, .L$SYM"."_OP";
say OUT " bl init_sym";
say OUT " bl symtable_setflag_interp";
say OUT " bl symtable_next";
}
for my $sym ( keys %comp_symbols ) {
(my $SYM = $sym) =~ tr/a-z/A-Z/;
say OUT " ldr r1, .L$SYM"."_ID";
say OUT " ldr r2, .L$SYM"."_OP";
say OUT " bl init_sym";
say OUT " bl symtable_next";
}
say OUT " bl symtable_set_null";
say OUT " bl symtable_restart";
say OUT " pop {lr}";
say OUT " bx lr";
say OUT "";
say OUT " .align 2";
for my $sym ( keys %all_symbols ) {
(my $SYM = $sym) =~ tr/a-z/A-Z/;
say OUT ".L$SYM"."_ID:";
say OUT " .word .LC$SYM";
say OUT ".L$SYM"."_OP:";
say OUT " .word $sym";
}