forked from begoon/smallc-85
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.c
195 lines (185 loc) · 4.78 KB
/
function.c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* File function.c: 2.1 (83/03/20,16:02:04)
*/
#include <stdio.h>
#include "defs.h"
#include "data.h"
int argtop;
/**
* begin a function
* called from "parse", this routine tries to make a function out
* of what follows
* modified version. p.l. woods
*/
newfunc() {
char n[NAMESIZE];
int idx, type;
fexitlab = getlabel();
if (!symname(n)) {
error("illegal function or declaration");
kill();
return;
}
if (idx = findglb(n)) {
if (symbol_table[idx].identity != FUNCTION)
multidef(n);
else if (symbol_table[idx].offset == FUNCTION)
multidef(n);
else
symbol_table[idx].offset = FUNCTION;
} else
add_global(n, FUNCTION, CINT, FUNCTION, PUBLIC);
if (!match("("))
error("missing open paren");
output_string(n);
output_label_terminator();
newline();
local_table_index = NUMBER_OF_GLOBALS; //locptr = STARTLOC;
argstk = 0;
// ANSI style argument declaration
if (doAnsiArguments() == 0) {
// K&R style argument declaration
while (!match(")")) {
if (symname(n)) {
if (findloc(n))
multidef(n);
else {
add_local(n, 0, 0, argstk, AUTO);
argstk = argstk + INTSIZE;
}
} else {
error("illegal argument name");
junk();
}
blanks();
if (!streq(line + lptr, ")")) {
if (!match(","))
error("expected comma");
}
if (endst())
break;
}
stkp = 0;
argtop = argstk;
while (argstk) {
if (type = get_type()) {
getarg(type);
need_semicolon();
} else {
error("wrong number args");
break;
}
}
}
statement(YES);
print_label(fexitlab);
output_label_terminator();
newline();
gen_modify_stack(0);
gen_ret();
stkp = 0;
local_table_index = NUMBER_OF_GLOBALS; //locptr = STARTLOC;
}
/**
* declare argument types
* called from "newfunc", this routine adds an entry in the local
* symbol table for each named argument
* completely rewritten version. p.l. woods
* @param t argument type (char, int)
* @return
*/
getarg(int t) {
int j, legalname, address, argptr;
char n[NAMESIZE];
FOREVER
{
if (argstk == 0)
return;
if (match("*"))
j = POINTER;
else
j = VARIABLE;
if (!(legalname = symname(n)))
illname();
if (match("[")) {
while (inbyte() != ']')
if (endst())
break;
j = POINTER;
}
if (legalname) {
if (argptr = findloc(n)) {
symbol_table[argptr].identity = j;
symbol_table[argptr].type = t;
address = argtop - symbol_table[argptr].offset;
symbol_table[argptr].offset = address;
} else
error("expecting argument name");
}
argstk = argstk - INTSIZE;
if (endst())
return;
if (!match(","))
error("expected comma");
}
}
doAnsiArguments() {
int type;
type = get_type();
if (type == 0) {
return 0; // no type detected, revert back to K&R style
}
argtop = argstk;
argstk = 0;
FOREVER
{
if (type) {
doLocalAnsiArgument(type);
} else {
error("wrong number args");
break;
}
if (match(",")) {
type = get_type();
continue;
}
if (match(")")) {
break;
}
}
}
doLocalAnsiArgument(int type) {
char symbol_name[NAMESIZE];
int identity, address, argptr, ptr;
if (match("*")) {
identity = POINTER;
} else {
identity = VARIABLE;
}
if (symname(symbol_name)) {
if (findloc(symbol_name)) {
multidef(symbol_name);
} else {
argptr = add_local (symbol_name, identity, type, 0, AUTO);
argstk = argstk + INTSIZE;
ptr = local_table_index;
while (ptr != NUMBER_OF_GLOBALS) { // modify stack offset as we push more params
ptr = ptr - 1;
address = symbol_table[ptr].offset;
symbol_table[ptr].offset = address + INTSIZE;
}
}
} else {
error("illegal argument name");
junk();
}
if (match("[")) {
while (inbyte() != ']') {
if (endst()) {
break;
}
}
identity = POINTER;
symbol_table[argptr].identity = identity;
}
}