#include #include #include #include #include "assemble.h" using namespace std; typedef map::value_type pare; // change endian this machine to big void this_to_big(unsigned long long in, char *out, int size){ for(int i = size - 1; i >= 0; i--){ out[i] = in % 0x100; in /= 0x100; } } // insert map from header file void insertmap_from_header(ifstream *ifs, map *map){ string line; bool flag = false; int count = 0; while(true){ getline(*ifs, line); if(!flag){ if(line.find("{") != string::npos) flag = true; continue; } if(line == "};"){ flag = false; break; } if(line.empty()) continue; line.erase(0, 2); map->insert(pare(line.substr(0, line.find_first_of(" ")), count)); count++; } } int main(int argc, char** argv){ if(argc < 2){ cout << "入力ファイルを指定して下さい" << endl; } ifstream ifs(argv[1]); if(ifs.fail()){ cout << "入力ファイルがありません : " << argv[1] << endl; return -1; } string outfile(argv[1]); outfile = outfile.substr(0, outfile.find_last_of(".")) + ".code"; ofstream ofs(outfile.c_str()); map conspool; map instcode; ifstream head("assemble.h"); insertmap_from_header(&head, &conspool); insertmap_from_header(&head, &instcode); /* debug map::iterator it = instcode.begin(); while(it != instcode.end()){ cout << (*it).second << " " << (*it).first << endl; it++; } */ string command; unsigned char code; long long inpl; unsigned long long inpul; double inpd; char buf[8]; int size; char tmp; while(true){ ifs >> command; // cout << command << endl; if(ifs.eof()) break; if(command[0] == '_'){ code = (unsigned char)conspool[command]; if(code != _conspools && code != _locals) ofs.put(code); if(code == _string) ifs >> tmp; // = '#' ifs >> inpul; this_to_big(inpul, buf, 2); ofs.write(buf, 2); switch(code){ case _charseq: for(int i = 0; i < inpul; i++){ ifs >> tmp; ofs << tmp; } break; } }else{ code = (unsigned char)instcode[command]; ofs.put(code); size = 1; switch(code){ case push_lu: size *= 2; case push_iu: size *= 2; case push_cu: size *= 2; case push_bu: ifs >> inpul; this_to_big(inpul, buf, size); ofs.write(buf, size); break; case push_ls: size *= 2; case push_is: size *= 4; ifs >> inpl; this_to_big((unsigned long long)inpl, buf, size); ofs.write(buf, size); break; case push_ds: ifs >> inpd; *((double *)&inpul) = inpd; this_to_big(inpul, buf, 8); ofs.write(buf, 8); break; case push_pt: ifs >> tmp; // = '#' ifs >> inpul; this_to_big(inpul, buf, 2); ofs.write(buf, 2); break; case stor_iu: case stor_is: case stor_lu: case stor_ls: case stor_ds: case stor_pt: case load_iu: case load_is: case load_lu: case load_ls: case load_ds: case load_pt: ifs >> tmp; // = '#' ifs >> inpul; this_to_big(inpul, buf, 2); ofs.write(buf, 2); break; } } } }