Commit 242dfd6f authored by Axel Simon's avatar Axel Simon

Merge branch 'experimental' of versioncontrolseidl.in.tum.de:jucs/gdsl-toolkit

Conflicts:
	detail/codegen/c1/runtime.c
parents 89b41318 a13120d4
This diff is collapsed.
......@@ -22,13 +22,11 @@ struct state {
char* heap; /* current top of the heap */
@state_type@
; /* the current monadic state */
char* ip_start; /* beginning of code buffer */
unsigned char* ip_start; /* beginning of code buffer */
size_t ip_base; /* base address of code */
char* ip_limit; /* first byte beyond the code buffer */
char* ip; /* current pointer into the buffer */
unsigned char* buf_be;
char le;
char token_size;
unsigned char* ip_limit; /* first byte beyond the code buffer */
unsigned char* ip; /* current pointer into the buffer */
size_t token_addr_inv;
char* err_str; /* a string describing the fatal error that occurred */
jmp_buf err_tgt; /* the position of the exception handler */
FILE* handle; /* the file that the puts primitve uses */
......@@ -233,50 +231,27 @@ static inline int_t consume(state_t s, char size) {
longjmp(s->err_tgt, 1);
};
int_t result = 0;
char size_left = size;
while(size_left) {
char be_buf_left = -((size_t) s->buf_be) & (s->token_size - 1);
if(!be_buf_left) {
s->buf_be -= s->token_size;
int i;
for(i = 0; i < s->token_size; i++)
s->buf_be[i] = s->le ? s->ip[s->token_size - i - 1] : s->ip[i];
be_buf_left += s->token_size;
}
for(; be_buf_left && size_left; be_buf_left--) {
result |= *(s->buf_be++) << (--size_left * 8);
s->ip++;
}
}
while(size)
result |= s->ip_start[(s->ip++ - s->ip_start) ^ s->token_addr_inv] << (--size*8);
return result;
}
static inline void unconsume(state_t s, char size) {
char be_buf_consumed = ((size_t) s->buf_be) & (s->token_size - 1);
if(size < be_buf_consumed) {
s->buf_be -= size;
s->ip -= size;
} else if(size == be_buf_consumed) {
s->buf_be += (s->token_size - size);
s->ip -= size;
} else {
char be_buf_left = -((size_t) s->buf_be) & (s->token_size - 1);
s->buf_be += be_buf_left;
char size_left = size - be_buf_consumed;
s->ip -= be_buf_consumed;
char tokens = size_left / s->token_size;
s->ip -= (tokens + 1) * s->token_size;
char inner_token = size_left % s->token_size;
consume(s, s->token_size - inner_token);
}
s->ip -= size;
}
void
@endianness@
(state_t s, int_t kind, int_t size) {
(state_t s, int_t le, int_t size) {
if (size != 1 && size != 2 && size != 4 && size != 8) {
s->err_str = "GDSL runtime: endianness(); invalid token size";
longjmp(s->err_tgt, 100);
};
if (le != 0 && le != 1) {
s->err_str = "GDSL runtime: endianness(); invalid kind";
longjmp(s->err_tgt, 101);
};
s->token_addr_inv = le * (size - 1);
}
static int_t vec_to_signed(state_t s, vec_t v) {
......@@ -344,7 +319,10 @@ static string_t int_to_string(state_t s, int_t v) {
void
@set_code@
(state_t s, char* buf, size_t buf_len, size_t base) {
(state_t s, unsigned char* buf, size_t buf_len, size_t base) {
/*
* Todo: fix signedness
*/
s->ip = buf;
s->ip_limit = buf+buf_len;
s->ip_start = buf;
......@@ -420,6 +398,10 @@ state_t
s->heap_base = NULL;
s->heap_limit = NULL;
s->heap = NULL;
@endianness@
(s, 0, 1);
return s;
}
......@@ -434,7 +416,7 @@ state_t
#endif
#define BUF_SIZE 32*1024*1024
static char blob[BUF_SIZE];
static unsigned char blob[BUF_SIZE];
int main (int argc, char** argv) {
uint64_t buf_size = BUF_SIZE;
......
......@@ -40,7 +40,7 @@ state_t
in GDSL returns when no bytes have been consumed. */
void
@set_code@
(state_t s, char* buf, size_t buf_len, size_t base);
(state_t s, unsigned char* buf, size_t buf_len, size_t base);
/* Query the offset of the current IP relative to base. */
size_t
......
This diff is collapsed.
......@@ -35,7 +35,7 @@ public:
* generic
*/
int_t get_ip_offset();
void set_code(char *buffer, uint64_t size, uint64_t base);
void set_code(unsigned char *buffer, uint64_t size, uint64_t base);
bool seek(int_t ip);
void reset_heap();
......
......@@ -49,10 +49,9 @@ int_t gdsl::gdsl::get_ip_offset() {
return frontend->native().generic.get_ip_offset(gdsl_state);
}
void gdsl::gdsl::set_code(char *buffer, uint64_t size, uint64_t base) {
void gdsl::gdsl::set_code(unsigned char *buffer, uint64_t size, uint64_t base) {
if(setjmp(*frontend->native().generic.err_tgt(gdsl_state)))
throw gdsl_exception("set_code() failed", string(frontend->native().generic.get_error_message(gdsl_state)));
frontend->native().generic.set_code(gdsl_state, buffer, size, base);
}
......
......@@ -500,10 +500,6 @@ callbacks_t rreil_gdrr_builder_callbacks_get(state_t state) {
callbacks_heap->callbacks = callbacks;
// config.callbacks.sem_stmts.sem_cons = &sem_cons;
// config.callbacks.sem_stmts.sem_nil = &sem_nil;
// config.gdrr_config_stmts_handling = GDRR_CONFIG_STMTS_HANDLING_RECURSIVE;
return &callbacks_heap->callbacks;
}
......@@ -525,24 +521,4 @@ std::vector<gdsl::rreil::statement*>* gdsl::rreil_builder::convert(obj_t rreil)
return v;
}
//std::vector<statement*> *gdsl::rreil_builder::translate() {
// char err = gdsl_multiplex_frontend_get_by_lib_name(&frontend, "x86");
// if(err != GDSL_MULTIPLEX_ERROR_NONE) throw "blah";
//
// uint16_t buffer = 0;
// state_t s = frontend.generic.init();
// frontend.generic.set_code(s, (char*)&buffer, 2, 0);
//
// obj_t insn = frontend.decoder.decode(s, frontend.decoder.config_default(s));
// obj_t rreil = frontend.translator.translate(s, insn);
//
// callbacks_t cbs = rreil_gdrr_builder_callbacks_get(s);
//
// auto v = (std::vector<statement*> *)frontend.translator.rreil_convert_sem_stmts(s, cbs, rreil);
//
// for (statement *stmt : *v) {
// printf("%s\n", stmt->to_string().c_str());
// }
//
// return NULL;
//}
......@@ -12,7 +12,7 @@ struct frontend_desc {
struct _generic {
state_t function () init;
void function (state_t, char*, ulong, ulong) set_code;
void function (state_t, ubyte*, ulong, ulong) set_code;
char function (state_t, int_t) seek;
jmp_buf* function (state_t) err_tgt;
string_t function (state_t, obj_t) merge_rope;
......@@ -69,4 +69,4 @@ unittest {
import std.stdio;
frontend f;
// writeln(f.sizeof);
}
\ No newline at end of file
}
......@@ -40,7 +40,7 @@ class Binop : Expression {
}
public override string toString() {
return _opnd1.toString ~ " " ~ cast(string)_type ~ " " ~ _opnd2.toString();
return _opnd1.toString() ~ " " ~ cast(string)_type ~ " " ~ _opnd2.toString();
}
}
......
module gdsl.rreil.expr_cmp;
module gdsl.rreil.expr_cmp.expr_cmp;
import gdsl.rreil.linear.linear;
enum CompareOpType : string {
CMPEQ = "==",
CMPNEQ = "!=",
CMPLES = "<=s",
CMPLEU = "<=u",
CMPLTS = "<s",
CMPLTU = "<u"
}
class CompareExpression {
public abstract string toString();
}
\ No newline at end of file
private CompareOpType _op;
@property public CompareOpType op() {
return op;
}
private Linear _opnd1;
@property public Linear opnd1() {
return _opnd1;
}
private Linear _opnd2;
@property public Linear opnd2() {
return _opnd2;
}
public this(CompareOpType op, Linear opnd1, Linear opnd2) {
this._op = op;
this._opnd1 = opnd1;
this._opnd2 = opnd2;
}
public override string toString() {
return _opnd1.toString() ~ " " ~ cast(string)_op ~ _opnd2.toString();
}
}
......@@ -20,7 +20,5 @@ class CompareExpression : Sexpr {
}
unittest {
import gdsl.rreil.linear.immediate;
Linear l = new Linear(new Immediate(99));
assert(l.toString() == "99");
// CompareExpression ce = new CompareExpression(new expr_cmp.CompareExpression(
}
\ No newline at end of file
......@@ -21,7 +21,7 @@ struct frontend_desc {
struct frontend {
struct {
state_t (*init)();
void (*set_code)(state_t state, char *buffer, uint64_t size, uint64_t base);
void (*set_code)(state_t state, unsigned char *buffer, uint64_t size, uint64_t base);
char (*seek)(state_t state, int_t ip);
jmp_buf *(*err_tgt)(state_t s);
string_t (*merge_rope)(state_t s, obj_t rope);
......
......@@ -81,7 +81,7 @@ JNIEXPORT void JNICALL Java_gdsl_Gdsl_setCode(JNIEnv *env, jobject this, jlong f
if(setjmp(*frontend->generic.err_tgt(state)))
THROW_GDSL_ERROR()
frontend->generic.set_code(state, (char*)(buffer + offset), (uint64_t)(size - offset), (uint64_t)base);
frontend->generic.set_code(state, (unsigned char*)(buffer + offset), (uint64_t)(size - offset), (uint64_t)base);
}
static obj_t decode_one(JNIEnv *env, jobject this, jlong frontendPtr, jlong gdslStatePtr, int_t decode_config) {
......
......@@ -343,7 +343,7 @@ struct tester_result tester_test_binary(void (*name)(char *), char fork_, uint8_
result.type = TESTER_RTYPE_SUCCESS;
state_t state = gdsl_init();
gdsl_set_code(state, (char*)data, data_size, 0);
gdsl_set_code(state, data, data_size, 0);
obj_t insn;
int_t features;
......
......@@ -117,7 +117,6 @@ val show/register r =
| F29: "f29"
| F30: "f30"
| F31: "f31"
| F x: "f" +++ show-int x
| FIR: "fir"
| FCCR: "fccr"
| FEXR: "fexr"
......
......@@ -70,9 +70,38 @@ val arch-show-id r =
| Sem_F29: "f29"
| Sem_F30: "f30"
| Sem_F31: "f31"
| Sem_FIR: "FIR"
| Sem_FCCR: "FCCR"
| Sem_FEXR: "FEXR"
| Sem_FENR: "FENR"
| Sem_FCSR: "FCSR"
| Sem_SREG: "SREG"
| Sem_LLBIT: "LLBit"
| Sem_DEBUG: "Debug"
| Sem_CONFIG1: "Config1"
| Sem_ISA_MODE: "ISA Mode"
| Sem_SRSCTL: "SRSCtl"
| Sem_CONFIG3: "Config3"
| Sem_EPC: "EPC"
| Sem_ERROR_EPC: "ErrorEPC"
| Sem_DEPC: "DEPC"
| Sem_CPUNUM: "CPUNum"
| Sem_SYNCI_STEP: "SYNCI Step Size"
| Sem_CC: "CC"
| Sem_CCRES: "CCRes"
| Sem_ULR: "ULR"
end
val pretty-arch-id r = arch-show-id r
val pretty-arch-exception exception = ""
val pretty-arch-exception exception =
case exception of
SEM_EXC_OVERFLOW : "{Exception: Overflow}"
| SEM_EXC_VADDR_ERROR : "{Exception: Virtual Address Error}"
| SEM_EXC_TRAP : "{Exception: Trap}"
| SEM_EXC_SYSTEM_CALL : "{Exception: System Call}"
| SEM_EXC_BREAKPOINT : "{Exception: Breakpoint}"
| SEM_EXC_DEBUG_BREAKPOINT : "{Exception: Debug Breakpoint}"
| SEM_EXC_DEBUG_MODE_BREAKPOINT : "{Exception: Debug Mode Breakpoint}"
| SEM_EXC_RESERVED_INSTRUCTION : "{Exception: Reserved Instruction}"
end
type sem_id =
Sem_PC
| Sem_SREG
| Sem_LLBIT
| Sem_DEBUG
| Sem_CONFIG1
| Sem_CONFIG3
| Sem_ISA_MODE
| Sem_SRSCTL
| Sem_EPC
| Sem_ERROR_EPC
| Sem_DEPC
type sem_id =
Sem_ZERO
......@@ -71,15 +80,57 @@ type sem_id =
| Sem_F29
| Sem_F30
| Sem_F31
| Sem_FIR
| Sem_FCCR
| Sem_FEXR
| Sem_FENR
| Sem_FCSR
val fRE = sem-reg-offset (sreg-get) 25
type sem_id =
Sem_CPUNUM
| Sem_SYNCI_STEP
| Sem_CC
| Sem_CCRES
| Sem_ULR
val fIE = sem-reg-offset (semantic-reg-of Sem_SREG) 0
val fRE = sem-reg-offset (semantic-reg-of Sem_SREG) 25
val fCA = sem-reg-offset (semantic-reg-of Sem_CONFIG1) 2
val fISA = sem-reg-offset (semantic-reg-of Sem_CONFIG3) 14
val fDM = sem-reg-offset (semantic-reg-of Sem_DEBUG) 0
val fIEXI = sem-reg-offset (semantic-reg-of Sem_DEBUG) 1
val fEXL = sem-reg-offset (semantic-reg-of Sem_SREG) 1
val fERL = sem-reg-offset (semantic-reg-of Sem_SREG) 2
val fKSU = sem-reg-offset (semantic-reg-of Sem_SREG) 3
val fBEV = sem-reg-offset (semantic-reg-of Sem_SREG) 22
val fCSS = sem-reg-offset (semantic-reg-of Sem_SRSCTL) 0
val fPSS = sem-reg-offset (semantic-reg-of Sem_SRSCTL) 6
val fESS = sem-reg-offset (semantic-reg-of Sem_SRSCTL) 12
val fHSS = sem-reg-offset (semantic-reg-of Sem_SRSCTL) 26
val sem-reg-offset r o = @{offset=r.offset + o}r
val ip-get = {id=Sem_PC,offset=0,size=32}
val hi-get = {id=Sem_HI,offset=0,size=32}
val lo-get = {id=Sem_LO,offset=0,size=32}
val sreg-get = {id=Sem_SREG,offset=0,size=32}
val semantic-reg-of x =
case x of
Sem_PC : {id=Sem_PC,offset=0,size=32}
| Sem_HI : {id=Sem_HI,offset=0,size=32}
| Sem_LO : {id=Sem_LO,offset=0,size=32}
| Sem_SREG : {id=Sem_SREG,offset=0,size=32}
| Sem_LLBIT : {id=Sem_LLBIT,offset=0,size=1}
| Sem_DEBUG : {id=Sem_DEBUG,offset=0,size=32}
| Sem_CONFIG1 : {id=Sem_CONFIG1,offset=0,size=32}
| Sem_CONFIG3 : {id=Sem_CONFIG3,offset=0,size=32}
| Sem_ISA_MODE : {id=Sem_ISA_MODE,offset=0,size=1}
| Sem_SRSCTL : {id=Sem_SRSCTL,offset=0,size=32}
| Sem_EPC : {id=Sem_EPC,offset=0,size=32}
| Sem_ERROR_EPC : {id=Sem_ERROR_EPC,offset=0,size=32}
| Sem_DEPC : {id=Sem_DEPC,offset=0,size=32}
| Sem_CPUNUM : {id=Sem_CPUNUM,offset=0,size=32}
| Sem_SYNCI_STEP : {id=Sem_SYNCI_STEP,offset=0,size=32}
| Sem_CC : {id=Sem_CC,offset=0,size=32}
| Sem_CCRES : {id=Sem_CCRES,offset=0,size=32}
| Sem_ULR : {id=Sem_ULR,offset=0,size=32}
end
val semantic-gpr-of r =
case r of
......@@ -151,4 +202,9 @@ val semantic-fpr-of f =
| F29 : {id=Sem_F29 ,offset=0,size=32}
| F30 : {id=Sem_F30 ,offset=0,size=32}
| F31 : {id=Sem_F31 ,offset=0,size=32}
| FIR : {id=Sem_FIR ,offset=0,size=32}
| FCCR : {id=Sem_FCCR ,offset=0,size=32}
| FEXR : {id=Sem_FEXR ,offset=0,size=32}
| FENR : {id=Sem_FENR ,offset=0,size=32}
| FCSR : {id=Sem_FCSR ,offset=0,size=32}
end
......@@ -10,6 +10,11 @@ in
traverse f insn
end
val traverse-others f insn =
case insn of
PAUSE: f "PAUSE" (NULLOP)
end
# -> sftl
type uarity =
NULLOP
......@@ -72,7 +77,7 @@ val traverse f insn =
| CLO x: f "CLO" (TERNOP x)
| CLZ x: f "CLZ" (TERNOP x)
| COP2 x: f "COP2" (UNOP_SRC x)
| CTC1 x: f "CTC1" (BINOP x)
| CTC1 x: f "CTC1" (BINOP_SRC x)
| CTC2 x: f "CTC2" (BINOP_SRC x)
| CVT-D-fmt x: f "CVT.D.fmt" (BINOP_FMT x)
| CVT-L-fmt x: f "CVT.L.fmt" (BINOP_FMT x)
......@@ -241,6 +246,7 @@ val traverse f insn =
| WSBH x: f "WSBH" (BINOP x)
| XOR x: f "XOR" (TERNOP x)
| XORI x: f "XORI" (TERNOP x)
| _: traverse-others f insn
end
......
......@@ -33,7 +33,6 @@ type imm =
type lvalue =
GPR of register
| FPR of register
| FPC of register
type rvalue =
LVALUE of lvalue
......@@ -51,10 +50,21 @@ val right lvalue = do
return (LVALUE lvalue)
end
val pause? s = (s.rt == '00000') and (s.rd == '00000') and (s.sa == '00101')
###
# SLL not script handled yet
#val / ['000000 00000 /rt /rd /sa 000000']
# | pause? = nullop PAUSE
# | otherwise = ternop SLL rd (right rt) sa
###
# -> sftl
val decode config = do
update@{rs='',rt='',rd='',fr='',fs='',ft='',fd='',immediate='',offset='',sel='',impl='',code10='',code19='',code20='',stype='',msb='',msbd='',lsb='',sa='',instr_index='',cofun='',cc='',cond='',op='',hint='',fmt=''};
set-endianness BIG_ENDIAN 4;
update@{rs='00000',rt='00000',rd='00000',fr='00000',fs='00000',ft='00000',fd='00000',immediate='0000000000000000',offset16='0000000000000000',offset9='000000000',sel='000',impl='0000000000000000',code10='0000000000',code19='0000000000000000000',code20='00000000000000000000',stype='00000',msb='00000',msbd='00000',lsb='00000',sa='00000',instr_index='00000000000000000000000000',cofun='0000000000000000000000000',cc='000',cond='0000',op='00000',hint='00000',fmt='00000'};
idx-before <- idxget;
insn <- /;
idx-after <- idxget;
......@@ -248,7 +258,7 @@ val / ['010010 1 /cofun'] = unop-src COP2 cofun
### CTC1
### - Move Control Word to Floating Point
val / ['010001 00110 /rt /fs 00000000000'] = binop CTC1 fs/ctrl (right rt)
val / ['010001 00110 /rt /fs 00000000000'] = binop-src CTC1 (right rt) fs/ctrl
### CTC2
### - Move Control Word to Coprocessor 2
......@@ -648,7 +658,7 @@ val / ['010001 10110 /ft /fs /fd 101111'] = ternop PUU-PS fd (right ft) (right f
### RDHWR
### - Read Hardware Register
val / ['011111 00000 /rt /rd 00000 111011'] = binop RDHWR rt (right rd)
val / ['011111 00000 /rt /rd 00000 111011'] = binop RDHWR rt rd/imm
### RDPGPR
### - Read GRP from Previous Shadow Set
......@@ -728,7 +738,9 @@ val / ['011111 /rs /rt /offset9 0 011101'] = ternop-src SHE (right rs) (right rt
### SLL
### - Shift Word Left Logical
val / ['000000 00000 /rt /rd /sa 000000'] = ternop SLL rd (right rt) sa
val / ['000000 00000 /rt /rd /sa 000000']
| pause? = nullop PAUSE
| otherwise = ternop SLL rd (right rt) sa
### SLLV
### - Shift Word Left Logical Variable
......@@ -941,181 +953,181 @@ val / ['001110 /rs /rt /immediate'] = ternop XORI rt (right rs) immediate
val rs = do
rs <- query $rs;
update @{rs=''};
update @{rs='00000'};
return (GPR (gpr-from-bits rs))
end
val rt = do
rt <- query $rt;
update @{rt=''};
update @{rt='00000'};
return (GPR (gpr-from-bits rt))
end
val rd = do
rd <- query $rd;
update @{rd=''};
update @{rd='00000'};
return (GPR (gpr-from-bits rd))
end
val rd/imm = do
rd <- query $rd;
update @{rd=''};
update @{rd='00000'};
return (IMM (IMM5 rd))
end
val rt/imm = do
rt <- query $rt;
update @{rt=''};
update @{rt='00000'};
return (IMM (IMM5 rt))
end
val fr = do
fr <- query $fr;
update @{fr=''};
update @{fr='00000'};
return (FPR (fpr-from-bits fr))
end
val fs = do
fs <- query $fs;
update @{fs=''};
update @{fs='00000'};
return (FPR (fpr-from-bits fs))
end
val ft = do
ft <- query $ft;
update @{ft=''};
update @{ft='00000'};
return (FPR (fpr-from-bits ft))
end
val fd = do
fd <- query $fd;
update @{fd=''};
update @{fd='00000'};
return (FPR (fpr-from-bits fd))
end
val fs/ctrl = do
fs <- query $fs;
update @{fs=''};
return (FPC (fpc-from-bits fs))
update @{fs='00000'};
return (IMM (IMM5 fs))
end
val immediate = do
immediate <- query $immediate;
update @{immediate=''};
update @{immediate='0000000000000000'};
return (IMM (IMM16 immediate))
end
val offset16 = do
offset <- query $offset;
update @{offset=''};
return (IMM (OFFSET16 offset))
offset16 <- query $offset16;
update @{offset16='0000000000000000'};
return (IMM (OFFSET16 offset16))
end
val offset9 = do
offset <- query $offset;
update @{offset=''};
return (IMM (OFFSET9 offset))
offset9 <- query $offset9;
update @{offset9='000000000'};
return (IMM (OFFSET9 offset9))
end
val sel = do
sel <- query $sel;
update @{sel=''};
update @{sel='000'};
return (IMM (SEL sel))
end
val impl = do
impl <- query $impl;
update @{impl=''};
update @{impl='0000000000000000'};
return (IMM (IMPL impl))
end
val code10 = do
code10 <- query $code10;
update @{code10=''};
update @{code10='0000000000'};
return (IMM (CODE10 code10))
end
val code19 = do
code19 <- query $code19;
update @{code19=''};
update @{code19='0000000000000000000'};
return (IMM (CODE19 code19))
end
val code20 = do
code20 <- query $code20;
update @{code20=''};
update @{code20='00000000000000000000'};
return (IMM (CODE20 code20))
end
val stype = do
stype <- query $stype;
update @{stype=''};
update @{stype='00000'};
return (IMM (STYPE stype))
end
val msb = do
msb <- query $msb;
update @{msb=''};
update @{msb='00000'};
return (IMM (POSSIZE msb))
end
val msbd = do
msbd <- query $msbd;
update @{msbd=''};
update @{msbd='00000'};
return (IMM (SIZE msbd))
end
val lsb = do
lsb <- query $lsb;
update @{lsb=''};
update @{lsb='00000'};
return (IMM (POS lsb))
end
val sa = do
sa <- query $sa;
update @{sa=''};
update @{sa='00000'};
return (IMM (IMM5 sa))
end
val instr_index = do
instr_index <- query $instr_index;
update @{instr_index=''};
update @{instr_index='00000000000000000000000000'};
return (IMM (INSTRINDEX instr_index))
end
val cofun = do
cofun <- query $cofun;
update @{cofun=