-
Notifications
You must be signed in to change notification settings - Fork 124
/
target.py
333 lines (261 loc) · 10 KB
/
target.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from pixie.vm.compiler import with_ns, NS_VAR
from pixie.vm.reader import StringReader
from rpython.jit.codewriter.policy import JitPolicy
from rpython.rlib.jit import JitHookInterface, Counters
from rpython.rlib.rfile import create_stdio
from rpython.annotator.policy import AnnotatorPolicy
from pixie.vm.code import wrap_fn, NativeFn, intern_var, Var
from pixie.vm.object import WrappedException
from rpython.translator.platform import platform
from pixie.vm.primitives import nil
from pixie.vm.atom import Atom
from pixie.vm.persistent_vector import EMPTY as EMPTY_VECTOR
from pixie.vm.util import unicode_from_utf8, unicode_to_utf8
import sys
import os
import os.path as path
import rpython.rlib.rpath as rpath
import rpython.rlib.rpath as rposix
from rpython.rlib.objectmodel import we_are_translated
from rpython.jit.codewriter.policy import log
class DebugIFace(JitHookInterface):
def on_abort(self, reason, jitdriver, greenkey, greenkey_repr, logops, operations):
# print "Aborted Trace, reason: ", Counters.counter_names[reason], logops, greenkey_repr
pass
import sys, pdb
class Policy(JitPolicy, AnnotatorPolicy):
def __init__(self):
JitPolicy.__init__(self, DebugIFace())
def jitpolicy(driver):
return JitPolicy(jithookiface=DebugIFace())
PROGRAM_ARGUMENTS = intern_var(u"pixie.stdlib", u"program-arguments")
PROGRAM_ARGUMENTS.set_root(nil)
LOAD_PATHS = intern_var(u"pixie.stdlib", u"load-paths")
LOAD_PATHS.set_root(nil)
load_path = intern_var(u"pixie.stdlib", u"internal-load-path")
class ReplFn(NativeFn):
def __init__(self, args):
self._argv = args
def inner_invoke(self, args):
import pixie.vm.rt as rt
from pixie.vm.code import intern_var
rt.load_ns(rt.wrap(u"pixie/repl.pxi"))
repl = intern_var(u"pixie.repl", u"repl")
with with_ns(u"user"):
repl.invoke([])
load_file = intern_var(u"pixie.stdlib", u"load-file")
class BatchModeFn(NativeFn):
def __init__(self, args):
self._file = args[0]
self._argv = args[1:]
def inner_invoke(self, args):
import pixie.vm.rt as rt
import pixie.vm.persistent_vector as vector
with with_ns(u"user"):
NS_VAR.deref().include_stdlib()
acc = vector.EMPTY
for x in self._argv:
acc = rt.conj(acc, rt.wrap(x))
PROGRAM_ARGUMENTS.set_root(acc)
with with_ns(u"user"):
try:
f = None
if self._file == '-':
f, _, _ = create_stdio()
else:
if not path.isfile(self._file):
print "Error: Cannot open '" + self._file + "'"
os._exit(1)
load_file.invoke([rt.wrap(self._file)])
return None
data = f.read()
f.close()
if data.startswith("#!"):
newline_pos = data.find("\n")
if newline_pos > 0:
data = data[newline_pos:]
rt.load_reader(StringReader(unicode_from_utf8(data)))
except WrappedException as ex:
print "Error: ", ex._ex.__repr__()
os._exit(1)
class EvalFn(NativeFn):
def __init__(self, expr):
self._expr = expr
def inner_invoke(self, args):
import pixie.vm.rt as rt
with with_ns(u"user"):
NS_VAR.deref().include_stdlib()
rt.load_reader(StringReader(unicode_from_utf8(self._expr)))
class CompileFileFn(NativeFn):
def __init__(self, filename):
self._filename = filename
def inner_invoke(self, args):
import pixie.vm.rt as rt
try:
rt.compile_file(rt.wrap(self._filename))
except WrappedException as ex:
print "Error: ", ex._ex.__repr__()
os._exit(1)
class IsPreloadFlag(object):
def __init__(self):
self._is_true = False
def is_true(self):
return self._is_true
def set_true(self):
self._is_true = True
stdlib_loaded = IsPreloadFlag()
@wrap_fn
def run_load_stdlib():
global stdlib_loaded
if stdlib_loaded.is_true():
return
rt.load_ns(rt.wrap(u"pixie/stdlib.pxi"))
rt.load_ns(rt.wrap(u"pixie/stacklets.pxi"))
stdlib_loaded.set_true()
def load_stdlib():
run_load_stdlib.invoke([])
from pixie.vm.code import intern_var
run_with_stacklets = intern_var(u"pixie.stacklets", u"run-with-stacklets")
def init_vm(progname):
import pixie.vm.stacklet
pixie.vm.stacklet.init()
init_load_path(progname)
load_stdlib()
add_to_load_paths(".")
def entry_point(args):
try:
init_vm(args[0])
interactive = True
exit = False
script_args = []
i = 1
while i < len(args):
arg = args[i]
if arg.startswith('-') and arg != '-':
if arg == '-v' or arg == '--version':
print "Pixie 0.1"
return 0
elif arg == '-h' or arg == '--help':
print args[0] + " [<options>] [<file>]"
print " -h, --help print this help"
print " -v, --version print the version number"
print " -e, --eval=<expr> evaluate the given expression"
print " -l, --load-path=<path> add <path> to pixie.stdlib/load-paths"
print " -c, --compile=<file> compile <path> to a .pxic file"
return 0
elif arg == '-e' or arg == '--eval':
i += 1
if i < len(args):
expr = args[i]
run_with_stacklets.invoke([EvalFn(expr)])
return 0
else:
print "Expected argument for " + arg
return 1
elif arg == '-l' or arg == '--load-path':
i += 1
if i < len(args):
path = args[i]
add_to_load_paths(path)
else:
print "Expected argument for " + arg
return 1
elif arg == "-c" or arg == "--compile":
i += 1
if i < len(args):
path = args[i]
print "Compiling ", path
run_with_stacklets.invoke([CompileFileFn(path)])
exit = True
else:
print "Expected argument for " + arg
return 1
else:
print "Unknown option " + arg
return 1
else:
interactive = False
script_args = args[i:]
break
i += 1
if not exit:
if interactive:
run_with_stacklets.invoke([ReplFn(args)])
else:
run_with_stacklets.invoke([BatchModeFn(script_args)])
except WrappedException as we:
print we._ex.__repr__()
return 0
def add_to_load_paths(path):
rt.reset_BANG_(LOAD_PATHS.deref(), rt.conj(rt.deref(LOAD_PATHS.deref()), rt.wrap(path)))
def init_load_path(self_path):
if not path.isfile(self_path):
self_path = find_in_path(self_path)
assert self_path is not None
if path.islink(self_path):
self_path = os.readlink(self_path)
self_path = dirname(rpath.rabspath(self_path))
# runtime is not loaded yet, so we have to do it manually
LOAD_PATHS.set_root(Atom(EMPTY_VECTOR.conj(rt.wrap(self_path))))
# just for run_load_stdlib (global variables can't be assigned to)
load_path.set_root(rt.wrap(self_path))
def dirname(path):
return rpath.sep.join(path.split(rpath.sep)[0:-1])
def find_in_path(exe_name):
paths = os.environ.get('PATH')
paths = paths.split(path.pathsep)
for p in paths:
exe_path = path.join(p, exe_name)
if path.isfile(exe_path):
return exe_path
return None
from rpython.rtyper.lltypesystem import lltype
from rpython.jit.metainterp import warmspot
def run_child(glob, loc):
interp = loc['interp']
graph = loc['graph']
interp.malloc_check = False
def returns_null(T, *args, **kwds):
return lltype.nullptr(T)
interp.heap.malloc_nonmovable = returns_null # XXX
from rpython.jit.backend.llgraph.runner import LLGraphCPU
#LLtypeCPU.supports_floats = False # for now
apply_jit(interp, graph, LLGraphCPU)
def apply_jit(interp, graph, CPUClass):
print 'warmspot.jittify_and_run() started...'
policy = Policy()
warmspot.jittify_and_run(interp, graph, [], policy=policy,
listops=True, CPUClass=CPUClass,
backendopt=True, inline=True)
def run_debug(argv):
from rpython.rtyper.test.test_llinterp import get_interpreter
# first annotate and rtype
try:
interp, graph = get_interpreter(entry_point, [], backendopt=False,
#config=config,
#type_system=config.translation.type_system,
policy=Policy())
except Exception, e:
print '%s: %s' % (e.__class__, e)
pdb.post_mortem(sys.exc_info()[2])
raise
# parent process loop: spawn a child, wait for the child to finish,
# print a message, and restart
#unixcheckpoint.restartable_point(auto='run')
from rpython.jit.codewriter.codewriter import CodeWriter
CodeWriter.debug = True
run_child(globals(), locals())
import pixie.vm.rt as rt
rt.init()
#stacklet.global_state = stacklet.GlobalState()
def target(*args):
import pixie.vm.rt as rt
driver = args[0]
driver.exe_name = "pixie-vm"
rt.__config__ = args[0].config
print "ARG INFO: ", args
return entry_point, None
import rpython.config.translationoption
print rpython.config.translationoption.get_combined_translation_config()
if __name__ == "__main__":
entry_point(sys.argv)