f2c4dc81
Salvador Abreu
added most of the...
|
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
|
divert(-1)
# -----------------------------------------------------------------------------
#
# Make m4 macros less obtrusive.
#
# -----------------------------------------------------------------------------
define(_define_,defn(`define'))undefine(`define')
_define_(_defn_,defn(`defn'))undefine(`defn')
_define_(_undefine_,_defn_(`undefine'))undefine(`undefine')
_define_(_prepend_,`_define_(_$1_,_defn_(`$1'))_undefine_(`$1')')
_prepend_(`builtin')
_prepend_(`changecom')dnl
_prepend_(`changequote')
_prepend_(`debugfile')
_prepend_(`debugmode')
_prepend_(`decr')
_prepend_(`divert')
_prepend_(`divnum')
_prepend_(`dnl')
_prepend_(`dumpdef')
_prepend_(`errprint')
_prepend_(`esyscmd')
_prepend_(`eval')
_prepend_(`format')
_prepend_(`ifdef')
_prepend_(`ifelse')
_prepend_(`include')
_prepend_(`incr')
_prepend_(`index')
_prepend_(`indir')
_prepend_(`len')
_prepend_(`m4exit')
_prepend_(`m4wrap')
_prepend_(`maketemp')
_prepend_(`patsubst')
_prepend_(`popdef')
_prepend_(`pushdef')
_prepend_(`regexp')
_prepend_(`shift')
_prepend_(`sinclude')
_prepend_(`substr')
_prepend_(`syscmd')
_prepend_(`sysval')
_prepend_(`traceoff')
_prepend_(`traceon')
_prepend_(`translit')
_prepend_(`undivert')
# -----------------------------------------------------------------------------
_define_(`_CURPC_', 0)
_define_(`_pc_at_', `P_`'_eval_(_CURPC_+$1)_define_(`_L_'_eval_(_CURPC_+$1))')
_define_(`_np_', `_define_(`_CURPC_',_incr_(_CURPC_))_dnl_
_ifdef_(`_L_'_CURPC_,P_`'_CURPC_:,)')
_define_(`push', `_np_ M[--SP] = _refto_($1); // PUSH $1')
_define_(`pop', `_np_ ++SP; // POP')
_define_(`dup', `_np_ M[SP-1] = M[SP]; --SP; // DUP')
_define_(`swap', `_np_ M[SP-1] = M[SP]; M[SP] = M[SP+1]; M[SP+1]=M[SP-1]; // SWAP')
_define_(`add', `_np_ M[SP+1] = M[SP+1] + M[SP+0]; ++SP; // ADD')
_define_(`sub', `_np_ M[SP+1] = M[SP+1] - M[SP+0]; ++SP; // SUB')
_define_(`mul', `_np_ M[SP+1] = M[SP+1] * M[SP+0]; ++SP; // MUL')
_define_(`div', `_np_ M[SP+1] = M[SP+1] / M[SP+0]; ++SP; // DIV')
_define_(`mod', `_np_ M[SP+1] = M[SP+1] % M[SP+0]; ++SP; // MOD')
_define_(`slt', `_np_ M[SP+1] = M[SP+1] < M[SP+0]; ++SP; // SLT')
_define_(`load', `_np_ M[SP] = M[M[SP]]; // LOAD')
_define_(`store', `_np_ M[M[SP]] = M[SP+1]; SP += 2; // STORE')
_define_(`skipz', `_np_ if (! M[SP++]) goto _pc_at_(2); // SKIPZ')
_define_(`jump', `_np_ goto * ((void *) M[SP++]); // JUMP')
|
fb3afe45
Salvador Abreu
doing 64bits now...
|
75
|
_define_(`call', `_np_ { void *C = (void *) M[SP]; M[SP]=(long)&&_pc_at_(1); goto *C; } // CALL')
|
f2c4dc81
Salvador Abreu
added most of the...
|
76
77
78
79
|
_define_(`local', `_np_ M[SP] = M[SP] + FP; // LOCAL')
_define_(`stack', `_np_ M[SP] = M[SP] + SP; // STACK')
|
fb3afe45
Salvador Abreu
doing 64bits now...
|
80
|
_define_(`link', `_np_ { long N = M[SP]; M[SP]= FP; FP=SP+1; SP -= N; } // LINK')
|
f2c4dc81
Salvador Abreu
added most of the...
|
81
82
83
84
85
86
87
88
89
90
91
92
|
_define_(`unlink', `_np_ SP=FP; FP=M[SP-1]; // UNLINK')
_define_(`dup_sr', `_np_ SR = M[SP]; // DUP_SR')
_define_(`push_sr', `_np_ M[--SP] = SR; // PUSH_SR')
_dnl_ -- labels ---------------------------------------------------------------
_define_(`_refto_', `_dnl_
_ifelse_(_regexp_($1,`^[-0-9]'), 0, `_dnl_
$1', `_dnl_ -- Numbers go untouched ------------
_ifdef_(`_DATA_'$1,_dnl_
_DATA_`'$1,_dnl_ -- (known) data labels -------------
|
fb3afe45
Salvador Abreu
doing 64bits now...
|
93
|
(long) &&$1`'_dnl_ -- code labels ---------------------
|
f2c4dc81
Salvador Abreu
added most of the...
|
94
95
96
97
|
)')')
_define_(`label', `_dnl_
_ifelse_(_data_text_,__data__,_dnl_
|
fb3afe45
Salvador Abreu
doing 64bits now...
|
98
|
` long m_`'$1[0]; // data `label' for "$1"_dnl_
|
f2c4dc81
Salvador Abreu
added most of the...
|
99
100
101
102
103
104
|
_define_(`_DATA_'$1,`GV_'$1)_global_var_($1)',_dnl_
`$1:')')
_define_(`_ac_',0)
_define_(`_anon_', `_define_(`_ac_',_incr_(_ac_))_format_(`a_%04.4d',_ac_)')
|
fb3afe45
Salvador Abreu
doing 64bits now...
|
105
|
_define_(`_word_',` long _anon_;_divert_(4)_dnl_
|
f2c4dc81
Salvador Abreu
added most of the...
|
106
107
108
|
global.named._format_(`a_%04.4d',_ac_) = _initializer_($1);
_divert_(1)')
|
fb3afe45
Salvador Abreu
doing 64bits now...
|
109
|
_define_(`_def_word_', ` long m_$1;_dnl_
|
f2c4dc81
Salvador Abreu
added most of the...
|
110
111
112
|
_divert_(4) global.named.m_$1 = _initializer_($2);
_define_(`_DATA_'$1,`GV_'$1)_global_var_($1)')
|
fb3afe45
Salvador Abreu
doing 64bits now...
|
113
|
_define_(`_def_space_', ` long m_$1[$2];_dnl_
|
f2c4dc81
Salvador Abreu
added most of the...
|
114
115
116
117
118
119
120
|
_divert_(4) for (i=0; i<$2; ++i) global.named.m_$1[i] = 0;
_define_(`_DATA_'$1,`GV_'$1)_global_var_($1)')
_define_(`_initializer_',`_dnl_
_ifelse_(_regexp_($1,`^[-0-9]'), 0, `$1', `GV_`'$1')')
_define_(`_global_var_',`_divert_(3)_dnl_
|
fb3afe45
Salvador Abreu
doing 64bits now...
|
121
|
#define GV_$1 ((long *) &global.named.m_$1 - (long *) &global.named)
|
f2c4dc81
Salvador Abreu
added most of the...
|
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
_divert_(1)')
_define_(`_data_', `_divert_(1)_define_(`_data_text_', __data__)')
_define_(`_text_', `_divert_(2)_define_(`_data_text_', __text__)')
_define_(`_dumpdata_', `
// == Global data =============================================================
#define W * 4
#define KB * 1024
#define MB KB KB
#define DATA_SZ 1 MB
#define HEAP_SZ 1 MB
#define STACK_SZ 1 MB
#define MEM_SZ ((DATA_SZ) + (HEAP_SZ) + (STACK_SZ))
union {
|
fb3afe45
Salvador Abreu
doing 64bits now...
|
141
|
long mem[0];
|
f2c4dc81
Salvador Abreu
added most of the...
|
142
143
144
145
|
struct {_dnl_
_undivert_(1)_dnl_
} named;
struct {
|
fb3afe45
Salvador Abreu
doing 64bits now...
|
146
147
148
|
long z_data[DATA_SZ];
long z_heap[HEAP_SZ];
long z_stack[STACK_SZ];
|
f2c4dc81
Salvador Abreu
added most of the...
|
149
150
151
152
153
154
155
156
|
} zone;
} global;
#define M global.mem
#define DATA global.zone.z_data
#define HEAP global.zone.z_heap
#define STACK global.zone.z_stack
|
fb3afe45
Salvador Abreu
doing 64bits now...
|
157
158
159
|
long SP = MEM_SZ;
long FP = 0;
long SR;
|
f2c4dc81
Salvador Abreu
added most of the...
|
160
161
162
163
164
165
166
167
168
169
170
|
void *PC; // (unused)
// -- Names for global variables ----------------------------------------------
_undivert_(3)_dnl_
')
_define_(`_dumptext_', `_dnl_
// == Program =================================================================
int main (int argc, char *argv[])
{
|
fb3afe45
Salvador Abreu
doing 64bits now...
|
171
|
long i;
|
f2c4dc81
Salvador Abreu
added most of the...
|
172
173
174
175
176
177
178
179
180
|
// -- Initialization ----------------------------------------------------------
_undivert_(4)_dnl_
// -- Library -----------------------------------------------------------------
_include_(LIBDIR/library.m4)_dnl_
// -- Start execution ---------------------------------------------------------
|
fb3afe45
Salvador Abreu
doing 64bits now...
|
181
|
M[--SP] = (long) &&L_exit_program; // Save return address for main program
|
f2c4dc81
Salvador Abreu
added most of the...
|
182
183
184
185
186
187
188
189
190
191
|
goto program; // start kicking...
L_exit_program: // Return here, and...
exit (0); // quit.
// -- Instructions ------------------------------------------------------------_dnl_
_undivert_(2)
}
')
_divert_`'_dnl_
|