forked from hpi-swa/RSqueak
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtargetimageloadingsmalltalk.py
More file actions
executable file
·280 lines (247 loc) · 11.2 KB
/
targetimageloadingsmalltalk.py
File metadata and controls
executable file
·280 lines (247 loc) · 11.2 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
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
#! /usr/bin/env python
import sys, time, os
from rpython.rlib import jit, rpath, objectmodel
from spyvm import model, interpreter, squeakimage, objspace, wrapper, error
def _usage(argv):
print """
Usage: %s <path> [-r|-m|-h] [-naPu] [-jpiS] [-tTslLE]
<path> - image path (default: Squeak.image)
Execution mode:
(no flags) - Image will be normally opened.
-r|--run <code> - Code will be compiled and executed in headless mode, result printed.
-m|--method <selector> - Selector will be sent to a SmallInteger in headless mode, result printed.
-h|--help - Output this and exit.
Execution parameters:
-n|--num <int> - Only with -m or -r. SmallInteger to be used as receiver (default: nil).
-a|--arg <arg> - Only with -m. Will be used as single String argument.
-P|--process - Only with -m or -r. Disable headless mode.
A high-priority Process for the new context will be created.
The last active Process in the image will be started,
but then quickly switch to the new synthetic high-prio Process.
By default, in headless mode, the active process in the image will be ignored,
and the image window will probably not open (good for benchmarking).
Headless mode also influences error reporting.
Without -r or -m, headless mode is always disabled.
-u - Only with -m or -r. Try to stop UI-process at startup. Can help benchmarking.
Other parameters:
-j|--jit <jitargs> - jitargs will be passed to the jit configuration.
-p|--poll - Actively poll for events. Try this if the image is not responding well.
-i|--no-interrupts - Disable timer interrupt. Disables non-cooperative scheduling.
-S - Disable specialized storage strategies; always use generic ListStorage
--hacks - Enable Spy hacks. Set display color depth to 8.
Logging parameters:
-t|--trace - Output a trace of each message, primitive, return value and process switch.
-T - Trace important events (Process switch, stack overflow, sender chain manipulation)
-s|--safe-trace - If tracing is active, omit printing contents of BytesObjects
-l|--storage-log - Output a log of storage operations.
-L|--storage-log-aggregate - Output an aggregated storage log at the end of execution.
""" % argv[0]
def get_parameter(argv, idx, arg):
if len(argv) < idx + 1:
raise error.Exit("Missing argument after %s" % arg)
return argv[idx], idx + 1
def get_int_parameter(argv, idx, arg):
param, idx = get_parameter(argv, idx, arg)
try:
result = int(param)
except ValueError, e:
raise error.Exit("Non-int argument after %s" % arg)
return result, idx
def print_error(str):
os.write(2, str + os.linesep)
prebuilt_space = objspace.ObjSpace()
def safe_entry_point(argv):
try:
return entry_point(argv)
except error.Exit, e:
print_error("Exited: %s" % e.msg)
return -1
except error.SmalltalkException, e:
print_error("Unhandled %s. Message: %s" % (e.exception_type, e.msg))
return -1
except BaseException, e:
print_error("Exception: %s" % str(e))
if not objectmodel.we_are_translated():
import traceback
traceback.print_exc()
return -1
finally:
prebuilt_space.strategy_factory.logger.print_aggregated_log()
def entry_point(argv):
# == Main execution parameters
path = None
selector = None
code = ""
number = 0
have_number = False
stringarg = None
headless = True
# == Other parameters
poll = False
interrupts = True
trace = False
trace_important = False
space = prebuilt_space
idx = 1
try:
while idx < len(argv):
arg = argv[idx]
idx += 1
if arg in ["-h", "--help"]:
_usage(argv)
return 0
elif arg in ["-j", "--jit"]:
jitarg, idx = get_parameter(argv, idx, arg)
jit.set_user_param(interpreter.Interpreter.jit_driver, jitarg)
elif arg in ["-n", "--number"]:
number, idx = get_int_parameter(argv, idx, arg)
have_number = True
elif arg in ["-m", "--method"]:
selector, idx = get_parameter(argv, idx, arg)
elif arg in ["-t", "--trace"]:
trace = True
elif arg in ["-T"]:
trace_important = True
elif arg in ["-s", "--safe-trace"]:
space.omit_printing_raw_bytes.activate()
elif arg in ["-p", "--poll"]:
poll = True
elif arg in ["-a", "--arg"]:
stringarg, idx = get_parameter(argv, idx, arg)
elif arg in ["-r", "--run"]:
code, idx = get_parameter(argv, idx, arg)
elif arg in ["-i", "--no-interrupts"]:
interrupts = False
elif arg in ["-P", "--process"]:
headless = False
elif arg in ["--hacks"]:
space.run_spy_hacks.activate()
elif arg in ["-S"]:
space.strategy_factory.no_specialized_storage.activate()
elif arg in ["-u"]:
from spyvm.plugins.vmdebugging import stop_ui_process
stop_ui_process()
elif arg in ["-l", "--storage-log"]:
space.strategy_factory.logger.activate()
elif arg in ["-L", "--storage-log-aggregate"]:
space.strategy_factory.logger.activate(aggregate=True)
elif path is None:
path = arg
else:
_usage(argv)
return -1
if path is None:
path = "Squeak.image"
if code and selector:
raise error.Exit("Cannot handle both -r and -m.")
except error.Exit as e:
print_error("Parameter error: %s" % e.msg)
return 1
path = rpath.rabspath(path)
try:
stream = squeakimage.Stream(filename=path)
except OSError as e:
print_error("%s -- %s (LoadError)" % (os.strerror(e.errno), path))
return 1
# Load & prepare image and environment
image = squeakimage.ImageReader(space, stream).create_image()
interp = interpreter.Interpreter(space, image,
trace=trace, trace_important=trace_important,
evented=not poll, interrupts=interrupts)
space.runtime_setup(argv[0], path)
print_error("") # Line break after image-loading characters
# Create context to be executed
if code or selector:
if not have_number:
w_receiver = space.w_nil
else:
w_receiver = space.wrap_int(number)
if code:
selector = compile_code(interp, w_receiver, code)
s_frame = create_context(interp, w_receiver, selector, stringarg)
if headless:
space.headless.activate()
context = s_frame
else:
create_process(interp, s_frame)
context = active_context(space)
else:
context = active_context(space)
w_result = execute_context(interp, context)
print result_string(w_result)
return 0
def result_string(w_result):
# This will also print contents of strings/symbols/numbers
if not w_result:
return ""
return w_result.as_repr_string().replace('\r', '\n')
def compile_code(interp, w_receiver, code):
selector = "DoIt%d" % int(time.time())
space = interp.space
w_receiver_class = w_receiver.getclass(space)
# The suppress_process_switch flag is a hack/workaround to enable compiling code
# before having initialized the image cleanly. The problem is that the TimingSemaphore is not yet
# registered (primitive 136 not called), so the idle process will never be left once it is entered.
# TODO - Find a way to cleanly initialize the image, without executing the active_context of the image.
# Instead, we want to execute our own context. Then remove this flag (and all references to it)
space.suppress_process_switch.activate()
w_result = interp.perform(
w_receiver_class,
"compile:classified:notifying:",
w_arguments = [space.wrap_string("%s\r\n%s" % (selector, code)),
space.wrap_string("spy-run-code"),
space.w_nil]
)
# TODO - is this expected in every image?
if not isinstance(w_result, model.W_BytesObject) or w_result.as_string() != selector:
raise error.Exit("Unexpected compilation result (probably failed to compile): %s" % result_string(w_result))
space.suppress_process_switch.deactivate()
w_receiver_class.as_class_get_shadow(space).s_methoddict().sync_method_cache()
return selector
def create_context(interp, w_receiver, selector, stringarg):
args = []
if stringarg:
args.append(interp.space.wrap_string(stringarg))
return interp.create_toplevel_context(w_receiver, selector, w_arguments = args)
def create_process(interp, s_frame):
space = interp.space
w_active_process = wrapper.scheduler(space).active_process()
assert isinstance(w_active_process, model.W_PointersObject)
w_benchmark_proc = model.W_PointersObject(
space, w_active_process.getclass(space), w_active_process.size()
)
if interp.image.version.has_closures:
# Priorities below 10 are not allowed in newer versions of Squeak.
active_priority = space.unwrap_int(w_active_process.fetch(space, 2))
priority = active_priority / 2 + 1
priority = max(11, priority)
else:
priority = 7
w_benchmark_proc.store(space, 1, s_frame.w_self())
w_benchmark_proc.store(space, 2, space.wrap_int(priority))
# Make process eligible for scheduling
wrapper.ProcessWrapper(space, w_benchmark_proc).put_to_sleep()
def active_context(space):
w_active_process = wrapper.scheduler(space).active_process()
active_process = wrapper.ProcessWrapper(space, w_active_process)
w_active_context = active_process.suspended_context()
assert isinstance(w_active_context, model.W_PointersObject)
active_process.store_suspended_context(space.w_nil)
return w_active_context.as_context_get_shadow(space)
def execute_context(interp, s_frame):
return interp.interpret_toplevel(s_frame.w_self())
# _____ Target and Main _____
def target(driver, *args):
# driver.config.translation.gc = "stmgc"
# driver.config.translation.gcrootfinder = "stm"
from rpython.rlib import rgc
if hasattr(rgc, "stm_is_enabled"):
driver.config.translation.stm = True
driver.config.translation.thread = True
driver.exe_name = "rsqueak"
return safe_entry_point, None
def jitpolicy(self):
from rpython.jit.codewriter.policy import JitPolicy
return JitPolicy()
if __name__ == "__main__":
safe_entry_point(sys.argv)