sexpir 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/sexpir/ast.rb +26 -1
- data/lib/sexpir/ast_sexp.rb +62 -4
- data/lib/sexpir/parser.rb +122 -3
- data/lib/sexpir/ruby_rtl_generator.rb +56 -3
- data/lib/sexpir/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d45365e286fe8bc9b4753ae19719cdeefd7658601a073815fb353261c2625291
|
|
4
|
+
data.tar.gz: 4520be485401dd92d359610bd35afd9381ffdc888e20ccda1c995766d4a3421e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d83e7befa32854a2ba94e0305fd25143f273e3013f0a4d6269ba833371be725beca266bfee71bb70121fa85a123076293d3bd1b53ce31c5c9f27bb8abaf68fb7
|
|
7
|
+
data.tar.gz: e5f9ee42cb50813177a76bd79115faca88b2fcdc216121633cc6225f3fcb30d1c8aae219534825d5fdda4077da77814f964a00d3151a9dda6ccfa9d708a74993
|
data/lib/sexpir/ast.rb
CHANGED
|
@@ -18,6 +18,11 @@ module Sexpir
|
|
|
18
18
|
|
|
19
19
|
class Signal < Ast
|
|
20
20
|
attr_accessor :name,:type
|
|
21
|
+
attr_accessor :bits_sign
|
|
22
|
+
attr_accessor :reset
|
|
23
|
+
attr_accessor :reset_less
|
|
24
|
+
attr_accessor :name_override
|
|
25
|
+
attr_accessor :min,:max
|
|
21
26
|
end
|
|
22
27
|
|
|
23
28
|
class Io < Signal
|
|
@@ -63,6 +68,21 @@ module Sexpir
|
|
|
63
68
|
@elsifs=[]
|
|
64
69
|
end
|
|
65
70
|
end
|
|
71
|
+
|
|
72
|
+
class Case < Ast
|
|
73
|
+
attr_accessor :expr,:whens,:default
|
|
74
|
+
def initialize
|
|
75
|
+
@whens=[]
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
class When < Ast
|
|
80
|
+
attr_accessor :expr,:body
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
class Default < Ast
|
|
84
|
+
attr_accessor :body
|
|
85
|
+
end
|
|
66
86
|
#===============================
|
|
67
87
|
class Component < Ast
|
|
68
88
|
attr_accessor :name,:type
|
|
@@ -89,10 +109,15 @@ module Sexpir
|
|
|
89
109
|
end
|
|
90
110
|
end
|
|
91
111
|
|
|
92
|
-
class Const <
|
|
112
|
+
class Const < Term
|
|
93
113
|
attr_accessor :value
|
|
94
114
|
def initialize value
|
|
95
115
|
@value=value
|
|
96
116
|
end
|
|
97
117
|
end
|
|
118
|
+
|
|
119
|
+
class Slice < Term
|
|
120
|
+
attr_accessor :expr
|
|
121
|
+
attr_accessor :msb,:lsb
|
|
122
|
+
end
|
|
98
123
|
end
|
data/lib/sexpir/ast_sexp.rb
CHANGED
|
@@ -7,6 +7,11 @@ class Symbol
|
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
module Sexpir
|
|
10
|
+
class Ast
|
|
11
|
+
def sexp
|
|
12
|
+
"(nyi sorry)"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
10
15
|
|
|
11
16
|
class Circuit < Ast
|
|
12
17
|
def sexp
|
|
@@ -26,7 +31,12 @@ module Sexpir
|
|
|
26
31
|
|
|
27
32
|
class Signal < Ast
|
|
28
33
|
def sexp
|
|
29
|
-
|
|
34
|
+
attrs=instance_variables.map do |ivar|
|
|
35
|
+
var=ivar[1..-1]
|
|
36
|
+
value=instance_variable_get(ivar)
|
|
37
|
+
"(#{var} #{value})"
|
|
38
|
+
end.join(' ')
|
|
39
|
+
"(signal #{attrs})"
|
|
30
40
|
end
|
|
31
41
|
end
|
|
32
42
|
|
|
@@ -38,13 +48,23 @@ module Sexpir
|
|
|
38
48
|
|
|
39
49
|
class Input < Io
|
|
40
50
|
def sexp
|
|
41
|
-
|
|
51
|
+
attrs=instance_variables.map do |ivar|
|
|
52
|
+
var=ivar[1..-1]
|
|
53
|
+
value=instance_variable_get(ivar)
|
|
54
|
+
"(#{var} #{value})"
|
|
55
|
+
end.join(' ')
|
|
56
|
+
"(input #{attrs})"
|
|
42
57
|
end
|
|
43
58
|
end
|
|
44
59
|
|
|
45
60
|
class Output < Io
|
|
46
61
|
def sexp
|
|
47
|
-
|
|
62
|
+
attrs=instance_variables.map do |ivar|
|
|
63
|
+
var=ivar[1..-1]
|
|
64
|
+
value=instance_variable_get(ivar)
|
|
65
|
+
"(#{var} #{value})"
|
|
66
|
+
end.join(' ')
|
|
67
|
+
"(output #{attrs})"
|
|
48
68
|
end
|
|
49
69
|
end
|
|
50
70
|
|
|
@@ -110,6 +130,44 @@ module Sexpir
|
|
|
110
130
|
code
|
|
111
131
|
end
|
|
112
132
|
end
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
class Case < Ast
|
|
136
|
+
def sexp
|
|
137
|
+
code=Code.new
|
|
138
|
+
code << "(case #{expr.sexp}"
|
|
139
|
+
code.indent=2
|
|
140
|
+
whens.each{|when_| code << when_.sexp}
|
|
141
|
+
code << self.default.sexp
|
|
142
|
+
code.indent=0
|
|
143
|
+
code << ")"
|
|
144
|
+
code
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
class When < Ast
|
|
149
|
+
def sexp
|
|
150
|
+
code=Code.new
|
|
151
|
+
code << "(when #{expr.sexp}"
|
|
152
|
+
code.indent=2
|
|
153
|
+
code << body.sexp
|
|
154
|
+
code.indent=0
|
|
155
|
+
code << ")"
|
|
156
|
+
code
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
class Default < Ast
|
|
161
|
+
def sexp
|
|
162
|
+
code=Code.new
|
|
163
|
+
code << "(default "
|
|
164
|
+
code.indent=2
|
|
165
|
+
code << body.sexp
|
|
166
|
+
code.indent=0
|
|
167
|
+
code << ")"
|
|
168
|
+
code
|
|
169
|
+
end
|
|
170
|
+
end
|
|
113
171
|
#===============================
|
|
114
172
|
class Component < Ast
|
|
115
173
|
def sexp
|
|
@@ -136,7 +194,7 @@ module Sexpir
|
|
|
136
194
|
end
|
|
137
195
|
end
|
|
138
196
|
|
|
139
|
-
class Const <
|
|
197
|
+
class Const < Term
|
|
140
198
|
def sexp
|
|
141
199
|
value
|
|
142
200
|
end
|
data/lib/sexpir/parser.rb
CHANGED
|
@@ -70,7 +70,29 @@ module Sexpir
|
|
|
70
70
|
sig=Signal.new
|
|
71
71
|
sexp.shift
|
|
72
72
|
sig.name=sexp.shift
|
|
73
|
-
|
|
73
|
+
case sexp.first
|
|
74
|
+
when Symbol
|
|
75
|
+
sig.type=sexp.shift
|
|
76
|
+
when Integer
|
|
77
|
+
val=sexp.shift
|
|
78
|
+
sig.type="bv#{val}"
|
|
79
|
+
end
|
|
80
|
+
while sexp.any?
|
|
81
|
+
case sexp.first
|
|
82
|
+
when Array
|
|
83
|
+
ary=sexp.shift
|
|
84
|
+
field_name=ary.shift
|
|
85
|
+
case field_name
|
|
86
|
+
when :bits_sign,:name,:reset,:reset_less,:name_override,:min,:max
|
|
87
|
+
sig.send("#{field_name}=",ary.shift)
|
|
88
|
+
else
|
|
89
|
+
raise "unknow signal attribut '#{field_name}'"
|
|
90
|
+
end
|
|
91
|
+
else
|
|
92
|
+
raise "unknown signal attribute '#{sexpr.first}'"
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
74
96
|
sig
|
|
75
97
|
end
|
|
76
98
|
|
|
@@ -78,7 +100,28 @@ module Sexpir
|
|
|
78
100
|
input=Input.new
|
|
79
101
|
sexp.shift
|
|
80
102
|
input.name=sexp.shift
|
|
81
|
-
|
|
103
|
+
case sexp.first
|
|
104
|
+
when Symbol
|
|
105
|
+
input.type=sexp.shift
|
|
106
|
+
when Integer
|
|
107
|
+
val=sexp.shift
|
|
108
|
+
input.type="bv#{val}"
|
|
109
|
+
end
|
|
110
|
+
while sexp.any?
|
|
111
|
+
case sexp.first
|
|
112
|
+
when Array
|
|
113
|
+
ary=sexp.shift
|
|
114
|
+
field_name=ary.shift
|
|
115
|
+
case field_name
|
|
116
|
+
when :bits_sign,:name,:reset,:reset_less,:name_override,:min,:max
|
|
117
|
+
input.send("#{field_name}=",ary.shift)
|
|
118
|
+
else
|
|
119
|
+
raise "unknow signal attribut '#{field_name}'"
|
|
120
|
+
end
|
|
121
|
+
else
|
|
122
|
+
raise "unknown signal attribute '#{sexpr.first}'"
|
|
123
|
+
end
|
|
124
|
+
end
|
|
82
125
|
input
|
|
83
126
|
end
|
|
84
127
|
|
|
@@ -86,7 +129,28 @@ module Sexpir
|
|
|
86
129
|
output=Output.new
|
|
87
130
|
sexp.shift
|
|
88
131
|
output.name=sexp.shift
|
|
89
|
-
|
|
132
|
+
case sexp.first
|
|
133
|
+
when Symbol
|
|
134
|
+
output.type=sexp.shift
|
|
135
|
+
when Integer
|
|
136
|
+
val=sexp.shift
|
|
137
|
+
output.type="bv#{val}"
|
|
138
|
+
end
|
|
139
|
+
while sexp.any?
|
|
140
|
+
case sexp.first
|
|
141
|
+
when Array
|
|
142
|
+
ary=sexp.shift
|
|
143
|
+
field_name=ary.shift
|
|
144
|
+
case field_name
|
|
145
|
+
when :bits_sign,:name,:reset,:reset_less,:name_override,:min,:max
|
|
146
|
+
output.send("#{field_name}=",ary.shift)
|
|
147
|
+
else
|
|
148
|
+
raise "unknow signal attribut '#{field_name}'"
|
|
149
|
+
end
|
|
150
|
+
else
|
|
151
|
+
raise "unknown signal attribute '#{sexpr.first}'"
|
|
152
|
+
end
|
|
153
|
+
end
|
|
90
154
|
output
|
|
91
155
|
end
|
|
92
156
|
|
|
@@ -129,6 +193,8 @@ module Sexpir
|
|
|
129
193
|
parse_if(sexp)
|
|
130
194
|
when :assign
|
|
131
195
|
parse_assign(sexp)
|
|
196
|
+
when :case
|
|
197
|
+
parse_case(sexp)
|
|
132
198
|
else
|
|
133
199
|
raise "unknow statement starting with : #{sexp.first}"
|
|
134
200
|
end
|
|
@@ -142,6 +208,14 @@ module Sexpir
|
|
|
142
208
|
comb
|
|
143
209
|
end
|
|
144
210
|
|
|
211
|
+
def parse_seq sexp
|
|
212
|
+
comb=Sequential.new
|
|
213
|
+
sexp.shift
|
|
214
|
+
label=sexp.shift
|
|
215
|
+
comb.body=parse_body(sexp)
|
|
216
|
+
comb
|
|
217
|
+
end
|
|
218
|
+
|
|
145
219
|
def parse_body sexp
|
|
146
220
|
body=Body.new
|
|
147
221
|
while sexp.any?
|
|
@@ -195,6 +269,40 @@ module Sexpir
|
|
|
195
269
|
assign
|
|
196
270
|
end
|
|
197
271
|
|
|
272
|
+
def parse_case sexp
|
|
273
|
+
ret=Case.new
|
|
274
|
+
sexp.shift
|
|
275
|
+
ret.expr=parse_expression(sexp.shift)
|
|
276
|
+
while sexp.any?
|
|
277
|
+
ary=sexp.shift
|
|
278
|
+
case ary.first
|
|
279
|
+
when :when
|
|
280
|
+
ret.whens << parse_when(ary)
|
|
281
|
+
when :default
|
|
282
|
+
ret.default=parse_default(ary)
|
|
283
|
+
else
|
|
284
|
+
raise "unknown case alternative : '#{ary.first}'"
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
ret
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def parse_when sexp
|
|
292
|
+
ret=When.new
|
|
293
|
+
sexp.shift
|
|
294
|
+
ret.expr=parse_expression(sexp.shift)
|
|
295
|
+
ret.body=parse_body(sexp)
|
|
296
|
+
ret
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def parse_default sexp
|
|
300
|
+
ret=Default.new
|
|
301
|
+
sexp.shift
|
|
302
|
+
ret.body=parse_body(sexp)
|
|
303
|
+
ret
|
|
304
|
+
end
|
|
305
|
+
|
|
198
306
|
# expressions
|
|
199
307
|
|
|
200
308
|
def parse_expression sexp
|
|
@@ -214,6 +322,8 @@ module Sexpir
|
|
|
214
322
|
ret=parse_unary(sexp)
|
|
215
323
|
when 3
|
|
216
324
|
ret=parse_binary(sexp)
|
|
325
|
+
when 4
|
|
326
|
+
ret=parse_slice(sexp)
|
|
217
327
|
else
|
|
218
328
|
raise "unknown expression '#{sexp}'(size = '#{sexp.size}')"
|
|
219
329
|
end
|
|
@@ -270,5 +380,14 @@ module Sexpir
|
|
|
270
380
|
ret.op=sexp.shift
|
|
271
381
|
ret
|
|
272
382
|
end
|
|
383
|
+
|
|
384
|
+
def parse_slice sexp
|
|
385
|
+
ret=Slice.new
|
|
386
|
+
sexp.shift
|
|
387
|
+
ret.expr=parse_expression(sexp.shift)
|
|
388
|
+
ret.msb=parse_expression(sexp.shift)
|
|
389
|
+
ret.lsb=parse_expression(sexp.shift)
|
|
390
|
+
ret
|
|
391
|
+
end
|
|
273
392
|
end
|
|
274
393
|
end
|
|
@@ -12,18 +12,27 @@ module Sexpir
|
|
|
12
12
|
log "[+] generating RubyRTL '#{circuit.name}'"
|
|
13
13
|
code=circuit.accept(self)
|
|
14
14
|
puts code.finalize
|
|
15
|
+
filename=circuit.name.to_s+".rb"
|
|
16
|
+
code.save_as filename
|
|
15
17
|
end
|
|
16
18
|
|
|
17
19
|
def visitCircuit circuit,args=nil
|
|
18
20
|
code=Code.new
|
|
19
21
|
code << "require 'ruby_rtl'"
|
|
22
|
+
code.newline
|
|
23
|
+
code << "include RubyRTL"
|
|
24
|
+
code.newline
|
|
20
25
|
code << "class #{circuit.name.capitalize} < Circuit"
|
|
21
26
|
code.indent=2
|
|
27
|
+
code << "def initialize"
|
|
28
|
+
code.indent=4
|
|
22
29
|
circuit.inputs.each{|input| code << input.accept(self)}
|
|
23
30
|
circuit.outputs.each{|output| code << output.accept(self)}
|
|
24
31
|
circuit.signals.each{|signal| code << signal.accept(self)}
|
|
25
32
|
code.newline
|
|
26
33
|
code << circuit.body.accept(self)
|
|
34
|
+
code.indent=2
|
|
35
|
+
code << "end"
|
|
27
36
|
code.indent=0
|
|
28
37
|
code << "end"
|
|
29
38
|
code
|
|
@@ -107,18 +116,55 @@ module Sexpir
|
|
|
107
116
|
def visitIf if_,args=nil
|
|
108
117
|
cond=if_.cond.accept(self)
|
|
109
118
|
code=Code.new
|
|
110
|
-
code << "
|
|
119
|
+
code << "If(#{cond}){"
|
|
111
120
|
code.indent=2
|
|
112
121
|
code << if_.then.accept(self)
|
|
113
122
|
if if_.else
|
|
114
123
|
code.indent=0
|
|
115
|
-
code << "
|
|
124
|
+
code << "Else{"
|
|
116
125
|
code.indent=2
|
|
117
126
|
code << if_.else.accept(self)
|
|
118
127
|
code.indent=0
|
|
128
|
+
code << "}"
|
|
129
|
+
else
|
|
130
|
+
code.indent=0
|
|
131
|
+
code << "}"
|
|
119
132
|
end
|
|
133
|
+
code
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def visitCase case_,args=nil
|
|
137
|
+
expr=case_.expr.accept(self)
|
|
138
|
+
code=Code.new
|
|
139
|
+
code << "Case(#{expr}){"
|
|
140
|
+
code.indent=2
|
|
141
|
+
case_.whens.each do |when_|
|
|
142
|
+
code << when_.accept(self)
|
|
143
|
+
end
|
|
144
|
+
code << case_.default.accept(self)
|
|
120
145
|
code.indent=0
|
|
121
|
-
code << "
|
|
146
|
+
code << "}"
|
|
147
|
+
code
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def visitWhen when_,args=nil
|
|
151
|
+
expr=when_.expr.accept(self)
|
|
152
|
+
code=Code.new
|
|
153
|
+
code << "When(#{expr}){"
|
|
154
|
+
code.indent=2
|
|
155
|
+
code << when_.body.accept(self)
|
|
156
|
+
code.indent=0
|
|
157
|
+
code << "}"
|
|
158
|
+
code
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def visitDefault default_,args=nil
|
|
162
|
+
code=Code.new
|
|
163
|
+
code << "Else{"
|
|
164
|
+
code.indent=2
|
|
165
|
+
code << default_.body.accept(self)
|
|
166
|
+
code.indent=0
|
|
167
|
+
code << "}"
|
|
122
168
|
code
|
|
123
169
|
end
|
|
124
170
|
|
|
@@ -164,6 +210,13 @@ module Sexpir
|
|
|
164
210
|
const.value
|
|
165
211
|
end
|
|
166
212
|
|
|
213
|
+
def visitSlice slice,args=nil
|
|
214
|
+
expr=slice.expr.accept(self)
|
|
215
|
+
msb=slice.msb.accept(self)
|
|
216
|
+
lsb=slice.lsb.accept(self)
|
|
217
|
+
"#{expr}[#{msb}..#{lsb}]"
|
|
218
|
+
end
|
|
219
|
+
|
|
167
220
|
|
|
168
221
|
end
|
|
169
222
|
end
|
data/lib/sexpir/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sexpir
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jean-Christophe Le Lann
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2020-01-
|
|
11
|
+
date: 2020-01-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: distribution
|