/
function.c
146 lines (141 loc) · 2.58 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
/* File function.c: 2.1 (83/03/20,16:02:04) */
/*% cc -O -c %
*
*/
#include <stdio.h>
#include "defs.h"
#include "data.h"
/*
* begin a function
*
* called from "parse", this routine tries to make a function out
* of what follows
* modified version. p.l. woods
*
*/
int argtop;
newfunc ()
{
char n[NAMESIZE], *ptr;
fexitlab = getlabel();
if (!symname (n) ) {
error ("illegal function or declaration");
kill ();
return;
}
if (ptr = findglb (n)) {
if (ptr[IDENT] != FUNCTION)
multidef (n);
else if (ptr[OFFSET] == FUNCTION)
multidef (n);
else
ptr[OFFSET] = FUNCTION;
} else
addglb (n, FUNCTION, CINT, FUNCTION, PUBLIC);
prologue ();
if (!match ("("))
error ("missing open paren");
prefix ();
outstr (n);
col ();
nl ();
locptr = STARTLOC;
argstk = 0;
while (!match (")")) {
if (symname (n)) {
if (findloc (n))
multidef (n);
else {
addloc (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 (amatch ("register", 8)) {
if (amatch("char", 4))
getarg(CCHAR);
else if (amatch ("int", 3))
getarg(CINT);
else
getarg(CINT);
ns();
} else if (amatch ("char", 4)) {
getarg (CCHAR);
ns ();
} else if (amatch ("int", 3)) {
getarg (CINT);
ns ();
} else {
error ("wrong number args");
break;
}
}
statement(YES);
printlabel(fexitlab);
col();
nl();
modstk (0);
gret ();
stkp = 0;
locptr = STARTLOC;
}
/*
* declare argument types
*
* called from "newfunc", this routine add an entry in the local
* symbol table for each named argument
* completely rewritten version. p.l. woods
*
*/
getarg (t)
int t;
{
int j, legalname, address;
char n[NAMESIZE], c, *argptr;
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)) {
argptr[IDENT] = j;
argptr[TYPE] = t;
address = argtop - glint(argptr);
if (t == CCHAR && j == VARIABLE)
address = address + byteoff();
argptr[OFFSET] = (address) & 0xff;
argptr[OFFSET + 1] = (address >> 8) & 0xff;
} else
error ("expecting argument name");
}
argstk = argstk - intsize();
if (endst ())
return;
if (!match (","))
error ("expected comma");
}
}