forked from sheisc/COMP9024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecl.c
More file actions
81 lines (66 loc) · 1.97 KB
/
decl.c
File metadata and controls
81 lines (66 loc) · 1.97 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*************************************************************
* Declaration
* long id;
*
*************************************************************/
#include "decl.h"
#include "emit.h"
#include "error.h"
#include "expr.h"
#include "lex.h"
#include "stmt.h"
#include <stdio.h>
/*
Declaration:
long id;
To declare a global variable or a local variable.
*/
AstExprNodePtr Declaration(void) {
AstExprNodePtr dec = NULL;
Expect(TK_INT);
if (curToken.kind == TK_ID) {
// FIXME: the default type is integer
dec = CreateAstExprNode(TK_ID, &curToken.value, NULL, NULL);
NEXT_TOKEN;
} else {
Expect(TK_ID);
}
return dec;
}
static void OutputGlobalVarInfo(AstExprNodePtr globalVar) {
#define SYMBOL_TABLE_OUTPUT_FMT "# %12s %s"
EmitComments("\n");
EmitComments("# ----------------------------");
EmitComments(SYMBOL_TABLE_OUTPUT_FMT, "AccessName", "Global Var");
EmitComments("# ----------------------------");
EmitComments(SYMBOL_TABLE_OUTPUT_FMT,
GetNodeNameInAssembly(globalVar),
GetNodeNameInIR(globalVar));
EmitComments("# ----------------------------");
#undef SYMBOL_TABLE_OUTPUT_FMT
EmitComments("\n");
}
/*
Generate assembly code for a global variable (only supporting long integers).
Put it in the data section (global area) in the assembly file.
Example:
long year;
Assembly:
.data
.comm year, 8
------------------------------------------------------------------------------
.comm
Declares a common memory area for data that is not initialized.
For a global variable.
.lcomm
Declares a local common memory area for data that is not initialized.
For a static variable.
Syntax
.comm symbol, length
*/
void EmitGlobalDeclarationNode(AstExprNodePtr pNode) {
if (pNode && pNode->op == TK_ID) {
OutputGlobalVarInfo(pNode);
EmitAssembly(".comm %s, %d", GetNodeNameInAssembly(pNode), CPU_WORD_SIZE_IN_BYTES);
}
}