-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathglobals.c
66 lines (57 loc) · 1.33 KB
/
globals.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* quads.c by William Ho */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "globals.h"
#include "quads.h"
#include "symtable.h"
#include "statements.h"
#include "expressions.h"
#include "declarations.h"
#include "y.tab.h"
struct string_lit *strings;
struct global *globals;
int string_counter;
struct generic_node *new_string(char *str) {
// add to list of strings
struct string_lit *s = malloc(sizeof(struct string_lit));
s->num = string_counter++;
s->str = get_escaped_string(str);
s->next = 0;
s->last = s;
strings->last->next = s;
strings->last = s;
// return a node
struct symbol *sym = malloc(sizeof(struct symbol));
char *const_name = malloc(10);
sprintf(const_name,".LC%d",s->num);
sym->id = const_name;
sym->nodetype = N_CONST;
/*printf("string info: %s %s",sym->id,s->str);*/
return (struct generic_node *)sym;
}
struct generic_node *new_global(struct generic_node *n) {
struct global *g = malloc(sizeof(struct global));
g->var = n;
g->next = 0;
g->last = g;
globals->last->next = g;
globals->last = g;
return n;
}
char *get_escaped_string(char *p) {
char *s = malloc(strlen(p)*4);
char *read=p, *write=s;
int i=0, j=0;
char c;
while (c = *read++) {
if (isprint(c)) {
*write++ = c;
}
else {
write += sprintf(write,"\\%03o",(unsigned char)c);
}
}
*write = '\0';
return s;
}