#include #include #include #include #include "assemble.h" #include "execute.h" using namespace std; // change endian big to this machine unsigned long long big_to_this(char *in, int size){ unsigned long long out = 0; for(int i = 0; i < size; i++){ out = out * 0x100 + (unsigned char)in[i]; } return out; } int main(int argc, char** argv){ if(argc < 2){ cout << "入力ファイルを指定して下さい" << endl; } ifstream ifs(argv[1]); if(ifs.fail()){ cout << "入力ファイルがありません : " << argv[1] << endl; return -1; } // 入力ファイルを一括読込 unsigned int ifsize; ifs.seekg(0, ios_base::end); ifsize = ifs.tellg(); ifs.seekg(0, ios_base::beg); char inbase[ifsize]; ifs.read(inbase, ifsize); char *incurr = inbase; char buf[8]; unsigned char code; int size; int index; AddrBuff conspool(new void*[1000]); int conspools = (int)big_to_this(incurr, 2); // number of conspool incurr += 2; // 後にアドレスをコピーするための情報の準備 list copylist; void ***copyitem; for(int i = 0; i < conspools; i++){ code = *incurr; incurr++; index = size = big_to_this(incurr, 2); incurr += 2; switch(code){ case _charseq: *conspool.curr = new string(incurr, size); incurr += size; conspool.curr++; break; case _string: copyitem = new void**[2]; copyitem[0] = conspool.curr; copyitem[1] = (void **)conspool.base + index; copylist.push_back(copyitem); conspool.curr++; break; } } // アドレスをコピー list::iterator it = copylist.begin(); while(it != copylist.end()){ *(*it)[0] = *(*it)[1]; it++; } DataBuff stack(new unsigned int[1000]); int locals = (int)big_to_this(incurr, 2); // locals incurr += 2; stack.curr += locals; int iu1, iu2; int is1, is2; long long lu1, lu2; long long ls1, ls2; double ds1, ds2; while(true){ if(incurr >= inbase + ifsize) break; size = 1; code = *incurr; // cout << (int)code << endl; incurr++; switch(code){ case print_iu: stack.curr--; cout << *stack.curr << endl; break; case print_is: stack.curr--; cout << *(int *)stack.curr << endl; break; case print_lu: stack.curr -= 2; cout << *(unsigned long long *)stack.curr << endl; break; case print_ls: stack.curr -= 2; cout << *(long long *)stack.curr << endl; break; case print_ds: stack.curr -= 2; cout << *(double *)stack.curr << endl; break; case print_st: stack.curr--; cout << **(string **)stack.curr << endl; break; case push_lu: case push_ls: case push_ds: size *= 2; case push_iu: case push_is: size *= 2; case push_cu: size *= 2; case push_bu: if(size <= 4){ *stack.curr = (unsigned int)big_to_this(incurr, size); stack.curr++; }else{ *(unsigned long long *)stack.curr = big_to_this(incurr, size); stack.curr += 2; } incurr += size; break; case push_pt: index = (int)big_to_this(incurr, 2); incurr += 2; *(void **)stack.curr = *(void **)(conspool.base + index); stack.curr++; break; case stor_lu: case stor_ls: case stor_ds: size *= 2; case stor_iu: case stor_is: case stor_pt: index = (int)big_to_this(incurr, 2); incurr += 2; stack.curr -= size; memcpy((void *)(stack.base + index), (const void *)stack.curr, size * 4); break; case load_lu: case load_ls: case load_ds: size *= 2; case load_iu: case load_is: case load_pt: index = (int)big_to_this(incurr, 2); incurr += 2; memcpy((void *)stack.curr, (const void*)(stack.base + index), size * 4); stack.curr += size; break; case add_iu: stack.curr--; iu1 = *stack.curr; stack.curr--; iu2 = *stack.curr; *stack.curr = iu1 + iu2; stack.curr++; break; case add_is: stack.curr--; is1 = *(int *)(stack.curr); stack.curr--; is2 = *(int *)(stack.curr); *(int *)stack.curr = is1 + is2; stack.curr++; break; case add_lu: stack.curr -= 2; lu1 = *(unsigned long long *)stack.curr; stack.curr -= 2; lu2 = *(unsigned long long *)stack.curr; *(unsigned long long *)stack.curr = lu1 + lu2; stack.curr += 2; break; case add_ls: stack.curr -= 2; ls1 = *(long long *)stack.curr; stack.curr -= 2; ls2 = *(long long *)stack.curr; *(long long *)stack.curr = ls1 + ls2; stack.curr += 2; break; case add_ds: stack.curr -= 2; ds1 = *(double *)stack.curr; stack.curr -= 2; ds2 = *(double *)stack.curr; *(double *)stack.curr = ds1 + ds2; stack.curr += 2; break; } } }