This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 569
/
binaryen.diff
193 lines (182 loc) · 5.44 KB
/
binaryen.diff
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
diff --git a/src/s2wasm.h b/src/s2wasm.h
index 0fc0201c..c56a3a8c 100644
--- a/src/s2wasm.h
+++ b/src/s2wasm.h
@@ -153,9 +153,16 @@ class S2WasmBuilder {
Name getStr() {
std::string str; // TODO: optimize this and the other get* methods
- while (*s && !isspace(*s)) {
- str += *s;
- s++;
+ if (*s == '\"') {
+ str += *s++;
+ while (*s && *s != '\"') {
+ str += *s++;
+ }
+ if (*s == '\"') str += *s++;
+ } else {
+ while (*s && !isspace(*s)) {
+ str += *s++;
+ }
}
return cashew::IString(str.c_str(), false);
}
@@ -168,6 +175,11 @@ class S2WasmBuilder {
Name getStrToSep() {
std::string str;
+ if (*s == '\"') {
+ str += *s++;
+ while (*s && *s != '\"') str += *s++;
+ if (*s == '\"') str += *s++;
+ }
while (*s && !isspace(*s) && *s != ',' && *s != '(' && *s != ')' && *s != ':' && *s != '+' && *s != '-' && *s != '=') {
str += *s;
s++;
@@ -177,10 +189,15 @@ class S2WasmBuilder {
Name getStrToColon() {
std::string str;
- while (*s && !isspace(*s) && *s != ':') {
- str += *s;
- s++;
+ if (*s == '\"') {
+ str += *s++;
+ while (*s && *s != '\"') {
+ str += *s++;
+ }
+ if (*s == '\"') str += *s++;
}
+ while (*s && !isspace(*s) && *s != ':')
+ str += *s++;
return cashew::IString(str.c_str(), false);
}
@@ -330,9 +347,18 @@ class S2WasmBuilder {
Name getSeparated(char separator) {
skipWhitespace();
std::string str;
- while (*s && *s != separator && *s != '\n') {
- str += *s;
- s++;
+ bool inQuoted = false;
+ int inBrackets = 0;
+ while (*s && (*s != separator || inQuoted || inBrackets > 0) && *s != '\n') {
+ if (*s == '\"') {
+ inQuoted = !inQuoted;
+ } else if (*s == '(') {
+ inBrackets++;
+ } else if (*s == ')') {
+ inBrackets--;
+ }
+
+ str += *s++;
}
skipWhitespace();
return cashew::IString(str.c_str(), false);
@@ -421,14 +447,32 @@ class S2WasmBuilder {
bool isFunctionName(Name name) {
return !!strstr(name.str, "@FUNCTION");
}
- // Drop the @ and after it.
- Name cleanFunction(Name name) {
- if (!strchr(name.str, '@')) return name;
+
+ Name cleanName(Name name, char terminator = 0) {
char *temp = strdup(name.str);
- *strchr(temp, '@') = 0;
+ static const char* bads = "\";<>() ";
+ static const char* goods = "\'$__$$$";
+ int index = 0;
+ while (bads[index] != 0) {
+ char *pos = temp;
+ while ((pos = strchr(pos, bads[index])) != nullptr) {
+ *pos = goods[index];
+ pos++;
+ }
+ index++;
+ }
+ if (terminator != 0) {
+ char* pos = strrchr(temp, terminator);
+ if (pos != nullptr) *pos = '\0';
+ }
Name ret = cashew::IString(temp, false);
free(temp);
return ret;
+ }
+
+ // Drop the @ and after it.
+ Name cleanFunction(Name name) {
+ return cleanName(name, '@');
}
// processors
@@ -446,7 +490,7 @@ class S2WasmBuilder {
if (match(".hidden")) mustMatch(name.str);
mustMatch(name.str);
if (match(":")) {
- info->implementedFunctions.insert(name);
+ info->implementedFunctions.insert(cleanName(name));
} else if (match("=")) {
Name alias = getAtSeparated();
mustMatch("@FUNCTION");
@@ -461,7 +505,7 @@ class S2WasmBuilder {
s = strchr(s, '\n');
} else {
// add data aliases
- Name lhs = getStrToSep();
+ Name lhs = cleanName(getStrToSep());
// When the current line contains only one word, e.g.".text"
if (match("\n"))
continue;
@@ -473,7 +517,7 @@ class S2WasmBuilder {
}
// get the original name
- Name rhs = getStrToSep();
+ Name rhs = cleanName(getStrToSep());
assert(!isFunctionName(rhs));
Offset offset = 0;
if (*s == '+') {
@@ -620,7 +664,7 @@ class S2WasmBuilder {
}
void parseGlobl() {
- linkerObj->addGlobal(getStr());
+ linkerObj->addGlobal(cleanName(getStr()));
skipWhitespace();
}
@@ -753,7 +797,7 @@ class S2WasmBuilder {
skipWhitespace();
} else break;
}
- Function* func = builder.makeFunction(name, std::move(params), resultType, std::move(vars));
+ Function* func = builder.makeFunction(cleanName(name), std::move(params), resultType, std::move(vars));
// parse body
func->body = allocator->alloc<Block>();
@@ -1343,6 +1387,11 @@ class S2WasmBuilder {
mustMatch(name.str);
skipWhitespace();
}
+ if (match(".weak")) {
+ // Do nothing special on weak symbols so far.
+ mustMatch(name.str);
+ skipWhitespace();
+ }
if (match(".align") || match(".p2align")) {
align = getInt();
skipWhitespace();
@@ -1436,9 +1485,10 @@ class S2WasmBuilder {
r->data = (uint32_t*)&raw[i];
}
// assign the address, add to memory
- linkerObj->addStatic(size, align, name);
+ Name wasmName = cleanName(name);
+ linkerObj->addStatic(size, align, wasmName);
if (!zero) {
- linkerObj->addSegment(name, raw);
+ linkerObj->addSegment(wasmName, raw);
}
}
@@ -1451,7 +1501,7 @@ class S2WasmBuilder {
skipComma();
localAlign = 1 << getInt();
}
- linkerObj->addStatic(size, std::max(align, localAlign), name);
+ linkerObj->addStatic(size, std::max(align, localAlign), cleanName(name));
}
void skipImports() {