ref: 9c45c2b5f383585dedcede18b0d0cac97b27ab1c
parent: daa27ec5c80b95ef9b0081d273142e96255ac3cf
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Sep 10 16:21:48 EDT 2014
Add parameter function This function allows to split variables between locals and parameters, because they compute different offsets in the sctack, so we can calculate the offset in each call to locals or parameters.
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -45,6 +45,7 @@
enum nerrors {
EINTNUM, /* too much internal identifiers */
EEXTNUM, /* too much external identifiers */
+ EPARNUM, /* too much parameters */
ENODEOV, /* node overflow */
ESTACKO, /* stack overflow */
ESTACKU, /* stack underflow */
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -15,8 +15,9 @@
#define NR_NODEPOOL 128
#define NR_EXPRESSIONS 64
-#define LOCAL 0
-#define GLOBAL 1
+enum {
+ LOCAL, GLOBAL, PARAMETER
+};
static Symbol *curfun;
static Node *stack[NR_STACKSIZ], **stackp = stack;
@@ -23,8 +24,6 @@
static Node *listexp[NR_EXPRESSIONS], **listp = listexp;
static Node nodepool[NR_NODEPOOL], *newp = nodepool;
-static Symbol *localtbl;
-static Symbol *globaltbl;
Type l_int8 = {
.size = 1,
@@ -79,8 +78,25 @@
};
static Symbol *
+parameter(char *num)
+{
+ static Symbol *tbl;
+ unsigned i = atoi(num+1);
+ static unsigned nr;
+
+ if (i >= NR_FUNPARAM)
+ error(EPARNUM);
+ if (i > nr) {
+ nr = i + 50;
+ tbl = xrealloc(tbl, nr * sizeof(Symbol));
+ }
+ return &tbl[i];
+}
+
+static Symbol *
local(char *num)
{
+ static Symbol *tbl;
unsigned i = atoi(num+1);
static unsigned nr;
@@ -88,14 +104,15 @@
error(EINTNUM);
if (i > nr) {
nr = i + 50;
- localtbl = xrealloc(localtbl, nr * sizeof(Symbol));
+ tbl = xrealloc(tbl, nr * sizeof(Symbol));
}
- return &localtbl[i];
+ return &tbl[i];
}
static Symbol *
global(char *num)
{
+ static Symbol *tbl;
unsigned i = atoi(num+1);
static unsigned nr;
@@ -103,9 +120,9 @@
error(EEXTNUM);
if (i >= nr) {
nr = i + 50;
- globaltbl = xrealloc(globaltbl, nr * sizeof(Symbol));
+ tbl = xrealloc(tbl, nr * sizeof(Symbol));
}
- return &globaltbl[i];
+ return &tbl[i];
}
static Node *
@@ -366,8 +383,10 @@
static Symbol *
declaration(uint8_t t, char *token)
{
- static Symbol *(*tbl[2])(char *)= {
- [LOCAL] = local, [GLOBAL] = global
+ static Symbol *(*tbl[3])(char *)= {
+ [LOCAL] = local,
+ [GLOBAL] = global,
+ [PARAMETER] = parameter
};
Symbol *sym = (*tbl[t])(token);
char *s;
@@ -420,7 +439,7 @@
static void
paramdcl(char *token)
{
- Symbol *sym = declaration(LOCAL, token);
+ Symbol *sym = declaration(PARAMETER, token);
sym->next = curfun->u.f.pars;
curfun->u.f.pars = sym;
}