2016-12-02 09:51:18 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2016-12-05 18:59:44 +00:00
|
|
|
#include "../dasm_proto.h"
|
|
|
|
#include "../dasm_s390x.h"
|
2016-12-02 09:51:18 +00:00
|
|
|
|
2016-12-05 18:59:44 +00:00
|
|
|
// DynASM directives.
|
|
|
|
|.arch s390x
|
|
|
|
|.actionlist actions
|
2016-12-02 09:51:18 +00:00
|
|
|
|
2016-12-05 18:59:44 +00:00
|
|
|
static void add(dasm_State *state)
|
2016-12-05 08:16:44 +00:00
|
|
|
{
|
2016-12-05 18:59:44 +00:00
|
|
|
dasm_State ** Dst = &state;
|
|
|
|
|
|
|
|
| ar r2,r3
|
|
|
|
| br r14
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sub(dasm_State *state)
|
|
|
|
{
|
|
|
|
dasm_State **Dst = &state;
|
|
|
|
|
|
|
|
| sr r2,r3
|
|
|
|
| br r14
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mul(dasm_State *state)
|
|
|
|
{
|
|
|
|
dasm_State **Dst = &state;
|
|
|
|
|
|
|
|
| msr r2 , r3
|
|
|
|
| br r14
|
|
|
|
}
|
|
|
|
|
2016-12-05 19:51:48 +00:00
|
|
|
static void rx(dasm_State *state)
|
|
|
|
{
|
|
|
|
dasm_State **Dst = &state;
|
|
|
|
|
|
|
|
int x = 1;
|
|
|
|
int y = 4095;
|
|
|
|
|
|
|
|
| la r4, 4095(r2, r3)
|
|
|
|
| la r5, 4095(r4)
|
|
|
|
| la r1, x(r5)
|
|
|
|
| la r2, y(r1, r0)
|
|
|
|
| br r14
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rxy(dasm_State *state)
|
|
|
|
{
|
|
|
|
dasm_State **Dst = &state;
|
|
|
|
|
|
|
|
int x = -524287;
|
|
|
|
int y = 524286;
|
|
|
|
|
|
|
|
| lay r4, -524288(r2, r3)
|
|
|
|
| lay r5, 524287(r4)
|
|
|
|
| lay r1, x(r5)
|
|
|
|
| lay r2, y(r1, r0)
|
|
|
|
| br r14
|
|
|
|
}
|
|
|
|
|
2016-12-05 18:59:44 +00:00
|
|
|
typedef struct {
|
2016-12-05 19:51:48 +00:00
|
|
|
int64_t arg1;
|
|
|
|
int64_t arg2;
|
2016-12-05 08:16:44 +00:00
|
|
|
void (*fn)(dasm_State *);
|
2016-12-05 19:51:48 +00:00
|
|
|
int64_t want;
|
2016-12-05 18:59:44 +00:00
|
|
|
const char *testname;
|
|
|
|
} test_table;
|
2016-12-05 08:16:44 +00:00
|
|
|
|
|
|
|
test_table test[] = {
|
2016-12-05 19:51:48 +00:00
|
|
|
{ 1, 2, add, 3, "add"},
|
|
|
|
{10, 5, sub, 5, "sub"},
|
|
|
|
{ 2, 3, mul, 6, "mul"},
|
|
|
|
{ 5, 7, rx, 12298, "rx"},
|
|
|
|
{ 5, 7, rxy, 10, "rxy"}
|
2016-12-05 18:59:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void *jitcode(dasm_State **state, size_t *size)
|
2016-12-02 09:51:18 +00:00
|
|
|
{
|
2016-12-05 18:59:44 +00:00
|
|
|
int dasm_status = dasm_link(state, size);
|
2016-12-02 09:51:18 +00:00
|
|
|
assert(dasm_status == DASM_S_OK);
|
|
|
|
|
2016-12-05 18:59:44 +00:00
|
|
|
void *ret = mmap(0, *size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2016-12-02 09:51:18 +00:00
|
|
|
dasm_encode(state, ret);
|
|
|
|
dasm_free(state);
|
|
|
|
|
2016-12-05 18:59:44 +00:00
|
|
|
mprotect(ret, *size, PROT_READ | PROT_EXEC);
|
2016-12-02 09:51:18 +00:00
|
|
|
return (int *)ret;
|
|
|
|
}
|
|
|
|
|
2016-12-05 18:59:44 +00:00
|
|
|
int main(int argc, char *argv[])
|
2016-12-02 09:51:18 +00:00
|
|
|
{
|
2016-12-05 18:59:44 +00:00
|
|
|
dasm_State *state;
|
2016-12-02 09:51:18 +00:00
|
|
|
|
2016-12-05 18:59:44 +00:00
|
|
|
for(int i=0; i < sizeof(test)/sizeof(test[0]); i++) {
|
|
|
|
dasm_init(&state, 1);
|
|
|
|
dasm_setup(&state, actions);
|
|
|
|
test[i].fn(state);
|
|
|
|
size_t size;
|
2016-12-05 19:51:48 +00:00
|
|
|
int64_t (*fptr)(int64_t, int64_t) = jitcode(&state, &size);
|
|
|
|
int64_t got = fptr(test[i].arg1, test[i].arg2);
|
2016-12-02 09:51:18 +00:00
|
|
|
|
2016-12-05 18:59:44 +00:00
|
|
|
if (got != test[i].want) {
|
2016-12-05 19:51:48 +00:00
|
|
|
fprintf(stderr, "FAIL: test %s: want %ld, got %ld\n", test[i].testname, test[i].want, got);
|
2016-12-05 08:16:44 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2016-12-05 18:59:44 +00:00
|
|
|
munmap(fptr, size);
|
2016-12-05 08:16:44 +00:00
|
|
|
}
|
2016-12-05 18:59:44 +00:00
|
|
|
printf("all tests passed\n");
|
|
|
|
return 0;
|
2016-12-02 09:51:18 +00:00
|
|
|
}
|