-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJuliogParameterize.jl
550 lines (509 loc) · 14.1 KB
/
JuliogParameterize.jl
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
# ex is a hardware function block
# passedArguments is the passed Tuple which represents its arguments
function parameterize(ex::Expr, passedArgs::Expr)
jexpr = Expr(:function)
jargs = Array{Any}(2)
funcArgs = ex.args[1]
# Resolve the keyword parameters
if isa(funcArgs.args[2], Expr) && funcArgs.args[2].head == :parameters
# The funcArgs[2] arguments must be keywords
l = length(funcArgs.args[2].args)
pkArgs = passedArgs.args[1]
if isa(pkArgs, Expr) && pkArgs.head == :parameters
m = length(pkArgs.args)
for i = 1:l
keywordSymbol = funcArgs.args[2].args[i].args[1]
keywordValue = funcArgs.args[2].args[i].args[2]
for j = 1:m
if keywordSymbol == pkArgs.args[j].args[1]
keywordValue = pkArgs.args[j].args[2]
break
end
end
eval( :($(keywordSymbol) = $(keywordValue)) )
end
else
for i = 1:l
keywordSymbol = funcArgs.args[2].args[i].args[1]
keywordValue = funcArgs.args[2].args[i].args[2]
eval( :($(keywordSymbol) = $(keywordValue)) )
end
end
# Remove the keywords
jargs[1] = [funcArgs.args[1] ; funcArgs.args[3:end]]
else
jargs[1] = funcArgs
end
global wireSymArray = Array{Symbol}(0)
# parameterize the expression block
# now with complete keyword information
jargs[2] = JPblock(ex.args[2])
jexpr.args = jargs
return jexpr
end
function JPblock(ex::Expr)
l = length(ex.args)
for i = 1:l
arg = ex.args[i]
if isa(arg, Expr)
ex.args[i] = JPblockhelper(arg)
elseif isa(arg, Symbol) && isdefined(arg)
ex.args[i] = eval(arg)
end
end
# scrub out the nothing(s)
l = length(ex.args)
i = 1
while i <= l
arg = ex.args[i]
if arg === nothing
deleteat!(ex.args, i)
l = l - 1
else
i = i + 1
end
end
# raise the blocks
i = 1
while i <= length(ex.args)
arg = ex.args[i]
if isa(arg, Expr) && arg.head == :block
index = i
args_ref = arg.args
ar = length(args_ref)
for j = 1:ar
insert!(ex.args, index, args_ref[j])
index = index + 1
end
deleteat!(ex.args, index)
else
i = i + 1
end
end
return ex
end
function JPblockhelper(ex::Expr)
h = ex.head
if h == :(=)
return JPequals(ex)
elseif h == :(:=)
return JPassignment(ex)
elseif h == :const
return JPconst(ex)
elseif h == :global
if isa(ex.args[1], Expr)
error("global expressions are not supported.")
end
eval(ex)
return nothing
elseif h == :import
eval(ex)
return nothing
elseif h == :macrocall
return JPmacro(ex)
elseif h == :if
return JPif(ex)
elseif h == :for
return JPfor(ex)
elseif h == :while
error("hit unfinished block: $(h)")
else
error("hit unexpected expr head $(h) in JPblockhelper:\n$(ex)")
end
end
function JPequals(ex::Expr)
global wireSymArray
assn = ex.args[2]
if isa(assn, Expr)
ex.args[2] = JPrhs(assn)
elseif isa(assn, Symbol) && isdefined(assn)
ex.args[2] = eval(assn)
end
if isa(ex.args[1], Expr)
ex.args[1] = JPlhs(ex.args[1])
else
if isa(assn ,Expr) && JPwiredec(assn)
push!(wireSymArray, ex.args[1])
end
if !isa(ex.args[2], Symbol) && !isa(ex.args[2], Expr) && !in(ex.args[1], wireSymArray)
eval(ex)
return nothing
elseif isa(ex.args[2], Expr) && isevaluable(ex.args[2]) && !in(ex.args[1], wireSymArray)
eval(ex)
return nothing
end
end
return ex
end
function JPassignment(ex::Expr)
assn = ex.args[2]
if isa(assn, Expr)
ex.args[2] = JPrhs(assn)
elseif isa(assn, Symbol) && isdefined(assn)
ex.args[2] = eval(assn)
end
if isa(ex.args[1], Expr)
ex.args[1] = JPlhs(ex.args[1])
end
return ex
end
function JPconst(ex::Expr)
assn = ex.args[1].args[2]
if isa(assn, Expr)
ex.args[1].args[2] = JPrhs(assn)
elseif isa(assn, Symbol) && isdefined(assn)
ex.args[1].args[2] = eval(assn)
end
if isevaluable(ex.args[1].args[2])
eval(ex)
return nothing
end
return ex
end
function JPrhs(ex::Expr)
h = ex.head
if h == :call
return JPcall(ex)
elseif h == :vcat
return JPvcat(ex)
elseif h == :ref
return JPref(ex)
elseif h == :if
return JPif(ex)
else
error("Hit unexpected expression symbol $(h) in JPrhs on expression:\n$(ex)")
end
end
function JPlhs(ex::Expr)
h = ex.head
if h == :call
return JPcall(ex)
elseif h == :ref
return JPref(ex)
elseif h == :vcat
return JPvcat(ex)
elseif h == :tuple
return JPtuple(ex)
else
error("Hit unexpected expression symbol $(h) in JPlhs on expression:\n$(ex)")
end
end
function JPcall(ex::Expr)
if ex.args[1] != :(<=)
# Is not a Non-Blocking operator
l = length(ex.args)
for i = 1:l
arg = ex.args[i]
if isa(arg, Expr)
ex.args[i] = JPcallhelper(arg)
# move to check for module i.e. main
elseif isa(arg, Symbol) && isdefined(arg)
ex.args[i] = eval(arg)
end
end
if isevaluable(ex)
return eval(ex)
end
return ex
else
# Definitely a Non-Blocking operator
assn = ex.args[3]
if isa(assn, Expr)
ex.args[3] = JPrhs(assn)
elseif isa(assn, Symbol) && isdefined(assn)
ex.args[3] = eval(assn)
end
if isa(ex.args[2], Expr)
ex.args[2] = JPlhs(ex.args[1])
end
return ex
end
end
function JPcallhelper(ex::Expr)
h = ex.head
if h == :call
return JPcall(ex)
elseif h == :vcat
return JPvcat(ex)
elseif h == :ref
return JPref(ex)
elseif h == :if
return JPif(ex)
else
error("Hit unexpected expression symbol $(h) in JPcall on expression:\n$(ex)")
end
end
function JPvcat(ex::Expr)
l = length(ex.args)
for i = 1:l
arg = ex.args[i]
if isa(arg, Expr)
ex.args[i] = JPvcathelper(arg)
# move to check for module i.e. main
elseif isa(arg, Symbol) && isdefined(arg)
ex.args[i] = eval(arg)
end
end
if isevaluable(ex)
return eval(ex)
end
return ex
end
# MOON vcats in vcats set up busses, matrices
function JPvcathelper(ex::Expr)
h = ex.head
if h == :call
return JPcall(ex)
elseif h == :vcat
return JPvcat(ex)
elseif h == :ref
return JPref(ex)
else
error("Hit unexpected expression symbol $(h) in JPvcat on expression:\n$(ex)")
end
end
# MOON Confirm Matrix and Verilog notation operation
function JPref(ex::Expr)
l = length(ex.args)
for i = 1:l
arg = ex.args[i]
if isa(arg, Expr)
ex.args[i] = JPrefhelper(arg)
elseif isa(arg, Symbol) && isdefined(arg)
ex.args[i] = eval(arg)
end
end
if isa(ex.args[1], Symbol) && isdefined(ex.args[1]) && JPrefevaluable(ex)
eval(ex)
return ex
end
return ex
end
function JPrefhelper(ex::Expr)
if ex.head == :ref || ex.head == :(:)
l = length(ex.args)
for i = 1:l
arg = ex.args[i]
if isa(arg, Expr)
ex.args[i] = JPrefhelper(arg)
elseif isa(arg, Symbol) && isdefined(arg)
ex.args[i] = eval(arg)
end
end
return ex
elseif ex.head == :call
return JPcall(ex)
else
error("Hit an unexpected expr head in JPrefhelper:\n$(ex)")
end
end
function JPrefevaluable(ex::Expr)
l = length(ex.args)
for i = 1:l
if isa(ex.args[i], Expr) && ex.args[i].head == :ref
return false
end
if JPrefevaluable(ex.args[i]) == false
return false
end
end
return true
end
function JPif(ex::Expr)
if isa(ex.args[1], Expr)
if ex.args[1].head == :ref
ex.args[1] = JPref(ex.args[1])
elseif ex.args[1].head == :call
ex.args[1] = JPcall(ex.args[1])
else
error("Incorrect if condition syntax:\n$(ex)")
end
end
condition = ex.args[1]
if isa(condition, Bool)
if condition
arg = ex.args[2]
if isa(arg, Expr)
return JPreduce(arg)
elseif isa(arg, Symbol) && isdefined(arg)
return eval(arg)
else
return arg
end
else
if length(ex.args) == 3
arg = ex.args[3]
if isa(arg, Expr)
return JPreduce(arg)
elseif isa(arg, Symbol) && isdefined(arg)
return eval(arg)
else
return arg
end
else
return nothing
end
end
else
if isa(ex.args[2], Expr)
ex.args[2] = JPreduce(ex.args[2])
elseif isa(ex.args[2], Symbol) && isdefined(ex.args[2])
ex.args[2] = eval(ex.args[2])
end
if length(ex.args) == 3
if isa(ex.args[3], Expr)
ex.args[3] = JPreduce(ex.args[3])
elseif isa(ex.args[3], Symbol) && isdefined(ex.args[3])
ex.args[3] = eval(ex.args[3])
end
end
return ex
end
end
function JPreduce(ex::Expr)
h = ex.head
if h == :call
return JPcall(ex)
elseif h == :vcat
return JPvcat(ex)
elseif h == :ref
return JPref(ex)
elseif h == :if
return JPif(ex)
elseif h == :block
return JPblock(ex)
else
error("Hit unexpected expression symbol $(h) in JPreduce on expression:\n$(ex)")
end
end
function JPfor(ex::Expr)
h = ex.args[1].args[2].head
if h == :(:)
return JPforcolon(ex)
elseif h == :hcat
return JPforarray(ex)
warn("hcat functionality not guaranteed in JPfor on expression:\n$(ex)")
elseif h == :vcat
return JPforarray(ex)
warn("vcat functionality not guaranteed in JPfor on expression:\n$(ex)")
elseif h == :vect
return JPforarray(ex)
warn("vect functionality not guaranteed in JPfor on expression:\n$(ex)")
else
error("Hit unexpected expression symbol $(h) in JPfor on expression:\n$(ex)")
end
end
function JPforcolon(ex::Expr)
subex = ex.args[1].args[2]
# subex represents the looping condition
l = length(subex.args)
for i = 1:l
arg = subex.args[i]
if isa(arg, Expr)
ex.args[1].args[2].args[i] = JPreduce(arg)
elseif isa(arg, Symbol) && isdefined(arg)
ex.args[1].args[2].args[i] = eval(arg)
end
end
l = length(subex.args)
if isevaluable(subex)
if l == 2 && subex.args[1] > subex.args[2]
warn("Skipping over for loop due to negative order")
end
ex.args[1].args[2] = eval(subex)
end
loop = ex.args[1].args[2]
sym = ex.args[1].args[1]
block_arguments = Array{Any}(0)
for i = loop
arg_copy = copy(ex.args[2])
overwrite = :($sym = $i)
eval(overwrite)
arg_copy = JPblock(arg_copy)
append!(block_arguments, arg_copy.args)
end
new_ex = Expr(:block)
new_ex.args = block_arguments
return new_ex
end
function JPforarray(ex::Expr)
subex = ex.args[1].args[2]
# subex represents the looping condition
l = length(subex.args)
for i = 1:l
arg = subex.args[i]
if isa(arg, Expr)
ex.args[1].args[2].args[i] = JPreduce(arg)
elseif isa(arg, Symbol) && isdefined(arg)
ex.args[1].args[2].args[i] = eval(arg)
end
end
loop = ex.args[1].args[2]
sym = ex.args[1].args[1]
block_arguments = Array{Any}(0)
for i = loop
arg_copy = copy(ex.args[2])
overwrite = :($sym = $i)
eval(overwrite)
arg_copy = JPblock(arg_copy)
append!(block_arguments, arg_copy.args)
end
new_ex = Expr(:block)
new_ex.args = block_arguments
return new_ex
end
function JPmacro(ex::Expr)
if ex.args[1] == Symbol("@block")
if isa(ex.args[3] , Expr)
ex.args[3] = eval(ex.args[3])
end
if isa(ex.args[4], Expr)
# This should always be the case
# because a function needs more than one argument
l = length(ex.args[4].args)
for i = 1:l
arg = ex.args[4].args[i]
if isa(arg, Expr)
ex.args[4].args[i] = JPreduce(arg)
end
end
end
eval(ex)
return ex
else
# Any other macro calls
l = length(ex.args)
for i = 2:l
arg = ex.args[i]
if isa(arg, Expr)
ex.args[i] = JPreduce(arg)
end
end
return ex
end
end
function JPwiredec(ex::Expr)
if ex.head == :call
if length(ex.args) == 1
arg = ex.args[1]
return (arg == :Wire) || (arg == :Input) || (arg == :Output)
else
return false
end
elseif ex.head == :ref
if isa(ex.args[1], Expr)
return JPwiredec(ex.args[1])
end
end
return false
end
function isevaluable(ex::Expr)
for arg in ex.args
if isa(arg, Symbol) && !isdefined(arg)
return false
elseif isa(arg, Expr)
return false
end
end
return true
end