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
eb216185
Commit
eb216185
authored
Jul 03, 2014
by
dbarbera
Browse files
Refactor get_string_constant function into string_ptr context method
parent
078ca0b3
Changes
1
Hide whitespace changes
Inline
Side-by-side
LlvmGenerator.py
View file @
eb216185
...
...
@@ -178,6 +178,18 @@ class Context():
struct
=
decl_struct
([
'nCount'
,
'arr'
],
[
ctx
.
i32
,
arr_ty
],
name
)
return
struct
.
ty
def
string_ptr
(
self
,
str
):
''' Returns a pointer to a global string with the given value '''
if
str
in
self
.
strings
:
return
self
.
strings
[
str
].
gep
([
self
.
zero
,
self
.
zero
])
str_val
=
core
.
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
self
.
strings
[
str
]
=
var_ptr
return
var_ptr
.
gep
([
self
.
zero
,
self
.
zero
])
class
StructType
():
def
__init__
(
self
,
name
,
field_names
,
field_types
):
...
...
@@ -488,25 +500,20 @@ def generate_write(params):
expr_val
=
expression
(
param
)
if
basic_ty
.
kind
in
[
'IntegerType'
,
'Integer32Type'
]:
fmt_val
=
get_string_cons
(
'% d'
)
fmt_ptr
=
ctx
.
builder
.
gep
(
fmt_val
,
[
ctx
.
zero
,
ctx
.
zero
])
ctx
.
builder
.
call
(
ctx
.
funcs
[
'printf'
],
[
fmt_ptr
,
expr_val
])
fmt_str_ptr
=
ctx
.
string_ptr
(
'% d'
)
ctx
.
builder
.
call
(
ctx
.
funcs
[
'printf'
],
[
fmt_str_ptr
,
expr_val
])
elif
basic_ty
.
kind
==
'RealType'
:
fmt_val
=
get_string_cons
(
'% .14E'
)
fmt_ptr
=
ctx
.
builder
.
gep
(
fmt_val
,
[
ctx
.
zero
,
ctx
.
zero
])
ctx
.
builder
.
call
(
ctx
.
funcs
[
'printf'
],
[
fmt_ptr
,
expr_val
])
fmt_str_ptr
=
ctx
.
string_ptr
(
'% .14E'
)
ctx
.
builder
.
call
(
ctx
.
funcs
[
'printf'
],
[
fmt_str_ptr
,
expr_val
])
elif
basic_ty
.
kind
==
'BooleanType'
:
true_str_val
=
get_string_cons
(
'TRUE'
)
true_str_ptr
=
ctx
.
builder
.
gep
(
true_str_val
,
[
ctx
.
zero
,
ctx
.
zero
])
false_str_val
=
get_string_cons
(
'FALSE'
)
false_str_ptr
=
ctx
.
builder
.
gep
(
false_str_val
,
[
ctx
.
zero
,
ctx
.
zero
])
true_str_ptr
=
ctx
.
string_ptr
(
'TRUE'
)
false_str_ptr
=
ctx
.
string_ptr
(
'FALSE'
)
str_ptr
=
ctx
.
builder
.
select
(
expr_val
,
true_str_ptr
,
false_str_ptr
)
ctx
.
builder
.
call
(
ctx
.
funcs
[
'printf'
],
[
str_ptr
])
elif
basic_ty
.
kind
in
[
'StringType'
,
'OctetStringType'
]:
fmt_val
=
get_string_cons
(
'%s'
)
fmt_ptr
=
ctx
.
builder
.
gep
(
fmt_val
,
[
ctx
.
zero
,
ctx
.
zero
])
fmt_str_ptr
=
ctx
.
string_ptr
(
'%s'
)
arr_ptr
=
ctx
.
builder
.
gep
(
expr_val
,
[
ctx
.
zero
,
ctx
.
one
])
ctx
.
builder
.
call
(
ctx
.
funcs
[
'printf'
],
[
fmt_ptr
,
arr_ptr
])
ctx
.
builder
.
call
(
ctx
.
funcs
[
'printf'
],
[
fmt_
str_
ptr
,
arr_ptr
])
else
:
raise
NotImplementedError
...
...
@@ -515,9 +522,7 @@ def generate_writeln(params):
''' Generate the code for the writeln operator '''
generate_write
(
params
)
zero
=
core
.
Constant
.
int
(
ctx
.
i32
,
0
)
str_cons
=
get_string_cons
(
'
\n
'
)
str_ptr
=
ctx
.
builder
.
gep
(
str_cons
,
[
zero
,
zero
])
str_ptr
=
ctx
.
string_ptr
(
'
\n
'
)
ctx
.
builder
.
call
(
ctx
.
funcs
[
'printf'
],
[
str_ptr
])
...
...
@@ -1065,8 +1070,7 @@ def _empty_string(primary):
@
expression
.
register
(
ogAST
.
PrimStringLiteral
)
def
_string_literal
(
primary
):
''' Generate code for a string (Octet String) '''
str_val
=
get_string_cons
(
str
(
primary
.
value
[
1
:
-
1
]))
str_ptr
=
ctx
.
builder
.
gep
(
str_val
,
[
ctx
.
zero
,
ctx
.
zero
])
str_ptr
=
ctx
.
string_ptr
(
str
(
primary
.
value
[
1
:
-
1
]))
# Allocate anonymous OctetString struct
str_len
=
len
(
str
(
primary
.
value
[
1
:
-
1
]))
+
1
...
...
@@ -1386,19 +1390,6 @@ def _inner_procedure(proc):
func
.
verify
()
def
get_string_cons
(
str
):
''' Returns a reference to a global string constant with the given value '''
if
str
in
ctx
.
strings
:
return
ctx
.
strings
[
str
]
str_val
=
core
.
Constant
.
stringz
(
str
)
gvar_name
=
'.str%s'
%
len
(
ctx
.
strings
)
gvar_val
=
ctx
.
module
.
add_global_variable
(
str_val
.
type
,
gvar_name
)
gvar_val
.
initializer
=
str_val
ctx
.
strings
[
str
]
=
gvar_val
return
gvar_val
def
decl_func
(
name
,
return_ty
,
param_tys
,
extern
=
False
):
''' Declare a function '''
func_ty
=
core
.
Type
.
function
(
return_ty
,
param_tys
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment