-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcs_compiler.cpp
More file actions
140 lines (114 loc) · 3.87 KB
/
Copy pathcs_compiler.cpp
File metadata and controls
140 lines (114 loc) · 3.87 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cs_compiler.h"
#include "cc_macrotable.h"
#include "cc_compiledscript.h"
#include "cc_symboltable.h"
#include "cc_error.h"
#include "cc_options.h"
#include "script_common.h"
#include "cs_prepro.h"
#include "cs_parser.h"
const char *ccSoftwareVersion = "1.0";
char**defaultheaders = NULL;
char**defaultHeaderNames = NULL;
static int numheaders=0;
static int capacityHeaders=0;
MacroTable predefinedMacros;
int ccAddDefaultHeader(char* nhead, char *nName)
{
if (numheaders >= capacityHeaders)
{
capacityHeaders += 50;
defaultheaders = (char**)realloc(defaultheaders, sizeof(char*) * capacityHeaders);
defaultHeaderNames = (char**)realloc(defaultHeaderNames, sizeof(char*) * capacityHeaders);
}
defaultheaders[numheaders] = nhead;
defaultHeaderNames[numheaders] = nName;
numheaders++;
return 0;
}
void ccRemoveDefaultHeaders() {
numheaders = 0;
}
void ccDefineMacro(const char *macro, const char *definition) {
predefinedMacros.add((char*)macro, (char*)definition);
}
void ccClearAllMacros() {
predefinedMacros.shutdown();
predefinedMacros.init();
}
void ccSetSoftwareVersion(const char *versionNumber) {
ccSoftwareVersion = versionNumber;
}
ccScript* ccCompileText(const char *texo, const char *scriptName) {
int t;
ccCompiledScript *cctemp = new ccCompiledScript();
cctemp->init();
sym.reset();
preproc_startup(&predefinedMacros);
if (scriptName == NULL)
scriptName = "Main script";
ccError = 0;
ccErrorLine = 0;
for (t=0;t<numheaders;t++) {
if (defaultHeaderNames[t] != NULL)
ccCurScriptName = defaultHeaderNames[t];
else
ccCurScriptName = "Internal header file";
cctemp->start_new_section(ccCurScriptName);
cc_compile(defaultheaders[t],cctemp);
if (ccError) break;
}
if (!ccError) {
ccCurScriptName = scriptName;
cctemp->start_new_section(ccCurScriptName);
cc_compile(texo,cctemp);
}
preproc_shutdown();
if (ccError) {
cctemp->shutdown();
delete cctemp;
return NULL;
}
for (t=0; (size_t)t<sym.entries.size();t++) {
int stype = sym.get_type(t);
// blank out the name for imports that are not used, to save space
// in the output file
if (((sym.entries[t].flags & SFLG_IMPORTED)!=0) && ((sym.entries[t].flags & SFLG_ACCESSED)==0)) {
if ((stype == SYM_FUNCTION) || (stype == SYM_GLOBALVAR)) {
// unused func/variable
cctemp->imports[sym.entries[t].soffs][0] = 0;
}
else if (sym.entries[t].flags & SFLG_PROPERTY) {
// unused property -- get rid of the getter and setter
int propGet = sym.entries[t].get_propget();
int propSet = sym.entries[t].get_propset();
if (propGet >= 0)
cctemp->imports[propGet][0] = 0;
if (propSet >= 0)
cctemp->imports[propSet][0] = 0;
}
}
if ((sym.get_type(t) != SYM_GLOBALVAR) &&
(sym.get_type(t) != SYM_LOCALVAR)) continue;
if (sym.entries[t].flags & SFLG_IMPORTED) continue;
if (ccGetOption(SCOPT_SHOWWARNINGS)==0) ;
else if ((sym.entries[t].flags & SFLG_ACCESSED)==0) {
printf("warning: variable '%s' is never used\n",sym.get_friendly_name(t).c_str());
}
}
if (ccGetOption(SCOPT_EXPORTALL)) {
// export all functions
for (t=0;t<cctemp->numfunctions;t++) {
if (cctemp->add_new_export(cctemp->functions[t],EXPORT_FUNCTION,
cctemp->funccodeoffs[t], cctemp->funcnumparams[t]) == -1) {
cctemp->shutdown();
return NULL;
}
}
}
cctemp->free_extra();
return cctemp;
}