commit 0b5080ef495991eb73e8d946285a14d1ca143409
parent a9ca038e47093dad4d9961bcd5f2e209635dbb79
Author: Roberto E. Vargas Caballero <Roberto E. Vargas Caballero>
Date: Thu, 21 Apr 2016 03:15:28 +0200
[cc2-qbe] Avoid name collision in local variables
C is a block language, and it means we can have variables with the
same name in different blocks. For this reason we have to append
an identifier for every local variable and in this way we avoid
possible name clashes.
Diffstat:
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/cc2/arch/qbe/code.c b/cc2/arch/qbe/code.c
@@ -7,7 +7,7 @@
#include "../../cc2.h"
#include "../../../inc/sizes.h"
-#define ADDR_LEN (IDENTSIZ+2)
+#define ADDR_LEN (IDENTSIZ+64)
static void binary(void), load(void), store(void);
@@ -118,17 +118,19 @@ sigil(Symbol *sym)
static char *
symname(Symbol *sym)
{
- static char name[IDENTSIZ+2];
- static unsigned short id;
+ static char name[ADDR_LEN];
+ static unsigned id;
char c = sigil(sym);
if (sym->name) {
switch (sym->kind) {
case SEXTRN:
case SGLOB:
+ sprintf(name, "%c%s", c, sym->name);
+ return name;
case SPRIV:
case SAUTO:
- sprintf(name, "%c%s", c, sym->name);
+ sprintf(name, "%c%s.%u", c, sym->name, sym->id);
return name;
default:
abort();
@@ -137,7 +139,7 @@ symname(Symbol *sym)
if (sym->numid == 0 && (sym->numid = ++id) == 0)
error(EIDOVER);
- sprintf(name, "%c.%d", c, sym->numid);
+ sprintf(name, "%c.%u", c, sym->numid);
return name;
}