-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclang-wrapper
executable file
·255 lines (203 loc) · 8.57 KB
/
clang-wrapper
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/python
import sys
import os
import subprocess
import tempfile
import pathlib
import re
whiteSpaceCharacters = [' ', '\t']
class AssemblyToC:
__isMultilineComment = False
__symbolRenameList = {}
def __toC(self, line, labelReplacements):
if len(line.strip()) <= 0:
return line
for start, end, suffix in sorted(labelReplacements, key=lambda r: r[0], reverse=True):
line = line[:start] + self.__symbolRenameList[line[start:end]][0] + suffix + line[end:]
return 'asm("' + line.replace('\\', '\\\\').replace('"', '\\"') + '");'
def __call__(self, line):
result = ""
linePartStart = 0
labelReplacements = []
foundNonWhiteCharacters = self.__isMultilineComment
for c, i in zip(line, range(len(line))):
if self.__isMultilineComment:
if c == '/':
if 0 < i and line[i-1] == '*':
self.__isMultilineComment = False
result = result + line[linePartStart:i+1]
linePartStart = i+1
labelReplacements.clear()
continue
if not foundNonWhiteCharacters:
if c == '#':
return line
if c == '@':
return line[:i] + '//' + line[i+1:]
if c not in whiteSpaceCharacters:
foundNonWhiteCharacters = True
if c == '/' and 0 < i:
if line[i-1] == '/' and not self.__isMultilineComment:
return result + self.__toC(line[linePartStart:i-1], labelReplacements) + line[i-1:]
if c == '*' and 0 < i:
if line[i-1] == '/':
self.__isMultilineComment = True
result = result + self.__toC(line[linePartStart:i-1], labelReplacements)
linePartStart = i-1
labelReplacements.clear()
if c == '.' and not self.__isMultilineComment:
isDotLabel = True
if line[i+1] == 'L' and (i == 0 or line[i-1] in whiteSpaceCharacters):
# Assume private label
nameLength = 2
for nextChr in line[i+2:]:
if nextChr in [':', ',', ';'] or nextChr in whiteSpaceCharacters:
break
nameLength = nameLength + 1
name = line[i:i+nameLength]
if name not in self.__symbolRenameList:
self.__symbolRenameList[name] = (str(141879 + len(self.__symbolRenameList)), False)
if i+nameLength < len(line):
isDefenition = False
for nextChr in line[i+nameLength:]:
if nextChr == ':':
isDefenition = True
if nextChr not in whiteSpaceCharacters:
break
if isDefenition:
self.__symbolRenameList[name] = (self.__symbolRenameList[name][0], True)
labelReplacements.append((i, i+nameLength, ''))
else:
labelReplacements.append((i, i+nameLength, 'b' if self.__symbolRenameList[name][1] else 'f'))
else:
labelReplacements.append((i, i+nameLength, 'b' if self.__symbolRenameList[name][1] else 'f'))
remainder = line[linePartStart:]
result = result + (remainder if self.__isMultilineComment else self.__toC(remainder, labelReplacements))
return result
def setOutputFile(arguments, filename):
ind = arguments.index('-o') + 1
while ind < len(arguments):
a = arguments[ind].strip()
if a[0] != '-':
arguments[ind] = filename
break
ind = ind + 1
return arguments
def removeOutputFile(arguments):
result = list(arguments)
removeList = []
for arg, ind in zip(result, range(len(result))):
if arg in ['-o', '-MT', '-MQ', '-MF']:
removeList.append(ind)
removeList.append(ind+1)
for ind in sorted(removeList, reverse=True):
result.pop(ind)
return result
def detectVerbosityLevel(arguments):
verbose = 0
for arg in arguments:
if arg == '--verbose':
verbose = verbose + 1
if arg[0] == '-' and all(c == 'v' for c in arg[1:]):
verbose = verbose + (len(arg) - 1)
return verbose
def detectInputFilenames(arguments):
filenames = []
for arg in reversed(arguments):
if arg[0] == '-':
if arg == '-o':
filenames.pop()
break
filenames.append(arg)
return filenames
def replaceFilenames(arguments, filenameReplaceList):
lastOriginal = None
for i in reversed(range(len(arguments))):
arg = arguments[i]
if arg[0] == '-':
if arg == '-o' and lastOriginal is not None:
arguments[i+1] = lastOriginal
break
original, replacement = next(([orig, repl] for orig, repl in filenameReplaceList if arg == orig), (None, None))
if replacement is not None:
arguments[i] = replacement
lastOriginal = replacement
return arguments
def includeGnuExtensions(arguments):
result = list(arguments)
for arg, ind in zip(result, range(len(result))):
if arg.startswith("-std="):
std = arg[5:]
if std[0] == 'c':
result[ind] = '-std=gnu' + std[1:]
elif std[0:7] == 'iso9899':
version = std[7:]
versions= {'1990': 'gnu90', '199409': 'gnu90', '1999': 'gnu99', '2011': 'gnu11', '2017': 'gnu17'}
if version in versions:
result[ind] = '-std=' + versions[version]
else:
raise "" # TODO: Proper exception
return result
def forceUseLto(arguments):
result = list(arguments)
for arg, ind in zip(result, range(len(result))):
if arg.startswith('-flto'):
return result
return ['-flto=thin'] + result
def getCompilerCmd():
exeName = os.path.basename(sys.argv[0])
m = re.fullmatch("(?P<target>.*-)?(?P<compiler>[^-]+)", exeName)
if m is None:
print(f"Invalid executable name \"{exeName}\"", file=sys.stderr)
exit()
g = m.groupdict()
if 'target' in g:
return [g['compiler'], '-target', g['target'][0:-1]]
else:
return [g['compiler']]
def main():
compCmd = getCompilerCmd()
arguments = sys.argv[1:]
verbose = detectVerbosityLevel(arguments)
inputFilenames = detectInputFilenames(arguments)
filenameReplaceList = []
tmpFiles = []
for inputFilename in inputFilenames:
if pathlib.Path(inputFilename).suffix not in ['.s', '.S']:
continue
out = tempfile.NamedTemporaryFile(mode='w+', suffix='.c')
tmpFiles.append(out)
preprocessCommand = compCmd + ['-E', '-C'] + removeOutputFile(arguments)
if 1 <= verbose:
print(f"Executing preprocessor: {' '.join(preprocessCommand)}...")
sys.stdout.flush()
preprocessorResult = subprocess.run(preprocessCommand, stdout=subprocess.PIPE, text=True)
assemblyToC = AssemblyToC()
out.writelines([assemblyToC(line) + '\n' for line in preprocessorResult.stdout.splitlines()]);
out.flush()
if 2 <= verbose:
s = " --------------------------------------\n"
s = s + " --- File content for " + inputFilename + ":\n"
s = s + " --------------------------------------\n"
out.seek(0)
s = s + out.read() + '\n'
s = s + " --------------------------------------\n"
print(s)
sys.stdout.flush()
filenameReplaceList.append([inputFilename, out.name])
if 0 < len(filenameReplaceList):
c = replaceFilenames(arguments, filenameReplaceList)
c = includeGnuExtensions(c)
# c = forceUseLto(c)
cmd = compCmd + c
else:
cmd = compCmd + arguments
if 1 <= verbose and 0 < len(inputFilenames):
print(f"Executing: {' '.join(cmd)}...")
sys.stdout.flush()
result = subprocess.run(cmd)
for f in tmpFiles:
f.close()
return result.returncode
if __name__ == '__main__':
sys.exit(main())