[hackers] [scc] [cc2] Add generic support for builtins in cc2 || Roberto E. Vargas Caballero
commit bf2c6e4d7f1da97151ef509b3fd818b30298280d
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Sat Feb 11 07:53:01 2017 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Sat Feb 11 11:19:57 2017 +0100
[cc2] Add generic support for builtins in cc2
This patch only adds the support in the parser
diff --git a/cc1/code.c b/cc1/code.c
index 769e897..0b24c6a 100644
--- a/cc1/code.c
+++ b/cc1/code.c
_AT_@ -444,7 +444,7 @@ emitbuilt(unsigned op, void *arg)
emitnode(np->left);
emitnode(np->right);
- fprintf(outfp, "\t\"%s\tk", np->sym->name);
+ fprintf(outfp, "\t\"%s\tm", np->sym->name);
emitletter(np->type);
}
diff --git a/cc2/arch/amd64-sysv/types.c b/cc2/arch/amd64-sysv/types.c
index 5d8d81b..1abef83 100644
--- a/cc2/arch/amd64-sysv/types.c
+++ b/cc2/arch/amd64-sysv/types.c
_AT_@ -92,3 +92,8 @@ Type elipsistype = {
.size = 0,
.align = 0
};
+
+Type arg_type = {
+ .size = 24,
+ .align = 8
+};
diff --git a/cc2/arch/i386-sysv/types.c b/cc2/arch/i386-sysv/types.c
index 2050f22..2a448e8 100644
--- a/cc2/arch/i386-sysv/types.c
+++ b/cc2/arch/i386-sysv/types.c
_AT_@ -92,3 +92,9 @@ Type elipsistype = {
.size = 0,
.align = 0
};
+
+/* this type is not used in this architecture */
+Type arg_type = {
+ .size = 0,
+ .align = 0
+};
diff --git a/cc2/arch/qbe/types.c b/cc2/arch/qbe/types.c
index 66ee84d..dcd6f20 100644
--- a/cc2/arch/qbe/types.c
+++ b/cc2/arch/qbe/types.c
_AT_@ -92,3 +92,8 @@ Type elipsistype = {
.size = 0,
.align = 0
};
+
+Type arg_type = {
+ .size = 24,
+ .align = 8
+};
diff --git a/cc2/arch/z80/types.c b/cc2/arch/z80/types.c
index 7238c6f..06b8eb7 100644
--- a/cc2/arch/z80/types.c
+++ b/cc2/arch/z80/types.c
_AT_@ -92,3 +92,9 @@ Type elipsistype = {
.size = 0,
.align = 0
};
+
+/* this types is not going to be used in this arch */
+Type arg_type = {
+ .size = 0,
+ .align = 0
+};
diff --git a/cc2/cc2.h b/cc2/cc2.h
index 4466b91..5a7d2ba 100644
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
_AT_@ -97,6 +97,7 @@ enum op {
OCAST = 'g',
OINC = 'i',
ODEC = 'd',
+ OBUILTIN = 'm',
/*statements */
ONOP = 'q',
OJMP = 'j',
_AT_@ -112,6 +113,13 @@ enum op {
OEFUN = 'k',
};
+enum builtins {
+ BVA_START = 's',
+ BVA_END = 'e',
+ BVA_ARG = 'a',
+ BVA_COPY = 'c',
+};
+
enum nerrors {
EEOFFUN, /* EOF while parsing function */
ENLABEL, /* label without statement */
_AT_@ -128,6 +136,7 @@ enum nerrors {
EWTACKO, /* switch stack overflow */
EWTACKU, /* switch stack underflow */
ENOSWTC, /* Out of switch statement */
+ EBBUILT, /* Unknown builtin */
ENUMERR
};
diff --git a/cc2/parser.c b/cc2/parser.c
index 2b8b8e3..301a619 100644
--- a/cc2/parser.c
+++ b/cc2/parser.c
_AT_@ -19,7 +19,8 @@ extern Type int8type, int16type, int32type, int64type,
booltype,
ptrtype,
voidtype,
- elipsistype;
+ elipsistype,
+ arg_type;
Type funtype = {
.flags = FUNF
_AT_@ -43,7 +44,7 @@ typedef void parsefun(char *, union tokenop);
static parsefun type, symbol, getname, unary, binary, ternary, call,
constant, composed, binit, einit,
jump, oreturn, loop, assign,
- ocase, bswitch, eswitch;
+ ocase, bswitch, eswitch, builtin;
typedef void evalfun(void);
static evalfun vardecl, beginfun, endfun, endpars, stmt,
_AT_@ -78,6 +79,7 @@ static struct decoc {
['B'] = { NULL, type, .u.arg = &booltype},
['P'] = { NULL, type, .u.arg = &ptrtype},
['E'] = { NULL, type, .u.arg = &elipsistype},
+ ['1'] = { NULL, type, .u.arg = &arg_type},
['F'] = { NULL, type, .u.arg = &funtype},
['V'] = { array,composed, 0},
_AT_@ -120,6 +122,7 @@ static struct decoc {
['|'] = { NULL, binary, .u.op = OBOR},
['^'] = { NULL, binary, .u.op = OBXOR},
[','] = { NULL, binary, .u.op = OCOMMA},
+ ['m'] = { NULL, builtin,.u.op = OBUILTIN},
[':'] = { NULL, assign, .u.op = OASSIG},
['?'] = { NULL, ternary, .u.op = OASK},
_AT_@ -462,6 +465,40 @@ call(char *token, union tokenop u)
}
static void
+builtin(char *token, union tokenop u)
+{
+ Node *np = newnode(u.op);
+ char *name;
+ unsigned subop, nchilds;
+
+ np->type = *gettype(token+1);
+ name = pop();
+
+ if (!strcmp("__builtin_va_arg", name)) {
+ nchilds = 1;
+ subop = BVA_ARG;
+ } else if (!strcmp("__builtin_va_start", name)) {
+ nchilds = 2;
+ subop = BVA_START;
+ } else if (!strcmp("__builtin_va_end", name)) {
+ nchilds = 1;
+ subop = BVA_END;
+ } else if (!strcmp("__builtin_va_copy", name)) {
+ nchilds = 2;
+ subop = BVA_COPY;
+ } else {
+ error(EBBUILT);;
+ }
+
+ np->u.subop = subop;
+ np->right = (nchilds == 2) ? pop() : NULL;
+ np->left = (nchilds != 0) ? pop() : NULL;
+
+ free(name);
+ push(np);
+}
+
+static void
binary(char *token, union tokenop u)
{
Node *np = newnode(u.op);
Received on Sat Feb 11 2017 - 11:20:56 CET
This archive was generated by hypermail 2.3.0
: Sat Feb 11 2017 - 11:24:17 CET