-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_src_project.py
281 lines (233 loc) · 10.3 KB
/
prepare_src_project.py
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
'''
Contains functions to prepare the source project that will execute on the host OS and interact
with the hardware which is generated by prepare_hls_project
build_src_project is the main entry point to this module
@author: ian
'''
import os
from os.path import join
import shutil
from pycparser import c_ast
from string import Template
import astcache
import flowanalysis
from juniperrewrites import c_filename_of_java_method_sig, \
c_decl_node_of_java_sig, get_line_bounds_for_function, c_name_of_type
from prepare_hls_project import get_paramlist_of_sig
from utils import CaicosError, mkdir, copy_files, project_path, make_executable
def build_src_project(bindings, jamaicaoutput, targetdir, syscalls, interfaceResolver, debug, classrefs):
"""
Construct the software portion of the project. Copy the C source code for the Jamaica project,
refactoring the functions that are implemented on the FPGA.
Also copies the FPGA interface and build scripts.
bindings:
A map {id -> java method signature} that gives the ID of each hardware method.
Generated from prepare_hls_project.build_from_functions
jamaicaoutput:
Absolute path of the jamaica builder output directory which contains the source C files
targetdir:
Absolute path to place output files
"""
if not os.path.isfile(join(jamaicaoutput, "Main__nc.o")):
raise CaicosError("Cannot find file " + str(join(jamaicaoutput, "Main__nc.o")) +
". Ensure that the application has first be been built by Jamaica Builder.")
mkdir(targetdir)
copy_files(project_path("projectfiles", "juniper_fpga_interface"), join(targetdir, "juniper_fpga_interface"))
copy_files(project_path("projectfiles", "malloc_preload"), join(targetdir, "malloc_preload"))
refactor_src(bindings, jamaicaoutput, join(targetdir, "src"), debug)
if debug:
copy_files(project_path("debug_software"), join(targetdir, "src"))
generate_interrupt_handler(join(targetdir, "src", "caicos_interrupts.c"), syscalls, interfaceResolver, classrefs)
shutil.copy(join(jamaicaoutput, "Main__nc.o"), join(targetdir, "src"))
shutil.copy(project_path("projectfiles", "include", "juniperoperations.h"), join(targetdir, "src"))
shutil.copy(project_path("projectfiles", "scripts", "run.sh"), targetdir)
make_executable([join(targetdir, "run.sh")])
def refactor_src(bindings, jamaicaoutput, targetdir, debug):
"""
Copy the C source code for the Jamaica project. For files that contain functions that have
been implemented on the FPGA, replace their contents with code to invoke the FPGA instead.
Outputs files to targetdir
"""
#Which are the C files we will need to rewrite?
filestoedit = set()
for _, sig in bindings.iteritems():
filestoedit.add(c_filename_of_java_method_sig(sig, jamaicaoutput))
#Copy the C files over, rewriting those that contain functions we have passed to the hardware
mkdir(targetdir)
for item in os.listdir(jamaicaoutput):
filepath = os.path.join(jamaicaoutput, item)
if os.path.isfile(filepath) and (filepath.endswith(".c") or filepath.endswith(".h")):
if not filepath in filestoedit:
#Just copy the file, no edits required
shutil.copy(filepath, targetdir)
else:
#Any previous edits to the AST need to be disregarded
astcache.invalidate_ast_for(filepath)
#Which sigs are in this file?
toreplace = []
for callid, sig in bindings.iteritems():
if c_filename_of_java_method_sig(sig, jamaicaoutput) == filepath:
decl = c_decl_node_of_java_sig(sig, jamaicaoutput)
bounds = get_line_bounds_for_function(decl)
toreplace.append((bounds, callid, sig, decl))
toreplace.sort() #Sort by ascending line number
filecontents = open(filepath).readlines()
output = "#include <juniper_fpga_interface.h>\n#include <juniperoperations.h>\n#include \"jamaica.h\"\n#include <sys/mman.h>\n\n"
output += "extern void caicos_handle_pcie_interrupt(jamaica_thread *ct, int devNo, int partNo);\n\n"
if debug:
output += "#include \"caicos_debug.h\"\n\n"
lineno = 1
for bounds, callid, sig, decl in toreplace:
while lineno <= bounds[0]:
output = output + filecontents[lineno-1]
lineno += 1
output += "\t//~~~~~~~~~~~~~~~~" + str(sig) + " " + str(bounds) + "~~~~~~~~~~~~~~~~~~~~~\n"
output += "\t" + str(generate_replacement_code(sig, decl, callid, jamaicaoutput, (0, 0)))
if bounds[1] == None: #If a replacement is the last thing in the file we'll need to close some things
#TODO: Is this branch required any more? Suspect not.
output += "}\n\n#else\n#error 'jamaica.h' not found!\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n"
lineno = len(filecontents) #Done copying
else:
output += "}\n"
lineno = bounds[1] + 1
#Copy the rest of the file in
while lineno-1 < len(filecontents):
output = output + filecontents[lineno-1]
lineno += 1
with open(os.path.join(targetdir, item), "w") as outf:
outf.write(output)
def generate_replacement_code(java_sig, decl, callid, jamaicaoutput, device):
"""
Generate C code for the host which replaces the provided Decl node. The generated
code invokes the FPGA and calls the hardware call that has id callid
java_sig:
Java signature from which this function was generated
decl:
c_ast.Decl node of the function declaration we are rewriting
callid:
ID of the hardware call that we are generating for
jamaicaoutput:
Absolute path of the jamaica builder output directory which contains the source C files
device:
pair (device_id, part_id) that describes the hardware accelerator to use
"""
devNoPartNo = str(device[0]) + ", " + str(device[1])
def fpga_set_arg(argNo, val):
return r" juniper_fpga_partition_set_arg(" + devNoPartNo + ", " + str(argNo) + ", " + str(val) + ");\n"
def fpga_retval(rv):
return r" juniper_fpga_partition_get_retval(" + devNoPartNo + ", " + str(rv) + ");\n"
def fpga_run():
code = " juniper_fpga_partition_start(" + devNoPartNo + ");\n"
code += " while(1) {\n"
code += " if(juniper_fpga_partition_idle(" + devNoPartNo + ")) {\n\t\t\tbreak;\n\t\t}\n"
code += " if(juniper_fpga_partition_interrupted(" + devNoPartNo + ")) {\n\t\t\tcaicos_handle_pcie_interrupt(ct, " + devNoPartNo + ");\n\t\t}\n"
code += " }\n"
return code
def fpga_set_base():
code = "void *base = mmap((void *) -1, 0, 0, 0, 0, 0);\n"
code += "\tint rv;\n\n"
code += "\tjuniper_fpga_partition_set_mem_base(" + devNoPartNo + ", -((int) base));\n"
return code
code = fpga_set_base()
#Set up the arguments for the call
params = get_paramlist_of_sig(java_sig, jamaicaoutput)
firstarg = True
for pnum in xrange(len(params.params)):
paramname = params.params[pnum].name
typename = c_name_of_type(params.params[pnum].type)
if firstarg:
code += fpga_set_arg(0, "OP_WRITE_ARG")
firstarg = False
code += fpga_set_arg(1, pnum)
if typename in ["jamaica_int8", "jamaica_int16", "jamaica_int32", "jamaica_uint8", "jamaica_uint16", "jamaica_uint32", "jamaica_bool"]:
code += fpga_set_arg(2, paramname)
elif typename == "jamaica_ref":
code += fpga_set_arg(2, "(int)" + paramname + " / 4")
elif typename == "jamaica_float":
code += fpga_set_arg(2, "*((int *) &" + paramname + ")")
elif typename == "jamaica_address":
code += fpga_set_arg(2, paramname)
elif typename == "*jamaica_thread" and paramname == "ct":
code += fpga_set_arg(2, "((unsigned int) &ct) / 4")
elif typename in ["jamaica_int64", "jamaica_uint64"]:
#TODO
pass
#code += fpga_set_arg(2, paramname)
elif typename == "jamaica_double":
#TODO
pass
#code += fpga_set_arg(2, paramname)
else:
raise CaicosError("Unknown type " + str(typename) + " in function " + str(java_sig))
code += fpga_run() + "\n"
code += fpga_set_arg(0, "OP_CALL") + fpga_set_arg(1, callid) + fpga_run() + "\n"
#Do we need a return value?
rettype = decl.type.type.type.names[0]
if rettype == "void":
code += " return;\n"
elif rettype in ["jamaica_int8", "jamaica_int16", "jamaica_int32",
"jamaica_uint8", "jamaica_uint16", "jamaica_uint32",
"jamaica_bool"]:
code += fpga_retval("&rv")
code += " return rv;\n"
elif rettype in ["jamaica_ref", "jamaica_address"]:
code += fpga_retval("&rv")
code += " return (" + rettype + ") rv;\n"
elif rettype == "jamaica_float":
code += fpga_retval("&rv")
code += " return *((float *)(&rv));\n"
elif rettype in ["jamaica_int64", "jamaica_uint64"]:
#TODO
code += " return 0;\n"
elif rettype == "jamaica_double":
#TODO
code += " return 0;\n"
else:
raise CaicosError("Unknown return type " + str(rettype) + " in function " + str(java_sig))
return code
def generate_interrupt_handler(outputfile, syscalls, interfaceResolver, classrefs):
contents = open(project_path("projectfiles", "templates", "caicos_interrupts.c")).read()
template = Template(contents)
with open(outputfile, "w") as outf:
outf.write(template.safe_substitute({
'ADDITIONAL_SYSCALLS': interrupt_handler_syscall_code(syscalls, interfaceResolver),
'ADDITIONAL_CLASSREFS': interrupt_handler_classref_code(classrefs),
}))
def interrupt_handler_classref_code(classrefs):
t = "\t\t\t"
code = t + "switch(args.arg1) {\n"
for refname, refid in classrefs.referenced.iteritems():
code += t + "\tcase " + str(refid) + ":\n"
code += t + "\t\trv = (int) " + refname + ";\n"
code += t + "\t\tbreak;\n"
code += t + "}\n"
return code
def interrupt_handler_syscall_code(syscalls, interfaceResolver):
code = ""
for funcname, fid in syscalls.iteritems():
funcdecl = flowanalysis.get_funcdecl_of_system_funccall(funcname)
code += "\t\tcase " + str(fid) + ":\n"
#Maybe cast the return type
if funcname == "jamaicaInterpreter_getInterfaceMethod":
code += interfaceResolver.get_host_code()
else:
code += "\t\t\trv = (int) " + funcname + "(ct, "
paramlist = funcdecl.children()[0][1]
for pid in xrange(1, len(paramlist.params)): #Skip the CT argument which is already handled
prm = paramlist.params[pid]
pointer = False
if isinstance(prm.type, c_ast.PtrDecl):
pointer = True
pdec = prm.type.type
else:
pdec = prm.type
code += "(" + str(pdec.type.names[0])
if pointer:
code += "*"
code += ") "
code += "args.arg" + str(pid) #Handily, the args are numbered from arg1 so skipping 0 above works here
if not pid == len(paramlist.params) - 1:
code += ", "
code += ");\n"
code += "\t\t\tbreak;\n"
return code