Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
OpenGEODE
Commits
543c1a47
Commit
543c1a47
authored
Aug 15, 2014
by
dbarbera
Browse files
Rename imports
parent
0c0cf5fb
Changes
1
Hide whitespace changes
Inline
Side-by-side
LlvmGenerator.py
View file @
543c1a47
...
...
@@ -20,7 +20,9 @@
import
logging
from
singledispatch
import
singledispatch
from
llvm
import
core
,
ee
from
llvm
import
core
as
lc
from
llvm
import
ee
as
le
import
ogAST
import
Helper
...
...
@@ -34,8 +36,8 @@ class Context():
def
__init__
(
self
,
process
):
self
.
name
=
str
(
process
.
processName
)
self
.
process
=
process
self
.
module
=
c
ore
.
Module
.
new
(
self
.
name
)
self
.
target_data
=
e
e
.
TargetData
.
new
(
self
.
module
.
data_layout
)
self
.
module
=
l
c
.
Module
.
new
(
self
.
name
)
self
.
target_data
=
l
e
.
TargetData
.
new
(
self
.
module
.
data_layout
)
self
.
dataview
=
process
.
dataview
self
.
procedures
=
process
.
procedures
...
...
@@ -51,80 +53,80 @@ class Context():
self
.
basic_asn1types
=
{}
# Initialize built-in types
self
.
i1
=
c
ore
.
Type
.
int
(
1
)
self
.
i8
=
c
ore
.
Type
.
int
(
8
)
self
.
i32
=
c
ore
.
Type
.
int
(
32
)
self
.
i64
=
c
ore
.
Type
.
int
(
64
)
self
.
void
=
c
ore
.
Type
.
void
()
self
.
double
=
c
ore
.
Type
.
double
()
self
.
i1_ptr
=
c
ore
.
Type
.
pointer
(
self
.
i1
)
self
.
i8_ptr
=
c
ore
.
Type
.
pointer
(
self
.
i8
)
self
.
i32_ptr
=
c
ore
.
Type
.
pointer
(
self
.
i32
)
self
.
i64_ptr
=
c
ore
.
Type
.
pointer
(
self
.
i64
)
self
.
double_ptr
=
c
ore
.
Type
.
pointer
(
self
.
double
)
self
.
i1
=
l
c
.
Type
.
int
(
1
)
self
.
i8
=
l
c
.
Type
.
int
(
8
)
self
.
i32
=
l
c
.
Type
.
int
(
32
)
self
.
i64
=
l
c
.
Type
.
int
(
64
)
self
.
void
=
l
c
.
Type
.
void
()
self
.
double
=
l
c
.
Type
.
double
()
self
.
i1_ptr
=
l
c
.
Type
.
pointer
(
self
.
i1
)
self
.
i8_ptr
=
l
c
.
Type
.
pointer
(
self
.
i8
)
self
.
i32_ptr
=
l
c
.
Type
.
pointer
(
self
.
i32
)
self
.
i64_ptr
=
l
c
.
Type
.
pointer
(
self
.
i64
)
self
.
double_ptr
=
l
c
.
Type
.
pointer
(
self
.
double
)
# Initialize common constants
self
.
zero
=
c
ore
.
Constant
.
int
(
self
.
i32
,
0
)
self
.
one
=
c
ore
.
Constant
.
int
(
self
.
i32
,
1
)
self
.
zero
=
l
c
.
Constant
.
int
(
self
.
i32
,
0
)
self
.
one
=
l
c
.
Constant
.
int
(
self
.
i32
,
1
)
# Intialize built-in functions
ty
=
c
ore
.
Type
.
function
(
self
.
void
,
[
self
.
i8_ptr
],
True
)
ty
=
l
c
.
Type
.
function
(
self
.
void
,
[
self
.
i8_ptr
],
True
)
self
.
funcs
[
'printf'
]
=
self
.
module
.
add_function
(
ty
,
'printf'
)
ty
=
c
ore
.
Type
.
function
(
self
.
double
,
[
self
.
double
])
ty
=
l
c
.
Type
.
function
(
self
.
double
,
[
self
.
double
])
self
.
funcs
[
'round'
]
=
self
.
module
.
add_function
(
ty
,
'round'
)
self
.
funcs
[
'memcpy'
]
=
c
ore
.
Function
.
intrinsic
(
self
.
funcs
[
'memcpy'
]
=
l
c
.
Function
.
intrinsic
(
self
.
module
,
c
ore
.
INTR_MEMCPY
,
l
c
.
INTR_MEMCPY
,
[
self
.
i8_ptr
,
self
.
i8_ptr
,
self
.
i64
]
)
self
.
funcs
[
'powi'
]
=
c
ore
.
Function
.
intrinsic
(
self
.
funcs
[
'powi'
]
=
l
c
.
Function
.
intrinsic
(
self
.
module
,
c
ore
.
INTR_POWI
,
l
c
.
INTR_POWI
,
[
self
.
double
]
)
self
.
funcs
[
'ceil'
]
=
c
ore
.
Function
.
intrinsic
(
self
.
funcs
[
'ceil'
]
=
l
c
.
Function
.
intrinsic
(
self
.
module
,
c
ore
.
INTR_CEIL
,
l
c
.
INTR_CEIL
,
[
self
.
double
]
)
self
.
funcs
[
'cos'
]
=
c
ore
.
Function
.
intrinsic
(
self
.
funcs
[
'cos'
]
=
l
c
.
Function
.
intrinsic
(
self
.
module
,
c
ore
.
INTR_COS
,
l
c
.
INTR_COS
,
[
self
.
double
]
)
self
.
funcs
[
'fabs'
]
=
c
ore
.
Function
.
intrinsic
(
self
.
funcs
[
'fabs'
]
=
l
c
.
Function
.
intrinsic
(
self
.
module
,
c
ore
.
INTR_FABS
,
l
c
.
INTR_FABS
,
[
self
.
double
]
)
self
.
funcs
[
'floor'
]
=
c
ore
.
Function
.
intrinsic
(
self
.
funcs
[
'floor'
]
=
l
c
.
Function
.
intrinsic
(
self
.
module
,
c
ore
.
INTR_FLOOR
,
l
c
.
INTR_FLOOR
,
[
self
.
double
]
)
self
.
funcs
[
'sin'
]
=
c
ore
.
Function
.
intrinsic
(
self
.
funcs
[
'sin'
]
=
l
c
.
Function
.
intrinsic
(
self
.
module
,
c
ore
.
INTR_SIN
,
l
c
.
INTR_SIN
,
[
self
.
double
]
)
self
.
funcs
[
'sqrt'
]
=
c
ore
.
Function
.
intrinsic
(
self
.
funcs
[
'sqrt'
]
=
l
c
.
Function
.
intrinsic
(
self
.
module
,
c
ore
.
INTR_SQRT
,
l
c
.
INTR_SQRT
,
[
self
.
double
]
)
self
.
funcs
[
'trunc'
]
=
c
ore
.
Function
.
intrinsic
(
self
.
funcs
[
'trunc'
]
=
l
c
.
Function
.
intrinsic
(
self
.
module
,
c
ore
.
INTR_TRUNC
,
l
c
.
INTR_TRUNC
,
[
self
.
double
]
)
...
...
@@ -206,14 +208,14 @@ class Context():
is_variable_size
=
min_size
!=
max_size
elem_llty
=
self
.
lltype_of
(
asn1ty
.
type
)
array_llty
=
c
ore
.
Type
.
array
(
elem_llty
,
max_size
)
array_llty
=
l
c
.
Type
.
array
(
elem_llty
,
max_size
)
if
is_variable_size
:
struct
=
self
.
decl_struct
([
'nCount'
,
'arr'
],
[
self
.
i32
,
array_llty
],
name
)
else
:
struct
=
self
.
decl_struct
([
'arr'
],
[
array_llty
],
name
)
struct_ptr
=
c
ore
.
Type
.
pointer
(
struct
.
llty
)
struct_ptr
=
l
c
.
Type
.
pointer
(
struct
.
llty
)
self
.
decl_func
(
"asn1Scc%s_Equal"
%
name
,
self
.
i1
,
[
struct_ptr
,
struct_ptr
])
return
struct
.
llty
...
...
@@ -229,7 +231,7 @@ class Context():
struct
=
self
.
decl_struct
(
field_names
,
field_lltys
,
name
)
struct_ptr
=
c
ore
.
Type
.
pointer
(
struct
.
llty
)
struct_ptr
=
l
c
.
Type
.
pointer
(
struct
.
llty
)
self
.
decl_func
(
"asn1Scc%s_Equal"
%
name
,
self
.
i1
,
[
struct_ptr
,
struct_ptr
])
return
struct
.
llty
...
...
@@ -241,14 +243,14 @@ class Context():
for
idx
,
field_name
in
enumerate
(
Helper
.
sorted_fields
(
asn1ty
)):
# enum values used in choice determinant/present
self
.
enums
[
field_name
.
replace
(
'-'
,
'_'
)]
=
c
ore
.
Constant
.
int
(
self
.
i32
,
idx
)
self
.
enums
[
field_name
.
replace
(
'-'
,
'_'
)]
=
l
c
.
Constant
.
int
(
self
.
i32
,
idx
)
field_names
.
append
(
field_name
.
replace
(
'-'
,
'_'
))
field_lltys
.
append
(
self
.
lltype_of
(
asn1ty
.
Children
[
field_name
].
type
))
union
=
self
.
decl_union
(
field_names
,
field_lltys
,
name
)
union_ptr
=
c
ore
.
Type
.
pointer
(
union
.
llty
)
union_ptr
=
l
c
.
Type
.
pointer
(
union
.
llty
)
self
.
decl_func
(
"asn1Scc%s_Equal"
%
name
,
self
.
i1
,
[
union_ptr
,
union_ptr
])
return
union
.
llty
...
...
@@ -259,14 +261,14 @@ class Context():
max_size
=
int
(
asn1ty
.
Max
)
is_variable_size
=
min_size
!=
max_size
array_llty
=
c
ore
.
Type
.
array
(
self
.
i8
,
max_size
)
array_llty
=
l
c
.
Type
.
array
(
self
.
i8
,
max_size
)
if
is_variable_size
:
struct
=
self
.
decl_struct
([
'nCount'
,
'arr'
],
[
self
.
i32
,
array_llty
],
name
)
else
:
struct
=
self
.
decl_struct
([
'arr'
],
[
array_llty
],
name
)
struct_ptr
=
c
ore
.
Type
.
pointer
(
struct
.
llty
)
struct_ptr
=
l
c
.
Type
.
pointer
(
struct
.
llty
)
self
.
decl_func
(
"asn1Scc%s_Equal"
%
name
,
self
.
i1
,
[
struct_ptr
,
struct_ptr
])
return
struct
.
llty
...
...
@@ -276,7 +278,7 @@ class Context():
if
str
in
self
.
strings
:
return
self
.
strings
[
str
].
gep
([
self
.
zero
,
self
.
zero
])
str_val
=
c
ore
.
Constant
.
stringz
(
str
)
str_val
=
l
c
.
Constant
.
stringz
(
str
)
var_name
=
'.str%s'
%
len
(
self
.
strings
)
var_ptr
=
self
.
module
.
add_global_variable
(
str_val
.
type
,
var_name
)
var_ptr
.
initializer
=
str_val
...
...
@@ -285,9 +287,9 @@ class Context():
def
decl_func
(
self
,
name
,
return_llty
,
param_lltys
,
extern
=
False
):
''' Declare a function '''
func_llty
=
c
ore
.
Type
.
function
(
return_llty
,
param_lltys
)
func_llty
=
l
c
.
Type
.
function
(
return_llty
,
param_lltys
)
func_name
=
(
"%s_RI_%s"
%
(
self
.
name
,
name
))
if
extern
else
name
func
=
c
ore
.
Function
.
new
(
self
.
module
,
func_llty
,
func_name
)
func
=
l
c
.
Function
.
new
(
self
.
module
,
func_llty
,
func_name
)
self
.
funcs
[
name
.
lower
()]
=
func
return
func
...
...
@@ -319,7 +321,7 @@ class StructType():
def
__init__
(
self
,
name
,
field_names
,
field_lltys
):
self
.
name
=
name
self
.
field_names
=
field_names
self
.
llty
=
c
ore
.
Type
.
struct
(
field_lltys
,
self
.
name
)
self
.
llty
=
l
c
.
Type
.
struct
(
field_lltys
,
self
.
name
)
def
idx
(
self
,
field_name
):
return
self
.
field_names
.
index
(
field_name
)
...
...
@@ -333,7 +335,7 @@ class UnionType():
# Unions are represented a struct with a field indicating the index of its type
# and a byte array with the size of the biggest type in the union
self
.
size
=
max
([
ctx
.
target_data
.
size
(
ty
)
for
ty
in
field_lltys
])
self
.
llty
=
c
ore
.
Type
.
struct
([
ctx
.
i32
,
c
ore
.
Type
.
array
(
ctx
.
i8
,
self
.
size
)],
name
)
self
.
llty
=
l
c
.
Type
.
struct
([
ctx
.
i32
,
l
c
.
Type
.
array
(
ctx
.
i8
,
self
.
size
)],
name
)
def
kind
(
self
,
name
):
idx
=
self
.
field_names
.
index
(
name
)
...
...
@@ -398,22 +400,22 @@ def _process(process, ctx=None):
# Initialize states
for
name
,
val
in
process
.
mapping
.
viewitems
():
if
not
name
.
endswith
(
'START'
):
cons_val
=
c
ore
.
Constant
.
int
(
ctx
.
i32
,
len
(
ctx
.
states
))
cons_val
=
l
c
.
Constant
.
int
(
ctx
.
i32
,
len
(
ctx
.
states
))
ctx
.
states
[
name
.
lower
()]
=
cons_val
elif
name
!=
'START'
:
cons_val
=
c
ore
.
Constant
.
int
(
ctx
.
i32
,
val
)
cons_val
=
l
c
.
Constant
.
int
(
ctx
.
i32
,
val
)
ctx
.
states
[
name
.
lower
()]
=
cons_val
# Generate state var
state_cons
=
ctx
.
module
.
add_global_variable
(
ctx
.
i32
,
'.state'
)
state_cons
.
initializer
=
c
ore
.
Constant
.
int
(
ctx
.
i32
,
-
1
)
state_cons
.
initializer
=
l
c
.
Constant
.
int
(
ctx
.
i32
,
-
1
)
ctx
.
scope
.
define
(
'.state'
,
state_cons
)
# Generare process-level vars
for
name
,
(
asn1ty
,
expr
)
in
process
.
variables
.
viewitems
():
var_llty
=
ctx
.
lltype_of
(
asn1ty
)
global_var
=
ctx
.
module
.
add_global_variable
(
var_llty
,
str
(
name
))
global_var
.
initializer
=
c
ore
.
Constant
.
null
(
var_llty
)
global_var
.
initializer
=
l
c
.
Constant
.
null
(
var_llty
)
ctx
.
scope
.
define
(
str
(
name
).
lower
(),
global_var
)
# Declare set/reset timer functions
...
...
@@ -425,14 +427,14 @@ def _process(process, ctx=None):
# Declare output signal functions
for
signal
in
process
.
output_signals
:
if
'type'
in
signal
:
param_lltys
=
[
c
ore
.
Type
.
pointer
(
ctx
.
lltype_of
(
signal
[
'type'
]))]
param_lltys
=
[
l
c
.
Type
.
pointer
(
ctx
.
lltype_of
(
signal
[
'type'
]))]
else
:
param_lltys
=
[]
ctx
.
decl_func
(
str
(
signal
[
'name'
]),
ctx
.
void
,
param_lltys
,
True
)
# Declare external procedures functions
for
proc
in
[
proc
for
proc
in
process
.
procedures
if
proc
.
external
]:
param_lltys
=
[
c
ore
.
Type
.
pointer
(
ctx
.
lltype_of
(
p
[
'type'
]))
for
p
in
proc
.
fpar
]
param_lltys
=
[
l
c
.
Type
.
pointer
(
ctx
.
lltype_of
(
p
[
'type'
]))
for
p
in
proc
.
fpar
]
ctx
.
decl_func
(
str
(
proc
.
inputString
),
ctx
.
void
,
param_lltys
,
True
)
# Generate internal procedures
...
...
@@ -468,7 +470,7 @@ def generate_runtr_func(process, ctx):
body_block
=
func
.
append_basic_block
(
'runtr:body'
)
exit_block
=
func
.
append_basic_block
(
'runtr:exit'
)
ctx
.
builder
=
c
ore
.
Builder
.
new
(
entry_block
)
ctx
.
builder
=
l
c
.
Builder
.
new
(
entry_block
)
# entry
id_ptr
=
ctx
.
builder
.
alloca
(
ctx
.
i32
,
None
,
'id'
)
...
...
@@ -478,9 +480,9 @@ def generate_runtr_func(process, ctx):
# cond
ctx
.
builder
.
position_at_end
(
cond_block
)
no_tr_cons
=
c
ore
.
Constant
.
int
(
ctx
.
i32
,
-
1
)
no_tr_cons
=
l
c
.
Constant
.
int
(
ctx
.
i32
,
-
1
)
id_val
=
ctx
.
builder
.
load
(
id_ptr
)
cond_val
=
ctx
.
builder
.
icmp
(
c
ore
.
ICMP_NE
,
id_val
,
no_tr_cons
,
'cond'
)
cond_val
=
ctx
.
builder
.
icmp
(
l
c
.
ICMP_NE
,
id_val
,
no_tr_cons
,
'cond'
)
ctx
.
builder
.
cbranch
(
cond_val
,
body_block
,
exit_block
)
# body
...
...
@@ -490,7 +492,7 @@ def generate_runtr_func(process, ctx):
# transitions
for
idx
,
tr
in
enumerate
(
process
.
transitions
):
tr_block
=
func
.
append_basic_block
(
'runtr:tr%d'
%
idx
)
const
=
c
ore
.
Constant
.
int
(
ctx
.
i32
,
idx
)
const
=
l
c
.
Constant
.
int
(
ctx
.
i32
,
idx
)
switch
.
add_case
(
const
,
tr_block
)
ctx
.
builder
.
position_at_end
(
tr_block
)
generate
(
tr
,
ctx
)
...
...
@@ -522,7 +524,7 @@ def generate_startup_func(process, ctx):
ctx
.
open_scope
()
entry_block
=
func
.
append_basic_block
(
'startup:entry'
)
ctx
.
builder
=
c
ore
.
Builder
.
new
(
entry_block
)
ctx
.
builder
=
l
c
.
Builder
.
new
(
entry_block
)
# Initialize process level variables
for
name
,
(
ty
,
expr
)
in
process
.
variables
.
viewitems
():
...
...
@@ -530,7 +532,7 @@ def generate_startup_func(process, ctx):
global_var
=
ctx
.
scope
.
resolve
(
str
(
name
))
sdl_assign
(
global_var
,
expression
(
expr
,
ctx
),
ctx
)
sdl_call
(
'run_transition'
,
[
c
ore
.
Constant
.
int
(
ctx
.
i32
,
0
)],
ctx
)
sdl_call
(
'run_transition'
,
[
l
c
.
Constant
.
int
(
ctx
.
i32
,
0
)],
ctx
)
ctx
.
builder
.
ret_void
()
ctx
.
close_scope
()
...
...
@@ -544,7 +546,7 @@ def generate_input_signal(signal, inputs, ctx):
func_name
=
ctx
.
name
+
"_"
+
str
(
signal
[
'name'
])
param_lltys
=
[]
if
'type'
in
signal
:
param_lltys
.
append
(
c
ore
.
Type
.
pointer
(
ctx
.
lltype_of
(
signal
[
'type'
])))
param_lltys
.
append
(
l
c
.
Type
.
pointer
(
ctx
.
lltype_of
(
signal
[
'type'
])))
func
=
ctx
.
decl_func
(
func_name
,
ctx
.
void
,
param_lltys
)
...
...
@@ -552,7 +554,7 @@ def generate_input_signal(signal, inputs, ctx):
entry_block
=
func
.
append_basic_block
(
'input:entry'
)
exit_block
=
func
.
append_basic_block
(
'input:exit'
)
ctx
.
builder
=
c
ore
.
Builder
.
new
(
entry_block
)
ctx
.
builder
=
l
c
.
Builder
.
new
(
entry_block
)
g_state_val
=
ctx
.
builder
.
load
(
ctx
.
global_scope
.
resolve
(
'.state'
))
switch
=
ctx
.
builder
.
switch
(
g_state_val
,
exit_block
)
...
...
@@ -585,7 +587,7 @@ def generate_input_signal(signal, inputs, ctx):
sdl_assign
(
var_ptr
,
ctx
.
builder
.
load
(
func
.
args
[
0
]),
ctx
)
if
trans
:
id_val
=
c
ore
.
Constant
.
int
(
ctx
.
i32
,
state_input
.
transition_id
)
id_val
=
l
c
.
Constant
.
int
(
ctx
.
i32
,
state_input
.
transition_id
)
sdl_call
(
'run_transition'
,
[
id_val
],
ctx
)
ctx
.
builder
.
ret_void
()
...
...
@@ -629,7 +631,7 @@ def _output(output, ctx):
for
arg
in
args
:
arg_val
=
expression
(
arg
,
ctx
)
# Pass by reference
if
arg_val
.
type
.
kind
!=
c
ore
.
TYPE_POINTER
:
if
arg_val
.
type
.
kind
!=
l
c
.
TYPE_POINTER
:
arg_var
=
ctx
.
builder
.
alloca
(
arg_val
.
type
,
None
)
ctx
.
builder
.
store
(
arg_val
,
arg_var
)
arg_vals
.
append
(
arg_var
)
...
...
@@ -675,7 +677,7 @@ def _proc_call(proc_call, ctx):
else
:
arg_val
=
expression
(
arg
,
ctx
)
# Pass by reference
if
arg_val
.
type
.
kind
!=
c
ore
.
TYPE_POINTER
:
if
arg_val
.
type
.
kind
!=
l
c
.
TYPE_POINTER
:
arg_var
=
ctx
.
builder
.
alloca
(
arg_val
.
type
,
None
)
ctx
.
builder
.
store
(
arg_val
,
arg_var
)
arg_vals
.
append
(
arg_var
)
...
...
@@ -725,14 +727,14 @@ def generate_for_range(loop, ctx):
start_val
=
expression
(
loop
[
'range'
][
'start'
],
ctx
)
ctx
.
builder
.
store
(
start_val
,
loop_var
)
else
:
ctx
.
builder
.
store
(
c
ore
.
Constant
.
int
(
ctx
.
i64
,
0
),
loop_var
)
ctx
.
builder
.
store
(
l
c
.
Constant
.
int
(
ctx
.
i64
,
0
),
loop_var
)
stop_val
=
expression
(
loop
[
'range'
][
'stop'
],
ctx
)
ctx
.
builder
.
branch
(
cond_block
)
ctx
.
builder
.
position_at_end
(
cond_block
)
loop_val
=
ctx
.
builder
.
load
(
loop_var
)
cond_val
=
ctx
.
builder
.
icmp
(
c
ore
.
ICMP_SLT
,
loop_val
,
stop_val
)
cond_val
=
ctx
.
builder
.
icmp
(
l
c
.
ICMP_SLT
,
loop_val
,
stop_val
)
ctx
.
builder
.
cbranch
(
cond_val
,
body_block
,
end_block
)
ctx
.
builder
.
position_at_end
(
body_block
)
...
...
@@ -740,7 +742,7 @@ def generate_for_range(loop, ctx):
ctx
.
builder
.
branch
(
inc_block
)
ctx
.
builder
.
position_at_end
(
inc_block
)
step_val
=
c
ore
.
Constant
.
int
(
ctx
.
i64
,
loop
[
'range'
][
'step'
])
step_val
=
l
c
.
Constant
.
int
(
ctx
.
i64
,
loop
[
'range'
][
'step'
])
loop_val
=
ctx
.
builder
.
load
(
loop_var
)
temp_val
=
ctx
.
builder
.
add
(
loop_val
,
step_val
)
ctx
.
builder
.
store
(
temp_val
,
loop_var
)
...
...
@@ -770,7 +772,7 @@ def generate_for_iterable(loop, ctx):
ctx
.
open_scope
()
idx_ptr
=
ctx
.
builder
.
alloca
(
ctx
.
i32
)
ctx
.
builder
.
store
(
c
ore
.
Constant
.
int
(
ctx
.
i32
,
0
),
idx_ptr
)
ctx
.
builder
.
store
(
l
c
.
Constant
.
int
(
ctx
.
i32
,
0
),
idx_ptr
)
seqof_val
=
expression
(
loop
[
'list'
],
ctx
)
if
isinstance
(
seqof_val
,
SDLSubstringValue
):
...
...
@@ -788,7 +790,7 @@ def generate_for_iterable(loop, ctx):
# load the current number of elements that is on the first field
end_idx
=
ctx
.
builder
.
load
(
ctx
.
builder
.
gep
(
seqof_val
,
[
ctx
.
zero
,
ctx
.
zero
]))
else
:
end_idx
=
c
ore
.
Constant
.
int
(
ctx
.
i32
,
array_ptr
.
type
.
pointee
.
count
)
end_idx
=
l
c
.
Constant
.
int
(
ctx
.
i32
,
array_ptr
.
type
.
pointee
.
count
)
var_ptr
=
ctx
.
builder
.
alloca
(
elem_llty
,
None
,
str
(
loop
[
'var'
]))
ctx
.
scope
.
define
(
str
(
loop
[
'var'
]),
var_ptr
)
...
...
@@ -798,7 +800,7 @@ def generate_for_iterable(loop, ctx):
# load block
ctx
.
builder
.
position_at_end
(
load_block
)
idx_var
=
ctx
.
builder
.
load
(
idx_ptr
)
if
elem_llty
.
kind
==
c
ore
.
TYPE_STRUCT
:
if
elem_llty
.
kind
==
l
c
.
TYPE_STRUCT
:
elem_ptr
=
ctx
.
builder
.
gep
(
array_ptr
,
[
ctx
.
zero
,
idx_var
])
sdl_assign
(
var_ptr
,
elem_ptr
,
ctx
)
else
:
...
...
@@ -815,7 +817,7 @@ def generate_for_iterable(loop, ctx):
ctx
.
builder
.
position_at_end
(
cond_block
)
tmp_val
=
ctx
.
builder
.
add
(
idx_var
,
ctx
.
one
)
ctx
.
builder
.
store
(
tmp_val
,
idx_ptr
)
cond_val
=
ctx
.
builder
.
icmp
(
c
ore
.
ICMP_SLT
,
tmp_val
,
end_idx
)
cond_val
=
ctx
.
builder
.
icmp
(
l
c
.
ICMP_SLT
,
tmp_val
,
end_idx
)
ctx
.
builder
.
cbranch
(
cond_val
,
load_block
,
end_block
)
ctx
.
builder
.
position_at_end
(
end_block
)
...
...
@@ -843,14 +845,14 @@ def _prim_selector_reference(prim, ctx):
if
receiver_ptr
.
type
.
pointee
.
name
in
ctx
.
structs
:
struct
=
ctx
.
structs
[
receiver_ptr
.
type
.
pointee
.
name
]
field_idx_cons
=
c
ore
.
Constant
.
int
(
ctx
.
i32
,
struct
.
idx
(
field_name
))
field_idx_cons
=
l
c
.
Constant
.
int
(
ctx
.
i32
,
struct
.
idx
(
field_name
))
return
ctx
.
builder
.
gep
(
receiver_ptr
,
[
ctx
.
zero
,
field_idx_cons
])
else
:
union
=
ctx
.
unions
[
receiver_ptr
.
type
.
pointee
.
name
]
_
,
field_llty
=
union
.
kind
(
field_name
)
field_ptr
=
ctx
.
builder
.
gep
(
receiver_ptr
,
[
ctx
.
zero
,
ctx
.
one
])
return
ctx
.
builder
.
bitcast
(
field_ptr
,
c
ore
.
Type
.
pointer
(
field_llty
))
return
ctx
.
builder
.
bitcast
(
field_ptr
,
l
c
.
Type
.
pointer
(
field_llty
))
@
reference
.
register
(
ogAST
.
PrimIndex
)
...
...
@@ -880,7 +882,7 @@ def _prim_substring_reference(prim, ctx):
low_val
=
expression
(
prim
.
value
[
1
][
'substring'
][
0
],
ctx
)
high_val
=
expression
(
prim
.
value
[
1
][
'substring'
][
1
],
ctx
)
count_val
=
ctx
.
builder
.
sub
(
high_val
,
low_val
)
count_val
=
ctx
.
builder
.
add
(
count_val
,
c
ore
.
Constant
.
int
(
ctx
.
i64
,
1
))
count_val
=
ctx
.
builder
.
add
(
count_val
,
l
c
.
Constant
.
int
(
ctx
.
i64
,
1
))
count_val
=
ctx
.
builder
.
trunc
(
count_val
,
ctx
.
i32
)
if
isinstance
(
seqof_val
,
SDLSubstringValue
):
...
...
@@ -966,26 +968,26 @@ def _expr_rel(expr, ctx):
if
basic_asn1ty
.
kind
in
(
'IntegerType'
,
'Integer32Type'
):
if
isinstance
(
expr
,
ogAST
.
ExprLt
):
return
ctx
.
builder
.
icmp
(
c
ore
.
ICMP_SLT
,
left_val
,
right_val
)
return
ctx
.
builder
.
icmp
(
l
c
.
ICMP_SLT
,
left_val
,
right_val
)
elif
isinstance
(
expr
,
ogAST
.
ExprLe
):
return
ctx
.
builder
.
icmp
(
c
ore
.
ICMP_SLE
,
left_val
,
right_val
)
return
ctx
.
builder
.
icmp
(
l
c
.
ICMP_SLE
,
left_val
,
right_val
)
elif
isinstance
(
expr
,
ogAST
.
ExprGe
):
return
ctx
.
builder
.
icmp
(
c
ore
.
ICMP_SGE
,
left_val
,
right_val
)
return
ctx
.
builder
.
icmp
(
l
c
.
ICMP_SGE
,
left_val
,
right_val
)
elif
isinstance
(
expr
,
ogAST
.
ExprGt
):
return
ctx
.
builder
.
icmp
(
c
ore
.
ICMP_SGT
,
left_val
,
right_val
)
return
ctx
.
builder
.
icmp
(
l
c
.
ICMP_SGT
,
left_val
,
right_val
)
raise
CompileError
(
'Expression "%s" not supported for Integer types'
%
expr
.
__class__
.
__name__
)
elif
basic_asn1ty
.
kind
==
'RealType'
:
if
isinstance
(
expr
,
ogAST
.
ExprLt
):
return
ctx
.
builder
.
fcmp
(
c
ore
.
FCMP_OLT
,
left_val
,
right_val
)
return
ctx
.
builder
.
fcmp
(
l
c
.
FCMP_OLT
,
left_val
,
right_val
)
elif
isinstance
(
expr
,
ogAST
.
ExprLe
):
return
ctx
.
builder
.
fcmp
(
c
ore
.
FCMP_OLE
,
left_val
,
right_val
)
return
ctx
.
builder
.
fcmp
(
l
c
.
FCMP_OLE
,
left_val
,
right_val
)
elif
isinstance
(
expr
,
ogAST
.
ExprGe
):
return
ctx
.
builder
.
fcmp
(
c
ore
.
FCMP_OGE
,
left_val
,
right_val
)
return
ctx
.
builder
.
fcmp
(
l
c
.
FCMP_OGE
,
left_val
,
right_val
)
elif
isinstance
(
expr
,
ogAST
.
ExprGt
):
return
ctx
.
builder
.
fcmp
(
c
ore
.
FCMP_OGT
,
left_val
,
right_val
)
return
ctx
.
builder
.
fcmp
(
l
c
.
FCMP_OGT
,
left_val
,
right_val
)
raise
CompileError
(
'Expression "%s" not supported for Real types'
%
expr
.
__class__
.
__name__
)
...
...
@@ -1013,11 +1015,11 @@ def _expr_eq(expr, ctx):
def
_expr_neg
(
expr
,
ctx
):
''' Generate the IR for a negative expression '''
expr_val
=
expression
(
expr
.
expr
,
ctx
)
if
expr_val
.
type
.
kind
==
c
ore
.
TYPE_INTEGER
:
zero_val
=
c
ore
.
Constant
.
int
(
ctx
.
i64
,
0
)
if
expr_val
.
type
.
kind
==
l
c
.
TYPE_INTEGER
:
zero_val
=
l
c
.
Constant
.
int
(
ctx
.
i64
,
0
)
return
ctx
.
builder
.
sub
(
zero_val
,
expr_val
)
else
:
zero_val
=
c
ore
.
Constant
.
real
(
ctx
.
double
,
0
)
zero_val
=
l
c
.
Constant
.
real
(
ctx
.
double
,
0
)
return
ctx
.
builder
.
fsub
(
zero_val
,
expr_val
)
...
...
@@ -1090,10 +1092,10 @@ def _expr_logic(expr, ctx):
res_ptr
=
ctx
.
builder
.
alloca
(
left_ptr
.
type
.
pointee
)
array_llty
=
res_ptr
.
type
.
pointee
.
elements
[
0
]
len_val
=
c
ore
.
Constant
.
int
(
ctx
.
i32
,
array_llty
.
count
)
len_val
=
l
c
.
Constant
.
int
(
ctx
.
i32
,
array_llty
.
count
)
idx_ptr
=
ctx
.
builder
.
alloca
(
ctx
.
i32
)
ctx
.
builder
.
store
(
c
ore
.
Constant
.
int
(
ctx
.
i32
,
0
),
idx_ptr
)
ctx
.
builder
.
store
(
l
c
.
Constant
.
int
(
ctx
.
i32
,
0
),
idx_ptr
)
ctx
.
builder
.
branch
(
body_block
)
...
...
@@ -1126,7 +1128,7 @@ def _expr_logic(expr, ctx):
ctx
.
builder
.
position_at_end
(
next_block
)
idx_tmp_val
=
ctx
.
builder
.
add
(
idx_val
,
ctx
.
one
)
ctx
.
builder
.
store
(
idx_tmp_val
,
idx_ptr
)
end_cond_val
=
ctx
.
builder
.
icmp
(
c
ore
.
ICMP_SGE
,
idx_tmp_val
,
len_val
)
end_cond_val
=
ctx
.
builder
.
icmp
(
l
c
.
ICMP_SGE
,
idx_tmp_val
,
len_val
)
ctx
.
builder
.
cbranch
(
end_cond_val
,
end_block
,
body_block
)
# end block
...
...
@@ -1153,13 +1155,13 @@ def _expr_not(expr, ctx):
end_block
=
func
.
append_basic_block
(
'not:end'
)
idx_ptr
=
ctx
.
builder
.
alloca
(
ctx
.
i32
)
ctx
.
builder
.
store
(
c
ore
.
Constant
.
int
(
ctx
.
i32
,
0
),
idx_ptr
)
ctx
.
builder
.
store
(
l
c
.
Constant
.
int
(
ctx
.
i32
,
0
),
idx_ptr
)
struct_ptr
=
expression
(
expr
.
expr
,
ctx
)
res_struct_ptr
=
ctx
.
builder
.
alloca
(
struct_ptr
.
type
.
pointee
)
array_llty
=
struct_ptr
.
type
.
pointee
.
elements
[
0
]
len_val
=
c
ore
.
Constant
.
int
(
ctx
.
i32
,
array_llty
.
count
)
len_val
=
l
c
.
Constant
.
int
(
ctx
.
i32
,
array_llty
.
count
)
ctx
.
builder
.
branch
(
body_block
)
...
...
@@ -1179,7 +1181,7 @@ def _expr_not(expr, ctx):
ctx
.
builder
.
position_at_end
(
next_block
)
idx_tmp_val
=
ctx
.
builder
.
add
(
idx_val
,
ctx
.
one
)
ctx
.
builder
.
store
(
idx_tmp_val
,
idx_ptr
)
end_cond_val
=
ctx
.
builder
.
icmp
(
c
ore
.
ICMP_SGE
,
idx_tmp_val
,
len_val
)
end_cond_val
=
ctx
.
builder
.
icmp
(
l
c
.
ICMP_SGE
,
idx_tmp_val
,
len_val
)
ctx
.
builder
.
cbranch
(
end_cond_val
,
end_block
,
body_block
)
ctx
.
builder
.
position_at_end
(
end_block
)
...
...
@@ -1197,7 +1199,7 @@ def _expr_append(expr, ctx):
if
basic_asn1ty
.
kind
in
(
'SequenceOfType'
,
'OctetStringType'
):
res_llty
=
ctx
.
lltype_of
(
expr
.
exprType
)
elem_llty
=
res_llty
.
elements
[
1
].
element
elem_size_val
=
c
ore
.
Constant
.
sizeof
(
elem_llty
)
elem_size_val
=
l
c
.
Constant
.
sizeof
(
elem_llty
)
res_ptr
=
ctx
.
builder
.
alloca
(
res_llty
)
res_len_ptr
=
ctx
.
builder
.
gep
(
res_ptr
,
[
ctx
.
zero
,
ctx
.
zero
])
...
...
@@ -1228,8 +1230,8 @@ def _expr_append(expr, ctx):
ctx
.
builder
.
bitcast
(
res_arr_ptr
,
ctx
.
i8_ptr
),
ctx
.
builder
.
bitcast
(
left_arr_ptr
,
ctx
.
i8_ptr
),
ctx
.
builder
.
mul
(
elem_size_val
,
ctx
.
builder
.
zext
(
left_count_val
,
ctx
.
i64
)),
c
ore
.
Constant
.
int
(
ctx
.
i32
,
0
),
c
ore
.
Constant
.
int
(
ctx
.
i1
,
0
)
l
c
.
Constant
.
int
(
ctx
.
i32
,
0
),
l
c
.
Constant
.
int
(
ctx
.
i1
,
0
)
],
ctx
)
res_arr_ptr
=
ctx
.
builder
.
gep
(
res_ptr
,
[
ctx
.
zero
,
ctx
.
one
,
left_count_val
])
...
...
@@ -1238,8 +1240,8 @@ def _expr_append(expr, ctx):
ctx
.
builder
.
bitcast
(
res_arr_ptr
,
ctx
.
i8_ptr
),
ctx
.
builder
.
bitcast
(
right_arr_ptr
,
ctx
.
i8_ptr
),
ctx
.
builder
.
mul
(
elem_size_val
,
ctx
.
builder
.
zext
(
right_count_val
,
ctx
.
i64
)),
c
ore
.
Constant
.
int
(
ctx
.
i32
,
0
),
c
ore
.
Constant
.
int
(
ctx
.
i1
,
0
)
l
c
.
Constant
.
int
(
ctx
.
i32
,
0
),
l
c
.
Constant
.
int
(
ctx
.
i1
,
0
)
],
ctx
)
return
res_ptr
...
...
@@ -1263,7 +1265,7 @@ def _expr_in(expr, ctx):
is_variable_size
=
seqof_basic_asn1ty
.
Min
!=
seqof_basic_asn1ty
.
Max
idx_ptr
=
ctx
.
builder
.
alloca
(
ctx
.
i32
)
ctx
.
builder
.
store
(
c
ore
.
Constant
.
int
(
ctx
.
i32
,
0
),
idx_ptr
)
ctx
.
builder
.
store
(
l
c
.
Constant
.
int
(
ctx
.
i32
,
0
),
idx_ptr
)
value_val
=
expression
(
expr
.
right
,
ctx
)
seqof_val
=
expression
(
expr
.
left
,
ctx
)
...
...
@@ -1275,7 +1277,7 @@ def _expr_in(expr, ctx):
end_idx
=
ctx
.
builder
.
load
(
ctx
.
builder
.
gep
(
seqof_val
,
[
ctx
.
zero
,
ctx
.
zero
]))
else
:
array_llty
=
seqof_val
.
type
.
pointee
.
elements
[
0
]
end_idx
=
c
ore
.
Constant
.
int
(
ctx
.
i32
,
array_llty
.
count
)
end_idx
=
l
c
.
Constant
.
int
(
ctx
.
i32
,
array_llty
.
count
)
ctx
.
builder
.
branch
(
check_block
)
...
...
@@ -1300,7 +1302,7 @@ def _expr_in(expr, ctx):
ctx
.
builder
.
position_at_end
(
next_block
)
idx_tmp_val
=
ctx
.
builder
.
add
(
idx_val
,
ctx
.
one
)
ctx
.
builder
.
store
(
idx_tmp_val
,
idx_ptr
)
end_cond_val
=
ctx
.
builder
.
icmp
(
c
ore
.
ICMP_SGE
,
idx_tmp_val
,
end_idx
)
end_cond_val
=
ctx
.
builder
.
icmp
(
l
c
.
ICMP_SGE
,
idx_tmp_val
,
end_idx
)
ctx
.
builder
.
cbranch
(
end_cond_val
,
end_block
,
check_block
)
ctx
.
builder
.
position_at_end
(
end_block
)
...
...
@@ -1378,7 +1380,7 @@ def _prim_enumerated_value(prim, ctx):