-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexec.c
125 lines (108 loc) · 2.83 KB
/
exec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* a3e - ARMv7 emulator
* Copyright (C) 2011 Guillermo Ramos <[email protected]>
*
* This file is part of a3e
*
* a3e is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* a3e is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with a3e. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <libdisarm/disarm.h>
#include <stdio.h>
#include <unistd.h>
#include "types.h"
#include "memory.h"
#include "pipeline.h"
void parse(struct instruction *inst)
{
da_instr_t i;
if (inst->val._u32 == 0xffffffff) {
printf(".exit\n");
return;
}
da_instr_parse(&i, inst->val._u32, 0);
da_instr_parse_args(&inst->args, &i);
printf("%x\t", inst->val._u32);
da_instr_fprint(stdout, &i, &inst->args, 0);
inst->group = i.group;
inst->cond = da_instr_get_cond(&i);
putchar('\n');
}
inline void not_implemented(char *where, da_data_op_t op)
{
fprintf(stdout, "\tOp #%d not implemented in inst. type %s\n", op, where);
}
void exec_data_imm(da_args_data_imm_t *args)
{
if (args->op == DA_DATA_OP_MOV || args->op == DA_DATA_OP_MVN)
r[args->rd] = args->imm;
else
not_implemented("data_imm", args->op);
}
void exec_data_imm_sh(da_args_data_imm_sh_t *args)
{
// printf("rd:%d rn:%d rm:%d sh:%d sha:%d\n", args->rd, args->rn, args->rm,
// args->sh, args->sha);
if (args->op == DA_DATA_OP_ADD)
r[args->rd] = r[args->rn] + r[args->rm];
else if (args->op == DA_DATA_OP_SUB)
r[args->rd] = r[args->rn] - r[args->rm];
else
not_implemented("data_imm_sh", args->op);
}
void exec_swi(da_args_swi_t *args)
{
int nr = args->imm;
if (nr == 0) {
r[1] = syscall(r[7], r[0], r[1], r[2], r[3], r[4]);
printf("\t--> returned %d\n", r[1]);
} else {
fprintf(stderr, "Software interrupt #%x not implemented\n", nr);
}
}
void exec_bl(da_args_bl_t *args)
{
b(args->off - 1);
}
int exec(struct instruction *inst)
{
da_instr_args_t args;
parse(inst);
args = inst->args;
if (inst->val._u32 == 0xffffffff) { // exit
return -1;
}
switch (inst->group) {
case DA_GROUP_DATA_IMM:
// printf("Entering data_imm\n");
exec_data_imm(&args.data_imm);
break;
case DA_GROUP_SWI:
// printf("Entering swi\n");
exec_swi(&args.swi);
break;
case DA_GROUP_BL:
// printf("Entering group_bl\n");
exec_bl(&args.bl);
break;;
case DA_GROUP_DATA_IMM_SH:
// printf("Entering data_imm_sh\n");
exec_data_imm_sh(&args.data_imm_sh);
break;
default:
printf("\t--> NOT EXECUTED\n");
break;
}
return 0;
}