[hackers] [scc] [cc2-qbe] Add return statements to qbe || Roberto E. Vargas Caballero
commit aeb22ab4157f5e6f61991b8c1205e65960f52ba8
Author: Roberto E. Vargas Caballero <Roberto E. Vargas Caballero>
AuthorDate: Wed May 4 20:25:50 2016 +0200
Commit: Roberto E. Vargas Caballero <Roberto E. Vargas Caballero>
CommitDate: Wed May 4 20:25:50 2016 +0200
[cc2-qbe] Add return statements to qbe
We need return statements because qne doesn't support orphan labels
at the end of functions, so we have to add return statements in these
cases.
diff --git a/cc2/arch/qbe/arch.h b/cc2/arch/qbe/arch.h
index 00a2eec..9a1bbdb 100644
--- a/cc2/arch/qbe/arch.h
+++ b/cc2/arch/qbe/arch.h
_AT_@ -130,5 +130,6 @@ enum asmop {
ASEXTS,
ASTRUNCD,
- ASJMP
+ ASJMP,
+ ASRET,
};
diff --git a/cc2/arch/qbe/cgen.c b/cc2/arch/qbe/cgen.c
index 6d465be..70ba9d6 100644
--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
_AT_@ -225,7 +225,15 @@ cgen(Node *np)
if (!np)
return NULL;
- setlabel(np->label);
+ if (np->label) {
+ setlabel(np->label);
+ if (np->next == NULL) {
+ Node *tmp = newnode();
+ tmp->op = ORET;
+ addstmt(tmp);
+ prevstmt();
+ }
+ }
l = cgen(np->left);
r = cgen(np->right);
tp = &np->type;
_AT_@ -333,6 +341,10 @@ cgen(Node *np)
code(ASJMP, np, NULL, NULL);
return NULL;
case ORET:
+ if (l && (l->flags & (ISTMP|ISCONS)) == 0)
+ l = np->left = load(l);
+ code(ASRET, l, NULL, NULL);
+ return NULL;
case OCASE:
case ODEFAULT:
case OTABLE:
diff --git a/cc2/arch/qbe/code.c b/cc2/arch/qbe/code.c
index 02d0226..92828b3 100644
--- a/cc2/arch/qbe/code.c
+++ b/cc2/arch/qbe/code.c
_AT_@ -9,7 +9,7 @@
#define ADDR_LEN (IDENTSIZ+64)
-static void binary(void), unary(void), store(void), jmp(void);
+static void binary(void), unary(void), store(void), jmp(void), ret(void);
static struct opdata {
void (*fun)(void);
_AT_@ -123,6 +123,7 @@ static struct opdata {
[ASSLTOS]= {.fun = unary, .txt = "truncd", .letter = 's'},
[ASJMP] = {.fun = jmp},
+ [ASRET] = {.fun = ret},
};
static char buff[ADDR_LEN];
_AT_@ -391,6 +392,15 @@ unary(void)
}
static void
+ret(void)
+{
+ if (pc->from1.kind == SNONE)
+ puts("\t\tret");
+ else
+ printf("\t\tret\t%s\n", addr2txt(&pc->from1));
+}
+
+static void
jmp(void)
{
printf("\t\tjmp\t%s\n", addr2txt(&pc->to));
diff --git a/cc2/cc2.h b/cc2/cc2.h
index 9cb5c14..5ca106e 100644
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
_AT_@ -210,6 +210,8 @@ extern void delnode(Node *np);
extern void deltree(Node *np);
extern Node *newnode(void);
extern Node *addstmt(Node *np);
+extern Node *prevstmt(void), *nextstmt(void);
+
/* symbol.c */
#define TMPSYM 0
diff --git a/cc2/node.c b/cc2/node.c
index 96c3e90..56b64fd 100644
--- a/cc2/node.c
+++ b/cc2/node.c
_AT_@ -83,6 +83,12 @@ nextstmt(void)
return curstmt = curstmt->next;
}
+Node *
+prevstmt(void)
+{
+ return curstmt = curstmt->prev;
+}
+
void
delnode(Node *np)
{
Received on Fri May 06 2016 - 18:25:54 CEST
This archive was generated by hypermail 2.3.0
: Fri May 06 2016 - 18:36:22 CEST