Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
clean-and-itasks
sapl-interpreter
Commits
cdbe907d
Commit
cdbe907d
authored
Mar 19, 2016
by
Laszlo Domoszlai
Browse files
type safer reads from thunks
parent
2473fe68
Changes
1
Hide whitespace changes
Inline
Side-by-side
interpreter/prim.c
View file @
cdbe907d
...
...
@@ -3,6 +3,7 @@
#include <stdio.h>
#include <math.h>
#include "debug.h"
#include "prim.h"
#include "desc.h"
#include "thunk.h"
...
...
@@ -224,7 +225,7 @@ void __or(int dst_idx) {
void
__mod
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
target
->
desc
=
(
Desc
*
)
__INT__
;
target
->
_int
=
read
B
(
arg
(
2
))
%
read
B
(
arg
(
1
));
target
->
_int
=
read
I
(
arg
(
2
))
%
read
I
(
arg
(
1
));
}
void
__C2I
(
int
dst_idx
)
{
...
...
@@ -254,9 +255,12 @@ void __R2R(int dst_idx) {
void
__R2I
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
target
->
desc
=
(
Desc
*
)
__INT__
;
target
->
_int
=
(
int
)
readR
(
arg
(
1
));
double
v
=
readR
(
arg
(
1
));
target
->
_int
=
v
>=
0
?
(
long
)(
v
+
0
.
5
)
:
(
long
)(
v
-
0
.
5
);
}
char
buff
[
30
];
void
__S2R
(
int
dst_idx
)
{
Thunk
*
str
=
arg
(
1
);
...
...
@@ -274,8 +278,7 @@ void __S2R(int dst_idx) {
length
=
str
->
_array
.
length
;
}
char
buff
[
1080
];
if
(
length
>
1079
)
length
=
1079
;
if
(
length
>
29
)
length
=
29
;
memcpy
(
buff
,
chars
,
length
);
buff
[
length
]
=
'\0'
;
...
...
@@ -301,14 +304,13 @@ void __S2I(int dst_idx) {
length
=
str
->
_array
.
length
;
}
char
buff
[
20
];
if
(
length
>
19
)
length
=
19
;
memcpy
(
buff
,
chars
,
length
);
buff
[
length
]
=
'\0'
;
Thunk
*
target
=
get_dst
(
dst_idx
);
target
->
desc
=
(
Desc
*
)
__INT__
;
// use atol instead of strtol, the former overflows (Clen semantics), the latter returns MAX
// use atol instead of strtol, the former overflows (Cle
a
n semantics), the latter returns MAX
target
->
_int
=
atol
(
buff
);
}
...
...
@@ -345,7 +347,7 @@ void __string_select(int dst_idx)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
str
=
arg
(
2
);
Thunk
*
pos
=
arg
(
1
);
int
pos
=
readI
(
arg
(
1
)
)
;
char
*
chars
;
...
...
@@ -359,7 +361,7 @@ void __string_select(int dst_idx)
}
target
->
desc
=
(
Desc
*
)
__CHAR__
;
target
->
_int
=
(
char
)
chars
[
pos
->
_int
];
target
->
_int
=
(
char
)
chars
[
pos
];
}
...
...
@@ -367,9 +369,9 @@ void __array_select(int dst_idx)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
arr
=
arg
(
2
);
Thunk
*
pos
=
arg
(
1
);
int
pos
=
readI
(
arg
(
1
)
)
;
Thunk
*
elem
=
arr
->
_array
.
_elems
[
pos
->
_int
];
Thunk
*
elem
=
arr
->
_array
.
_elems
[
pos
];
if
(
target
!=
NULL
)
{
...
...
@@ -384,30 +386,30 @@ void __array_select_b_b(int dst_idx)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
arr
=
arg
(
2
);
Thunk
*
pos
=
arg
(
1
);
int
pos
=
readI
(
arg
(
1
)
)
;
target
->
desc
=
(
Desc
*
)
__BOOL__
;
target
->
_int
=
(
int
)
arr
->
_array
.
_bools
[
pos
->
_int
];
target
->
_int
=
(
int
)
arr
->
_array
.
_bools
[
pos
];
}
void
__array_select_b_i
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
arr
=
arg
(
2
);
Thunk
*
pos
=
arg
(
1
);
int
pos
=
readI
(
arg
(
1
)
)
;
target
->
desc
=
(
Desc
*
)
__INT__
;
target
->
_int
=
arr
->
_array
.
_ints
[
pos
->
_int
];
target
->
_int
=
arr
->
_array
.
_ints
[
pos
];
}
void
__array_select_b_r
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
arr
=
arg
(
2
);
Thunk
*
pos
=
arg
(
1
);
int
pos
=
readI
(
arg
(
1
)
)
;
target
->
desc
=
(
Desc
*
)
__REAL__
;
target
->
_real
=
arr
->
_array
.
_reals
[
pos
->
_int
];
target
->
_real
=
arr
->
_array
.
_reals
[
pos
];
}
Thunk
*
array_create
(
Thunk
*
target
,
int
elementsize
,
int
len
,
ArrayElementType
type
)
...
...
@@ -443,48 +445,48 @@ Thunk* string_create(Thunk* target, int len)
void
__string_create1
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
len
=
arg
(
1
);
set_return
(
dst_idx
,
string_create
(
target
,
len
->
_int
));
int
len
=
readI
(
arg
(
1
)
)
;
set_return
(
dst_idx
,
string_create
(
target
,
len
));
}
void
__array_create1
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
len
=
arg
(
1
);
set_return
(
dst_idx
,
array_create
(
target
,
sizeof
(
void
*
),
len
->
_int
,
AET_OTHER
));
int
len
=
readI
(
arg
(
1
)
)
;
set_return
(
dst_idx
,
array_create
(
target
,
sizeof
(
void
*
),
len
,
AET_OTHER
));
}
void
__array_create1_b_i
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
len
=
arg
(
1
);
set_return
(
dst_idx
,
array_create
(
target
,
sizeof
(
int
),
len
->
_int
,
AET_INT
));
int
len
=
readI
(
arg
(
1
)
)
;
set_return
(
dst_idx
,
array_create
(
target
,
sizeof
(
int
),
len
,
AET_INT
));
}
void
__array_create1_b_b
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
len
=
arg
(
1
);
set_return
(
dst_idx
,
array_create
(
target
,
sizeof
(
char
),
len
->
_int
,
AET_BOOL
));
int
len
=
readI
(
arg
(
1
)
)
;
set_return
(
dst_idx
,
array_create
(
target
,
sizeof
(
char
),
len
,
AET_BOOL
));
}
void
__array_create1_b_r
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
len
=
arg
(
1
);
set_return
(
dst_idx
,
array_create
(
target
,
sizeof
(
double
),
len
->
_int
,
AET_REAL
));
int
len
=
readI
(
arg
(
1
)
)
;
set_return
(
dst_idx
,
array_create
(
target
,
sizeof
(
double
),
len
,
AET_REAL
));
}
void
__string_create2
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
len
=
arg
(
2
);
Thunk
*
ch
=
arg
(
1
);
int
len
=
readI
(
arg
(
2
)
)
;
char
ch
=
readC
(
arg
(
1
)
)
;
target
=
string_create
(
target
,
len
->
_int
);
for
(
int
i
=
0
;
i
<
len
->
_int
;
i
++
)
target
=
string_create
(
target
,
len
);
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
target
->
_array
.
_chars
[
i
]
=
(
char
)
ch
->
_int
;
target
->
_array
.
_chars
[
i
]
=
(
char
)
ch
;
}
set_return
(
dst_idx
,
target
);
...
...
@@ -493,11 +495,11 @@ void __string_create2(int dst_idx)
void
__array_create2
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
len
=
arg
(
2
);
int
len
=
readI
(
arg
(
2
)
)
;
Thunk
*
def
=
arg
(
1
);
target
=
array_create
(
target
,
sizeof
(
Thunk
*
),
len
->
_int
,
AET_OTHER
);
for
(
int
i
=
0
;
i
<
len
->
_int
;
i
++
)
target
=
array_create
(
target
,
sizeof
(
Thunk
*
),
len
,
AET_OTHER
);
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
target
->
_array
.
_elems
[
i
]
=
def
;
}
...
...
@@ -508,11 +510,11 @@ void __array_create2(int dst_idx)
void
__array_create2_b_i
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
len
=
arg
(
2
);
int
len
=
readI
(
arg
(
2
)
)
;
Thunk
*
def
=
arg
(
1
);
target
=
array_create
(
target
,
sizeof
(
int
),
len
->
_int
,
AET_INT
);
for
(
int
i
=
0
;
i
<
len
->
_int
;
i
++
)
target
=
array_create
(
target
,
sizeof
(
int
),
len
,
AET_INT
);
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
target
->
_array
.
_ints
[
i
]
=
def
->
_int
;
}
...
...
@@ -523,13 +525,13 @@ void __array_create2_b_i(int dst_idx)
void
__array_create2_b_b
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
len
=
arg
(
2
);
Thunk
*
def
=
arg
(
1
);
int
len
=
readI
(
arg
(
2
)
)
;
int
def
=
readB
(
arg
(
1
)
)
;
target
=
array_create
(
target
,
sizeof
(
char
),
len
->
_int
,
AET_BOOL
);
for
(
int
i
=
0
;
i
<
len
->
_int
;
i
++
)
target
=
array_create
(
target
,
sizeof
(
char
),
len
,
AET_BOOL
);
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
target
->
_array
.
_bools
[
i
]
=
def
->
_int
;
target
->
_array
.
_bools
[
i
]
=
def
;
}
set_return
(
dst_idx
,
target
);
...
...
@@ -538,13 +540,13 @@ void __array_create2_b_b(int dst_idx)
void
__array_create2_b_r
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
len
=
arg
(
2
);
Thunk
*
def
=
arg
(
1
);
int
len
=
readI
(
arg
(
2
)
)
;
double
def
=
readR
(
arg
(
1
)
)
;
target
=
array_create
(
target
,
sizeof
(
double
),
len
->
_int
,
AET_REAL
);
for
(
int
i
=
0
;
i
<
len
->
_int
;
i
++
)
target
=
array_create
(
target
,
sizeof
(
double
),
len
,
AET_REAL
);
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
target
->
_array
.
_reals
[
i
]
=
def
->
_real
;
target
->
_array
.
_reals
[
i
]
=
def
;
}
set_return
(
dst_idx
,
target
);
...
...
@@ -554,8 +556,8 @@ void __string_update_copy(int dst_idx)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
str
=
arg
(
3
);
Thunk
*
idx
=
arg
(
2
);
Thunk
*
ch
=
arg
(
1
);
int
idx
=
readI
(
arg
(
2
)
)
;
char
ch
=
readC
(
arg
(
1
)
)
;
int
length
;
char
*
chars
;
...
...
@@ -574,17 +576,18 @@ void __string_update_copy(int dst_idx)
target
=
string_create
(
target
,
length
);
memcpy
(
target
->
_array
.
_chars
,
chars
,
length
);
target
->
_array
.
_chars
[
idx
->
_int
]
=
(
char
)
ch
->
_int
;
target
->
_array
.
_chars
[
idx
]
=
(
char
)
ch
;
set_return
(
dst_idx
,
target
);
}
void
__string_update
(
int
dst_idx
)
{
Thunk
*
arr
=
arg
(
3
);
Thunk
*
idx
=
arg
(
2
);
Thunk
*
elem
=
arg
(
1
);
int
idx
=
readI
(
arg
(
2
)
)
;
char
elem
=
readC
(
arg
(
1
)
)
;
arr
->
_array
.
_chars
[
idx
->
_int
]
=
(
char
)
elem
->
_int
;
arr
->
_array
.
_chars
[
idx
]
=
(
char
)
elem
;
set_return
(
dst_idx
,
arr
);
}
...
...
@@ -592,10 +595,10 @@ void __string_update(int dst_idx)
void
__array_update
(
int
dst_idx
)
{
Thunk
*
arr
=
arg
(
3
);
Thunk
*
idx
=
arg
(
2
);
int
idx
=
readI
(
arg
(
2
)
)
;
Thunk
*
elem
=
arg
(
1
);
arr
->
_array
.
_elems
[
idx
->
_int
]
=
elem
;
arr
->
_array
.
_elems
[
idx
]
=
elem
;
set_return
(
dst_idx
,
arr
);
}
...
...
@@ -603,10 +606,10 @@ void __array_update(int dst_idx)
void
__array_update_b_i
(
int
dst_idx
)
{
Thunk
*
arr
=
arg
(
3
);
Thunk
*
idx
=
arg
(
2
);
Thunk
*
elem
=
arg
(
1
);
int
idx
=
readI
(
arg
(
2
)
)
;
int
elem
=
readI
(
arg
(
1
)
)
;
arr
->
_array
.
_ints
[
idx
->
_int
]
=
elem
->
_int
;
arr
->
_array
.
_ints
[
idx
]
=
elem
;
set_return
(
dst_idx
,
arr
);
}
...
...
@@ -614,10 +617,10 @@ void __array_update_b_i(int dst_idx)
void
__array_update_b_b
(
int
dst_idx
)
{
Thunk
*
arr
=
arg
(
3
);
Thunk
*
idx
=
arg
(
2
);
Thunk
*
elem
=
arg
(
1
);
int
idx
=
readI
(
arg
(
2
)
)
;
int
elem
=
readB
(
arg
(
1
)
)
;
arr
->
_array
.
_bools
[
idx
->
_int
]
=
(
unsigned
char
)
elem
->
_int
;
arr
->
_array
.
_bools
[
idx
]
=
(
unsigned
char
)
elem
;
set_return
(
dst_idx
,
arr
);
}
...
...
@@ -625,10 +628,10 @@ void __array_update_b_b(int dst_idx)
void
__array_update_b_r
(
int
dst_idx
)
{
Thunk
*
arr
=
arg
(
3
);
Thunk
*
idx
=
arg
(
2
);
Thunk
*
elem
=
arg
(
1
);
int
idx
=
readI
(
arg
(
2
)
)
;
double
elem
=
readR
(
arg
(
1
)
)
;
arr
->
_array
.
_reals
[
idx
->
_int
]
=
elem
->
_real
;
arr
->
_array
.
_reals
[
idx
]
=
elem
;
set_return
(
dst_idx
,
arr
);
}
...
...
@@ -637,8 +640,8 @@ void __string_slice(int dst_idx)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
str
=
arg
(
3
);
Thunk
*
idx1
=
arg
(
2
);
Thunk
*
idx2
=
arg
(
1
);
int
idx1
=
readI
(
arg
(
2
)
)
;
int
idx2
=
readI
(
arg
(
1
)
)
;
char
*
chars
;
int
full_length
;
...
...
@@ -654,11 +657,11 @@ void __string_slice(int dst_idx)
full_length
=
str
->
_array
.
length
;
}
int
length
=
idx2
->
_int
-
idx1
->
_int
+
1
;
if
(
idx1
->
_int
+
length
>
full_length
)
length
=
full_length
-
idx1
->
_int
;
int
length
=
idx2
-
idx1
+
1
;
if
(
idx1
+
length
>
full_length
)
length
=
full_length
-
idx1
;
target
=
string_create
(
target
,
length
);
memcpy
(
target
->
_array
.
_chars
,
chars
+
idx1
->
_int
,
length
);
memcpy
(
target
->
_array
.
_chars
,
chars
+
idx1
,
length
);
set_return
(
dst_idx
,
target
);
}
...
...
@@ -811,19 +814,18 @@ void __ltS(int dst_idx)
void
__C2S
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
ch
=
arg
(
1
);
char
ch
=
readC
(
arg
(
1
)
)
;
target
=
string_create
(
target
,
1
);
target
->
_array
.
_chars
[
0
]
=
(
char
)
ch
->
_int
;
target
->
_array
.
_chars
[
0
]
=
(
char
)
ch
;
set_return
(
dst_idx
,
target
);
}
void
__I2S
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
i
=
arg
(
1
);
int
i
=
readI
(
arg
(
1
)
)
;
char
buff
[
20
];
itoa
(
i
->
_int
,
buff
,
10
);
itoa
(
i
,
buff
,
10
);
int
len
=
strlen
(
buff
);
target
=
string_create
(
target
,
len
);
...
...
@@ -833,11 +835,9 @@ void __I2S(int dst_idx) {
void
__R2S
(
int
dst_idx
)
{
Thunk
*
target
=
get_dst
(
dst_idx
);
Thunk
*
r
=
arg
(
1
);
double
r
=
readR
(
arg
(
1
)
)
;
char
buff
[
1080
];
snprintf
(
buff
,
1080
,
"%f"
,
r
->
_real
);
int
len
=
strlen
(
buff
);
int
len
=
snprintf
(
buff
,
30
,
"%g"
,
r
);
target
=
string_create
(
target
,
len
);
memcpy
(
target
->
_array
.
_chars
,
buff
,
len
);
...
...
Write
Preview
Markdown
is supported
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