This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
script.pir
574 lines (512 loc) · 11.8 KB
/
script.pir
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# Copyright (C) 2006-2009, Parrot Foundation.
# $Id$
.namespace ['Wmls']
.sub '__onload' :anon :load
# print "__onload (script.pir)\n"
$P0 = subclass 'Hash', ['Wmls'; 'Script']
$P0 = subclass 'Array', ['Wmls'; 'Constants']
$P0 = subclass 'Integer', ['Wmls'; 'ConstantInteger']
$P0 = subclass 'Float', ['Wmls'; 'ConstantFloat']
$P0 = subclass 'String', ['Wmls'; 'ConstantUTF8String']
$P0 = subclass 'String', ['Wmls'; 'ConstantEmptyString']
$P0 = subclass 'String', ['Wmls'; 'ConstantString']
$P0 = subclass 'Array', ['Wmls'; 'Pragmas']
$P0 = subclass 'Hash', ['Wmls'; 'AccessDomain']
$P0 = subclass 'Hash', ['Wmls'; 'AccessPath']
$P0 = subclass 'Hash', ['Wmls'; 'UserAgentProperty']
$P0 = subclass 'Hash', ['Wmls'; 'UserAgentProperty&Scheme']
$P0 = subclass 'Array', ['Wmls'; 'Functions']
$P0 = subclass 'Array', ['Wmls'; 'FunctionNameTable']
$P0 = subclass 'Hash', ['Wmls'; 'Function']
.end
.namespace ['Wmls'; 'Script']
.sub 'dump' :method
.local string filename
filename = self['filename']
print "## file:"
print filename
print "\n"
print "##\n"
print "## Bytecode Header\n"
print "##\n"
.local int version
.local int major
.local int minor
version = self['VersionNumber']
major = version / 16
major = major + 1
minor = version % 16
print "VersionNumber "
print major
print "."
print minor
print "\n"
.local int code_size
code_size = self['CodeSize']
print "CodeSize "
print code_size
print "\n"
print "## Constant Pool\n"
print "##\n"
.local pmc constants
.local int number_of_constants
constants = self['Constants']
number_of_constants = elements constants
print "NumberOfConstants "
print number_of_constants
print "\n"
.local int character_set
character_set = self['CharacterSet']
print "CharacterSet "
print character_set
print "\n"
constants.'dump'()
print "## Pragma Pool\n"
print "##\n"
.local pmc pragmas
.local int number_of_pragmas
pragmas = self['Pragmas']
number_of_pragmas = elements pragmas
print "NumberOfPragmas "
print number_of_pragmas
print "\n"
pragmas.'dump'()
print "## Function Pool\n"
print "##\n"
.local pmc functions
.local int number_of_functions
functions = self['Functions']
number_of_functions = elements functions
print "NumberOfFunctions "
print number_of_functions
print "\n"
.local pmc function_name_table
function_name_table = self['FunctionNameTable']
function_name_table.'dump'()
functions.'dump'()
.end
.sub 'translate' :method
.local string pir
pir = <<'PIRCODE'
.HLL 'wmlscript'
.loadlib 'wmls_group'
.loadlib 'wmls_ops'
.sub '__onload' :load :anon
$P0 = get_hll_global '@stdlibs'
unless null $P0 goto L1
load_bytecode 'languages/wmlscript/library/WMLScript.pbc'
L1:
PIRCODE
.local pmc function_name_table
function_name_table = self['FunctionNameTable']
$S0 = function_name_table.'translate'(self)
pir .= $S0
pir .= "\n.end\n"
.local pmc functions
functions = self['Functions']
$S0 = functions.'translate'(self)
pir .= $S0
pir .= <<'PIRCODE'
#.sub 'main' :main
# __onload()
# function0()
#.end
PIRCODE
.return (pir)
.end
.sub 'get_nb_arg' :method
.param int idx
.local pmc functions
functions = self['Functions']
.local pmc function
function = functions[idx]
.local int number_of_arguments
number_of_arguments = function['NumberOfArguments']
.return (number_of_arguments)
.end
.namespace ['Wmls'; 'Constants']
.sub 'dump' :method
.local int nb
.local int idx
.local pmc constant
nb = elements self
idx = 0
L1:
unless idx < nb goto L2
constant = self[idx]
print "cst"
print idx
print " "
constant.'dump'()
print "\n"
inc idx
goto L1
L2:
.end
.sub 'translate' :method
.param int idx
.local pmc constant
constant = self[idx]
$S0 = constant.'translate'(idx)
.return ($S0)
.end
.namespace ['Wmls'; 'ConstantInteger']
.sub 'dump' :method
print "int "
print self
.end
.sub 'translate' :method
.param int idx
.local string pir
pir = " .local pmc const"
$S0 = idx
pir .= $S0
pir .= "\n box const"
pir .= $S0
pir .= ", "
$S0 = self
pir .= $S0
pir .= "\n"
.return (pir)
.end
.namespace ['Wmls'; 'ConstantFloat']
.sub 'dump' :method
print "float "
print self
.end
.sub 'translate' :method
.param int idx
.local string pir
pir = " .local pmc const"
$S0 = idx
pir .= $S0
pir .= "\n box const"
pir .= $S0
pir .= ", "
# need a representation that always contains a dot,
# unless box a WmlsInteger
new $P0, 'FixedPMCArray'
set $P0, 1
$P0[0] = self
$S0 = sprintf "%f", $P0 # need a better precision
pir .= $S0
pir .= "\n"
.return (pir)
.end
.namespace ['Wmls'; 'ConstantUTF8String']
.sub 'dump' :method
.local int len
len = elements self
print "string UTF8 ["
print len
print "] "
print self
.end
.sub 'translate' :method
.param int idx
.local string pir
pir = " .local pmc const"
$S0 = idx
pir .= $S0
pir .= "\n box const"
pir .= $S0
pir .= ", unicode:\""
$S0 = self
$S0 = escape $S0
pir .= $S0
pir .= "\"\n"
.return (pir)
.end
.namespace ['Wmls'; 'ConstantEmptyString']
.sub 'dump' :method
print "empty string"
.end
.sub 'translate' :method
.param int idx
.local string pir
pir = " .local pmc const"
$S0 = idx
pir .= $S0
pir .= "\n new const"
pir .= $S0
pir .= ", 'WmlsString'\n"
.return (pir)
.end
.namespace ['Wmls'; 'ConstantString']
.sub 'dump' :method
.local int len
len = elements self
print "string ["
print len
print "] "
print self
.end
.sub 'translate' :method
.param int idx
.local string pir
pir = " .local pmc const"
$S0 = idx
pir .= $S0
pir .= "\n box const"
pir .= $S0
pir .= ", \""
$S0 = self
$S0 = escape $S0
pir .= $S0
pir .= "\"\n"
.return (pir)
.end
.namespace ['Wmls'; 'Pragmas']
.sub 'dump' :method
.local int nb
.local int idx
.local pmc pragma
nb = elements self
idx = 0
L1:
unless idx < nb goto L2
pragma = self[idx]
print "pragma"
print idx
print " "
pragma.'dump'()
print "\n"
inc idx
goto L1
L2:
.end
.namespace ['Wmls'; 'AccessDomain']
.sub 'dump' :method
.local int access_domain_index
access_domain_index = self['AccessDomainIndex']
print "AccessDomain "
print access_domain_index
.end
.namespace ['Wmls'; 'AccessPath']
.sub 'dump' :method
.local int access_path_index
access_path_index = self['AccessPathIndex']
print "AccessPath "
print access_path_index
.end
.namespace ['Wmls'; 'UserAgentProperty']
.sub 'dump' :method
.local int property_name_index
property_name_index = self['PropertyNameIndex']
.local int content_index
content_index = self['ContentIndex']
print "UserAgentProperty "
print property_name_index
print " "
print content_index
.end
.namespace ['Wmls'; 'UserAgentProperty&Scheme']
.sub 'dump' :method
.local int property_name_index
property_name_index = self['PropertyNameIndex']
.local int content_index
content_index = self['ContentIndex']
.local int scheme_index
scheme_index = self['SchemeIndex']
print "UserAgentProperty "
print property_name_index
print " "
print content_index
print " "
print scheme_index
.end
.namespace ['Wmls'; 'FunctionNameTable']
.sub 'dump' :method
print "## Function Name Table\n"
.local int number_of_function_names
number_of_function_names = elements self
print "NumberOfFunctionNames "
print number_of_function_names
print "\n"
.local int idx
.local pmc couple
.local string function_name
.local int function_name_size
.local int function_index
idx = 0
L1:
unless idx < number_of_function_names goto L2
couple = self[idx]
function_index = couple[0]
function_name = couple[1]
function_name_size = length function_name
print function_index
print " "
print function_name_size
print " "
print function_name
print "\n"
inc idx
goto L1
L2:
.end
.sub 'translate' :method
.param pmc script
.local string namespace
namespace = script['filename']
.local string pir
pir = "\n # Function Name Table\n"
.local int nb
nb = elements self
.local int idx
idx = 0
.local pmc couple
.local string function_name
.local int function_index
L1:
unless idx < nb goto L2
couple = self[idx]
function_index = couple[0]
function_name = couple[1]
pir .= " .const 'Sub' "
pir .= function_name
pir .= " = 'function"
$S0 = function_index
pir .= $S0
pir .= "'\n $P0 = clone "
pir .= function_name
pir .= "\n set_hll_global '"
pir .= namespace
pir .= ":"
pir .= function_name
pir .= "', $P0\n"
inc idx
goto L1
L2:
.return (pir)
.end
.namespace ['Wmls'; 'Functions']
.sub 'dump' :method
.local int nb
.local int idx
.local pmc function
nb = elements self
idx = 0
L1:
unless idx < nb goto L2
function = self[idx]
print "## function "
print idx
print "\n"
function.'dump'()
inc idx
goto L1
L2:
.end
.sub 'translate' :method
.param pmc script
.local string pir
pir = "\n#\n# Functions\n#\n"
.local int nb
.local int idx
.local pmc function
nb = elements self
idx = 0
L1:
unless idx < nb goto L2
function = self[idx]
$S0 = function.'translate'(script, idx)
pir .= $S0
inc idx
goto L1
L2:
.return (pir)
.end
.namespace ['Wmls'; 'Function']
.sub 'dump' :method
.local int number_of_arguments
number_of_arguments = self['NumberOfArguments']
.local int number_of_local_variables
number_of_local_variables = self['NumberOfLocalVariables']
.local string code_array
.local int function_size
code_array = self['CodeArray']
function_size = length code_array
print "NumberOfArgument "
print number_of_arguments
print "\n"
print "NumberOfLocalVariables "
print number_of_local_variables
print "\n"
print "FunctionSize "
print function_size
print "\n"
dump_code(code_array)
.end
.sub 'translate' :method
.param pmc script
.param int idx
.local string pir
pir = "\n.sub 'function"
$S0 = idx
pir .= $S0
pir .= "' :anon\n"
.local int number_of_arguments
number_of_arguments = self['NumberOfArguments']
.local int idx
idx = 0
L1:
unless idx < number_of_arguments goto L2
pir .= " .param pmc arg"
$S0 = idx
pir .= $S0
pir .= "\n"
inc idx
goto L1
L2:
.local int number_of_local_variables
number_of_local_variables = self['NumberOfLocalVariables']
.local int number_of_variables
number_of_variables = number_of_arguments + number_of_local_variables
idx = 0
L3:
unless idx < number_of_variables goto L4
pir .= " .local pmc local"
$S0 = idx
pir .= $S0
pir .= "\n"
inc idx
goto L3
L4:
idx = 0
L5:
unless idx < number_of_arguments goto L6
pir .= " local"
$S0 = idx
pir .= $S0
pir .= " = arg"
pir .= $S0
pir .= "\n"
inc idx
goto L5
L6:
L7:
unless idx < number_of_variables goto L8
pir .= " new local"
$S0 = idx
pir .= $S0
pir .= ", 'WmlsString'\n"
inc idx
goto L7
L8:
.local string code_array
code_array = self['CodeArray']
$S0 = translate_code(code_array, script)
pir .= $S0
pir .= <<'PIRCODE'
new $P0, 'WmlsString'
.return ($P0)
.end
PIRCODE
.return (pir)
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: