ref: 408728300a8203681bfe8ee19ef0cf8cba8ee849
parent: d6e214cc6f13349e7b0b9d07a90298400287460e
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Apr 20 23:15:28 EDT 2016
[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.
--- 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,8 +118,8 @@
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) {
@@ -126,9 +126,11 @@
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 @@
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;
}