From 0c3a73a896f45b7f8a1bdaeafc75a9e5012f890f Mon Sep 17 00:00:00 2001 From: felsabbagh3 Date: Sat, 9 Feb 2019 20:17:17 -0500 Subject: [PATCH] lots of errors --- src/Makefile | 2 +- src/enc.cpp | 471 +++++++------- src/harptool.cpp | 432 ++++++------- src/include/enc.h | 11 + src/include/instruction.h | 75 ++- src/include/obj.h | 326 +++++----- src/instruction.cpp | 666 +++++++++---------- src/obj.cpp | 1284 ++++++++++++++++++------------------- 8 files changed, 1676 insertions(+), 1591 deletions(-) diff --git a/src/Makefile b/src/Makefile index 882f2cd7..fd9e94f2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ ################################################################################ # HARPtools by Chad D. Kersey, Summer 2011 # ################################################################################ -CXXFLAGS ?= -fPIC -O3 # -g -DUSE_DEBUG=3 -DPRINT_ACTIVE_THREADS +CXXFLAGS ?= -std=c++11 -fPIC -O3 # -g -DUSE_DEBUG=3 -DPRINT_ACTIVE_THREADS LDLIBS ?= -pthread PREFIX ?= /usr/local diff --git a/src/enc.cpp b/src/enc.cpp index ec2eb760..6a623022 100644 --- a/src/enc.cpp +++ b/src/enc.cpp @@ -28,75 +28,75 @@ static void decodeError(string msg) { } void Encoder::encodeChunk(DataChunk &dest, const TextChunk &src) { - typedef vector::const_iterator vec_it; - const vector &s(src.instructions); - vector &d(dest.contents); + // typedef vector::const_iterator vec_it; + // const vector &s(src.instructions); + // vector &d(dest.contents); - /* Keep encoding the instructions. */ - Size n = 0; + // /* Keep encoding the instructions. */ + // Size n = 0; - /* For each instruction. */ - for (vec_it i = s.begin(); i != s.end(); i++) { - Ref *ref; + // /* For each instruction. */ + // for (vec_it i = s.begin(); i != s.end(); i++) { + // Ref *ref; - /* Perform the encoding. */ - n += encode(ref, d, n, **i); + // /* Perform the encoding. */ + // n += encode(ref, d, n, **i); - /* Add reference if necessary. */ - if (ref != NULL) { - ref->ibase = n; - dest.refs.push_back(ref); - } - } + // /* Add reference if necessary. */ + // if (ref != NULL) { + // ref->ibase = n; + // dest.refs.push_back(ref); + // } + // } - dest.alignment = src.alignment; - dest.flags = src.flags; - dest.address = src.address; - dest.bound = src.bound; - if (src.isGlobal()) dest.setGlobal(); + // dest.alignment = src.alignment; + // dest.flags = src.flags; + // dest.address = src.address; + // dest.bound = src.bound; + // if (src.isGlobal()) dest.setGlobal(); - d.resize(n); - dest.size = n; + // d.resize(n); + // dest.size = n; } void Decoder::decodeChunk(TextChunk &dest, const DataChunk &src) { - typedef vector::const_iterator vec_it; - const vector &v(src.contents); - Size n = 0; +// typedef vector::const_iterator vec_it; +// const vector &v(src.contents); +// Size n = 0; - setRefs(src.refs); +// setRefs(src.refs); - while (n < src.contents.size()) { - Instruction *inst = decode(v, n); - if (inst->hasRefLiteral()) { - dest.refs.push_back(inst->getRefLiteral()); - } +// while (n < src.contents.size()) { +// Instruction *inst = decode(v, n); +// if (inst->hasRefLiteral()) { +// dest.refs.push_back(inst->getRefLiteral()); +// } - dest.instructions.push_back(inst); - } +// dest.instructions.push_back(inst); +// } - dest.alignment = src.alignment; - dest.flags = src.flags; - dest.address = src.address; - dest.bound = src.bound; - if (src.isGlobal()) dest.setGlobal(); +// dest.alignment = src.alignment; +// dest.flags = src.flags; +// dest.address = src.address; +// dest.bound = src.bound; +// if (src.isGlobal()) dest.setGlobal(); - clearRefs(); -} +// clearRefs(); +// } -void Decoder::setRefs(const std::vector &refVec) { - haveRefs = true; +// void Decoder::setRefs(const std::vector &refVec) { +// haveRefs = true; - typedef std::vector::const_iterator vec_ci; +// typedef std::vector::const_iterator vec_ci; - for (vec_ci i = refVec.begin(); i != refVec.end(); i++) { - OffsetRef *oref = dynamic_cast(*i); - if (oref) { - refMap[oref->getOffset()] = *i; - } else { - decodeError("Unknown Ref type in Decoder::setRefs"); - } - } +// for (vec_ci i = refVec.begin(); i != refVec.end(); i++) { +// OffsetRef *oref = dynamic_cast(*i); +// if (oref) { +// refMap[oref->getOffset()] = *i; +// } else { +// decodeError("Unknown Ref type in Decoder::setRefs"); +// } +// } } Instruction *ByteDecoder::decode(const vector &v, Size &n) { @@ -242,27 +242,34 @@ static unsigned ceilLog2(RegNum x) { return z; } -static Word mask(Size bits) { - return (1ull< r) r = p; - oMask = mask(o); rMask = mask(r); pMask = mask(p); - i1Mask = mask(i1); i2Mask = mask(i2); i3Mask = mask(i3); + + inst_s = arch.getWordSize() * 8; + opcode_s = 7; + reg_s = 5; + func3_s = 3; + + shift_opcode = 0; + shift_rd = opcode_s; + shift_func3 = opcode_s + reg_s; + shift_rs1 = opcode_s + reg_s + func3_s; + shift_rs2 = opcode_s + reg_s + func3_s + reg_s; + shift_func7 = opcode_s + reg_s + func3_s + reg_s + reg_s; + shift_j_u_immed = opcode_s + reg_s; + shift_s_b_immed = opcode_s + reg_s + func3_s + reg_s + reg_s; + shift_i_immed = opcode_s + reg_s + func3_s + reg_s; + + reg_mask = 0x1f; + func3_mask = 0x7; + func7_mask = 0x7f; + opcode_mask = 0x7f; + i_immed_mask = 0xfff; + s_immed_mask = 0xfff; + b_immed_mask = 0x1fff; + u_immed_mask = 0xfffff; + j_immed_mask = 0xfffff; + } Word signExt(Word w, Size bit, Word mask) { @@ -278,69 +285,83 @@ Instruction *WordDecoder::decode(const std::vector &v, Size &idx) { bool predicated = false; if (predicated) { inst.setPred((code>>(n-p-1))&pMask); } - Instruction::Opcode op = (Instruction::Opcode)((code>>i1)&oMask); + Instruction::Opcode op = (Instruction::Opcode)((code>>shift_opcode)&opcode_mask); inst.setOpcode(op); bool usedImm(false); - switch(Instruction::instTable[op].argClass) { - case Instruction::AC_NONE: + + switch(Instruction::instTable[op].iType) + { + case Instruction::InstType::N_TYPE: break; - case Instruction::AC_1IMM: - inst.setSrcImm(signExt(code&i1Mask, i1, i1Mask)); + case Instruction::InstType::R_TYPE: + inst.setDestReg((code>>shift_rd) & reg_mask); + inst.setSrcReg((code>>shift_rs1) & reg_mask); + inst.setSrcReg((code>>shift_rs2) & reg_mask); + inst.setFunc3 ((code>>shift_func3) & func3_mask); + inst.setFunc7 ((code>>shift_func7) & func7_mask); + break; + case Instruction::InstType::I_TYPE + inst.setDestReg((code>>shift_rd) & reg_mask); + inst.setSrcReg((code>>shift_rs1) & reg_mask); + inst.setFunc3 ((code>>shift_func3) & func3_mask); + inst.setSrcImm(signExt(code>>shift_i_immed, 12, i_immed_mask)); usedImm = true; break; - case Instruction::AC_2IMM: - inst.setDestReg((code>>i2)&rMask); - inst.setSrcImm(signExt(code&i2Mask, i2, i2Mask)); + case Instruction::InstType::S_TYPE: + + inst.setSrcReg((code>>shift_rs1) & reg_mask); + inst.setSrcReg((code>>shift_rs2) & reg_mask); + inst.setFunc3 ((code>>shift_func3) & func3_mask); + + word dest_bits = (code>>shift_rd) & reg_mask; + Word imm_bits = (code>>shift_s_b_immed & func7_mask); + Word imeed = (imm_bits << reg_s) | dest_bits; + inst.setSrcImm(signExt(imeed, 12, s_immed_mask)); usedImm = true; break; - case Instruction::AC_3IMM: - inst.setDestReg((code>>i2)&rMask); - inst.setSrcReg((code>>i3)&rMask); - inst.setSrcImm(signExt(code&i3Mask, i3, i3Mask)); + case Instruction::InstType::B_TYPE: + + inst.setSrcReg((code>>shift_rs1) & reg_mask); + inst.setSrcReg((code>>shift_rs2) & reg_mask); + inst.setFunc3 ((code>>shift_func3) & func3_mask); + + word dest_bits = (code>>shift_rd) & reg_mask; + Word imm_bits = (code>>shift_s_b_immed & func7_mask); + + Word bit_11 = dest_bits & 0x1; + Word bit_4_1 = dest_bits >> 1; + Word bit_10_5 = imm_bits & 0x3f; + Word bit_12 = imm_bits >> 6; + + Word imeed = 0 | (bits_4_1 << 1) | (bit_10_5 << 5) | (bit_11 << 11) | (bit_12 << 12); + + inst.setSrcImm(signExt(imeed, 13, b_immed_mask)); usedImm = true; break; - case Instruction::AC_3IMMSRC: - inst.setSrcReg((code>>i2)&rMask); - inst.setSrcReg((code>>i3)&rMask); - inst.setSrcImm(signExt(code&i3Mask, i3, i3Mask)); + case Instruction::InstType::U_TYPE: + inst.setDestReg((code>>shift_rd) & reg_mask); + inst.setSrcImm(signExt(code>>shift_j_u_immed, 20, u_immed_mask)); usedImm = true; break; - case Instruction::AC_1REG: - inst.setSrcReg((code>>i2)&rMask); + case Instruction::InstType::J_TYPE: + inst.setDestReg((code>>shift_rd) & reg_mask); + + // [20 | 10:1 | 11 | 19:12] + + Word unordered = code>>shift_j_u_immed; + + Word bits_19_12 = unordered & 0xff; + Word bit_11 = (unordered>>8) & 0x1; + Word bits_10_1 = (unordered >> 9) & 0x3ff; + Word bit_20 = (unordered>>19) & 0x1; + + Word imeed = 0 | (bits_10_1 << 1) | (bit_11 << 11) | (bits_19_12 << 12) | (bit20 << 20); + + inst.setSrcImm(signExt(imeed, 20, j_immed_mask)); + usedImm = true; break; - case Instruction::AC_2REG: - inst.setDestReg((code>>i2)&rMask); - inst.setSrcReg((code>>i3)&rMask); - break; - case Instruction::AC_3REG: - inst.setDestReg((code>>i2)&rMask); - inst.setSrcReg((code>>i3)&rMask); - inst.setSrcReg((code>>(i3-r))&rMask); - break; - case Instruction::AC_3REGSRC: - inst.setSrcReg((code>>i2)&rMask); - inst.setSrcReg((code>>i3)&rMask); - inst.setSrcReg((code>>(i3-r))&rMask); - break; - case Instruction::AC_PREG_REG: - inst.setDestPReg((code>>i2)&pMask); - inst.setSrcReg((code>>i3)&rMask); - break; - case Instruction::AC_2PREG: - inst.setDestPReg((code>>i2)&pMask); - inst.setSrcPReg((code>>i3)&pMask); - break; - case Instruction::AC_3PREG: - inst.setDestPReg((code>>i2)&pMask); - inst.setSrcPReg((code>>i3)&pMask); - inst.setSrcPReg((code>>(i3-r))&pMask); - break; - case Instruction::AC_2REGSRC: - inst.setSrcReg((code>>i2)&rMask); - inst.setSrcReg((code>>i3)&rMask); - break; - defualt: + defualt: cout << "Unrecognized argument class in word decoder.\n"; exit(1); } @@ -359,120 +380,120 @@ Instruction *WordDecoder::decode(const std::vector &v, Size &idx) { return &inst; } -WordEncoder::WordEncoder(const ArchDef &arch) { - getSizes(arch, n, o, r, p, i1, i2, i3); - if (p > r) r = p; - oMask = mask(o); rMask = mask(r); pMask = mask(p); - i1Mask = mask(i1); i2Mask = mask(i2); i3Mask = mask(i3); -} +// WordEncoder::WordEncoder(const ArchDef &arch) { +// getSizes(arch, n, o, r, p, i1, i2, i3); +// if (p > r) r = p; +// oMask = mask(o); rMask = mask(r); pMask = mask(p); +// i1Mask = mask(i1); i2Mask = mask(i2); i3Mask = mask(i3); +// } -Size WordEncoder::encode(Ref *&ref, std::vector &v, - Size idx, Instruction &i) -{ - Word code = 0; - Size bitsWritten = 0; +// Size WordEncoder::encode(Ref *&ref, std::vector &v, +// Size idx, Instruction &i) +// { +// Word code = 0; +// Size bitsWritten = 0; - /* Predicate/predicated bit */ - if (i.hasPred()) { - code = 1 << p; - code |= (i.getPred()&pMask); - if (i.getPred() > pMask) { - cout << "Predicate in " << i << " does not fit in encoding.\n"; - exit(1); - } - } - bitsWritten += (1 + p); +// /* Predicate/predicated bit */ +// if (i.hasPred()) { +// code = 1 << p; +// code |= (i.getPred()&pMask); +// if (i.getPred() > pMask) { +// cout << "Predicate in " << i << " does not fit in encoding.\n"; +// exit(1); +// } +// } +// bitsWritten += (1 + p); - /* Opcode */ - code <<= o; - code |= (i.getOpcode()&oMask); - if (i.getOpcode() > oMask) { - cout << "Opcode in " << i << " does not fit in encoding.\n"; - exit(1); - } - bitsWritten += o; +// /* Opcode */ +// code <<= o; +// code |= (i.getOpcode()&oMask); +// if (i.getOpcode() > oMask) { +// cout << "Opcode in " << i << " does not fit in encoding.\n"; +// exit(1); +// } +// bitsWritten += o; - if (i.hasRDest()) { - code <<= r; - code |= i.getRDest(); - bitsWritten += r; - if (i.getRDest() > rMask) { - cout << "Destination register in " << i << " does not fit in encoding.\n"; - exit(1); - } - } +// if (i.hasRDest()) { +// code <<= r; +// code |= i.getRDest(); +// bitsWritten += r; +// if (i.getRDest() > rMask) { +// cout << "Destination register in " << i << " does not fit in encoding.\n"; +// exit(1); +// } +// } - if (i.hasPDest()) { - code <<= r; - code |= i.getPDest(); - bitsWritten += r; - if (i.getPDest() > rMask) { - cout << "Destination predicate in " < rMask) { +// cout << "Destination predicate in " < rMask) { - cout << "Source register " << j << " in " << i - << " does not fit in encoding.\n"; - exit(1); - } - } +// for (Size j = 0; j < i.getNRSrc(); j++) { +// code <<= r; +// code |= i.getRSrc(j); +// bitsWritten += r; +// if (i.getRSrc(j) > rMask) { +// cout << "Source register " << j << " in " << i +// << " does not fit in encoding.\n"; +// exit(1); +// } +// } - for (Size j = 0; j < i.getNPSrc(); j++) { - code <<= r; - code |= i.getPSrc(j); - bitsWritten += r; - if (i.getPSrc(j) > rMask) { - cout << "Source predicate " << j << " in " << i - << " does not fit in encoding.\n"; - exit(1); - } - } +// for (Size j = 0; j < i.getNPSrc(); j++) { +// code <<= r; +// code |= i.getPSrc(j); +// bitsWritten += r; +// if (i.getPSrc(j) > rMask) { +// cout << "Source predicate " << j << " in " << i +// << " does not fit in encoding.\n"; +// exit(1); +// } +// } - if (i.hasRefLiteral()) { - Ref *r = i.getRefLiteral(); - ref = new OffsetRef(r->name, v, idx, n - bitsWritten, n, i.hasRelImm()); - } else { - ref = NULL; - } +// if (i.hasRefLiteral()) { +// Ref *r = i.getRefLiteral(); +// ref = new OffsetRef(r->name, v, idx, n - bitsWritten, n, i.hasRelImm()); +// } else { +// ref = NULL; +// } - if (i.hasImm()) { - if (bitsWritten == n - i1) { - code <<= i1; - code |= (i.getImm()&i1Mask); - bitsWritten += i1; - Word_s ws(i.getImm()); - if ((ws >> i1) != 0 && (ws >> i1) != -1) goto tooBigImm; - } else if (bitsWritten == n - i2) { - code <<= i2; - code |= (i.getImm()&i2Mask); - bitsWritten += i2; - Word_s ws(i.getImm()); - if ((ws >> i2) != 0 && (ws >> i2) != -1) goto tooBigImm; - } else if (bitsWritten == n - i3) { - code <<= i3; - code |= (i.getImm()&i3Mask); - bitsWritten += i3; - Word_s ws(i.getImm()); - if ((ws >> i3) != 0 && (ws >> i3) != -1) goto tooBigImm; - } else { - cout << "WordEncoder::encode() could not encode: " << i << '\n'; - exit(1); - } - } +// if (i.hasImm()) { +// if (bitsWritten == n - i1) { +// code <<= i1; +// code |= (i.getImm()&i1Mask); +// bitsWritten += i1; +// Word_s ws(i.getImm()); +// if ((ws >> i1) != 0 && (ws >> i1) != -1) goto tooBigImm; +// } else if (bitsWritten == n - i2) { +// code <<= i2; +// code |= (i.getImm()&i2Mask); +// bitsWritten += i2; +// Word_s ws(i.getImm()); +// if ((ws >> i2) != 0 && (ws >> i2) != -1) goto tooBigImm; +// } else if (bitsWritten == n - i3) { +// code <<= i3; +// code |= (i.getImm()&i3Mask); +// bitsWritten += i3; +// Word_s ws(i.getImm()); +// if ((ws >> i3) != 0 && (ws >> i3) != -1) goto tooBigImm; +// } else { +// cout << "WordEncoder::encode() could not encode: " << i << '\n'; +// exit(1); +// } +// } - if (bitsWritten < n) code <<= (n - bitsWritten); +// if (bitsWritten < n) code <<= (n - bitsWritten); - writeWord(v, idx, n/8, code); +// writeWord(v, idx, n/8, code); - return n/8; +// return n/8; -tooBigImm: - cout << "Immediate in " << i << " too large to encode.\n"; - exit(1); -} +// tooBigImm: +// cout << "Immediate in " << i << " too large to encode.\n"; +// exit(1); +// } diff --git a/src/harptool.cpp b/src/harptool.cpp index 1e2175ef..4eaa3f34 100644 --- a/src/harptool.cpp +++ b/src/harptool.cpp @@ -50,158 +50,158 @@ HarpToolMode findMode(int argc, char** argv) { } int asm_main(int argc, char **argv) { - string archString("8w32/32/8/8"), outFileName("a.out.HOF"), - inFileName(argv[argc-1]); - bool showHelp; + // string archString("8w32/32/8/8"), outFileName("a.out.HOF"), + // inFileName(argv[argc-1]); + // bool showHelp; - /* Get command line arguments. */ - CommandLineArgFlag fh("-h", "--help", "", showHelp); - CommandLineArgSetterfo("-o", "--output", "", outFileName); - CommandLineArgSetterfa("-a", "--arch", "", archString); + // /* Get command line arguments. */ + // CommandLineArgFlag fh("-h", "--help", "", showHelp); + // CommandLineArgSetterfo("-o", "--output", "", outFileName); + // CommandLineArgSetterfa("-a", "--arch", "", archString); - CommandLineArg::readArgs(argc-1, argv); + // CommandLineArg::readArgs(argc-1, argv); - if (showHelp || argc == 0) { - cout << Help::asmHelp; - exit(0); - } + // if (showHelp || argc == 0) { + // cout << Help::asmHelp; + // exit(0); + // } - ArchDef arch(archString); + // ArchDef arch(archString); - D(0, "Created ArchDef for " << string(arch)); + // D(0, "Created ArchDef for " << string(arch)); - /* Create an appropriate encoder. */ - Encoder *enc; - switch (arch.getEncChar()) { - case 'b': enc = new ByteEncoder(arch); break; - case 'w': enc = new WordEncoder(arch); break; - defaulet: - cout << "Unknown encoding type, \"" << arch.getEncChar() << "\"\n"; - exit(1); - } + // /* Create an appropriate encoder. */ + // Encoder *enc; + // switch (arch.getEncChar()) { + // case 'b': enc = new ByteEncoder(arch); break; + // case 'w': enc = new WordEncoder(arch); break; + // defaulet: + // cout << "Unknown encoding type, \"" << arch.getEncChar() << "\"\n"; + // exit(1); + // } - /* Open files. */ - if (outFileName == "") { - cout << "HARP Assembler: No output filename given.\n"; - exit(1); - } + // /* Open files. */ + // if (outFileName == "") { + // cout << "HARP Assembler: No output filename given.\n"; + // exit(1); + // } - ifstream asmFile(inFileName.c_str()); - ofstream outFile(outFileName.c_str()); + // ifstream asmFile(inFileName.c_str()); + // ofstream outFile(outFileName.c_str()); - if (!asmFile) { - cout << "Could not open \"" << inFileName << "\" for reading.\n"; - exit(1); - } + // if (!asmFile) { + // cout << "Could not open \"" << inFileName << "\" for reading.\n"; + // exit(1); + // } - if (!outFile) { - cout << "Could not open \"" << outFileName << "\" for writing.\n"; - exit(1); - } + // if (!outFile) { + // cout << "Could not open \"" << outFileName << "\" for writing.\n"; + // exit(1); + // } - /* Read an Obj from the assembly file. */ - D(0, "Passing AsmReader ArchDef: " << string(arch)); - AsmReader ar(arch); - Obj *o = ar.read(asmFile); + // /* Read an Obj from the assembly file. */ + // D(0, "Passing AsmReader ArchDef: " << string(arch)); + // AsmReader ar(arch); + // Obj *o = ar.read(asmFile); - /* Encode the text chunks read from the assembly file. */ - for (Size j = 0; j < o->chunks.size(); j++) { - Chunk *&c = o->chunks[j]; - TextChunk *tc; - DataChunk *dc; - if ((tc = dynamic_cast(c)) != NULL) { - /* Encode it. */ - dc = new DataChunk(tc->name); - enc->encodeChunk(*dc, *tc); + // /* Encode the text chunks read from the assembly file. */ + // for (Size j = 0; j < o->chunks.size(); j++) { + // Chunk *&c = o->chunks[j]; + // TextChunk *tc; + // DataChunk *dc; + // if ((tc = dynamic_cast(c)) != NULL) { + // /* Encode it. */ + // dc = new DataChunk(tc->name); + // enc->encodeChunk(*dc, *tc); - /* Delete the text chunk. */ - delete tc; + // /* Delete the text chunk. */ + // delete tc; - /* Do the switch. */ - c = dc; - } - } - asmFile.close(); - delete enc; + // /* Do the switch. */ + // c = dc; + // } + // } + // asmFile.close(); + // delete enc; - /* Write a HOF binary. */ - D(0, "Creating a HOFWriter, passing it ArchDef: " << string(arch)); - HOFWriter hw(arch); - hw.write(outFile, *o); - outFile.close(); + // /* Write a HOF binary. */ + // D(0, "Creating a HOFWriter, passing it ArchDef: " << string(arch)); + // HOFWriter hw(arch); + // hw.write(outFile, *o); + // outFile.close(); - delete o; + // delete o; return 0; } int disasm_main(int argc, char **argv) { - bool showHelp; - string outFileName("a.out.s"), archString("8w32/32/8/8"); + // bool showHelp; + // string outFileName("a.out.s"), archString("8w32/32/8/8"); - /* Get command line arguments. */ - CommandLineArgFlag fh("-h", "--help", "", showHelp); - CommandLineArgSetterfa("-a", "--arch", "", archString); - CommandLineArgSetterfo("-o", "--output", "", outFileName); + // /* Get command line arguments. */ + // CommandLineArgFlag fh("-h", "--help", "", showHelp); + // CommandLineArgSetterfa("-a", "--arch", "", archString); + // CommandLineArgSetterfo("-o", "--output", "", outFileName); - if (argc != 0) CommandLineArg::readArgs(argc-1, argv); + // if (argc != 0) CommandLineArg::readArgs(argc-1, argv); - if (argc == 0 || showHelp) { - cout << Help::disasmHelp; - exit(0); - } + // if (argc == 0 || showHelp) { + // cout << Help::disasmHelp; + // exit(0); + // } - ifstream objFile(argv[argc-1]); - ofstream outFile(outFileName.c_str()); - ArchDef arch(archString); + // ifstream objFile(argv[argc-1]); + // ofstream outFile(outFileName.c_str()); + // ArchDef arch(archString); - if (!objFile) { - cout << "Disassembler could not open \"" << argv[argc-1] - << "\" for reading.\n"; - exit(1); - } + // if (!objFile) { + // cout << "Disassembler could not open \"" << argv[argc-1] + // << "\" for reading.\n"; + // exit(1); + // } - if (!outFile) { - cout << "Disassembler could not open \"" << outFileName - << "\" for output.\n"; - exit(1); - } + // if (!outFile) { + // cout << "Disassembler could not open \"" << outFileName + // << "\" for output.\n"; + // exit(1); + // } - HOFReader hr(arch); - Obj *o = hr.read(objFile); - objFile.close(); - Decoder *dec; + // HOFReader hr(arch); + // Obj *o = hr.read(objFile); + // objFile.close(); + // Decoder *dec; - switch (arch.getEncChar()) { - case 'b': dec = new ByteDecoder(arch); break; - case 'w': dec = new WordDecoder(arch); break; - default: - cout << "Unrecognized encoding character for disassembler.\n"; - exit(1); - } + // switch (arch.getEncChar()) { + // case 'b': dec = new ByteDecoder(arch); break; + // case 'w': dec = new WordDecoder(arch); break; + // default: + // cout << "Unrecognized encoding character for disassembler.\n"; + // exit(1); + // } - /* Decode the chunks read from the object. */ - for (Size j = 0; j < o->chunks.size(); j++) { - Chunk *&c = o->chunks[j]; - if (c->flags & EX_USR) { - TextChunk *tc; - DataChunk *dc; - if ((dc = dynamic_cast(c)) != NULL) { - TextChunk *tc = new TextChunk(dc->name); - dec->decodeChunk(*tc, *dc); - delete dc; - c = tc; - } - } - } - delete dec; + // /* Decode the chunks read from the object. */ + // for (Size j = 0; j < o->chunks.size(); j++) { + // Chunk *&c = o->chunks[j]; + // if (c->flags & EX_USR) { + // TextChunk *tc; + // DataChunk *dc; + // if ((dc = dynamic_cast(c)) != NULL) { + // TextChunk *tc = new TextChunk(dc->name); + // dec->decodeChunk(*tc, *dc); + // delete dc; + // c = tc; + // } + // } + // } + // delete dec; - AsmWriter aw(arch); - aw.write(outFile, *o); - outFile.close(); + // AsmWriter aw(arch); + // aw.write(outFile, *o); + // outFile.close(); - delete o; + // delete o; return 0; } @@ -254,117 +254,117 @@ int emu_main(int argc, char **argv) { } int ld_main(int argc, char **argv) { - bool showHelp, mustResolveRefs(true); - string outFileName("a.out.bin"), archString("8w32/32/8/8"), - formatString("bin"); - Size nObjects; - Addr binOffset(0); + // bool showHelp, mustResolveRefs(true); + // string outFileName("a.out.bin"), archString("8w32/32/8/8"), + // formatString("bin"); + // Size nObjects; + // Addr binOffset(0); - /* Get command line arguments. */ - CommandLineArgFlag fh("-h", "--help", "", showHelp); - CommandLineArgSetterfa("-a", "--arch", "", archString); - CommandLineArgSetterff("-f", "--format", "", formatString); - CommandLineArgSetter foffset("--offset", "", binOffset); - CommandLineArgSetterfo("-o", "--output", "", outFileName); + // /* Get command line arguments. */ + // CommandLineArgFlag fh("-h", "--help", "", showHelp); + // CommandLineArgSetterfa("-a", "--arch", "", archString); + // CommandLineArgSetterff("-f", "--format", "", formatString); + // CommandLineArgSetter foffset("--offset", "", binOffset); + // CommandLineArgSetterfo("-o", "--output", "", outFileName); - int firstInput(0), newArgc; - for (size_t i = 0; i < argc; i++) { - if (*(argv[i]) != '-') { firstInput = i; newArgc = i; break; } - else if (string(argv[i]) == "--") { firstInput = i+1; newArgc = i; break; } - else i++; /* Skip both the switch and its argument. */ - } - nObjects = argc - firstInput; + // int firstInput(0), newArgc; + // for (size_t i = 0; i < argc; i++) { + // if (*(argv[i]) != '-') { firstInput = i; newArgc = i; break; } + // else if (string(argv[i]) == "--") { firstInput = i+1; newArgc = i; break; } + // else i++; /* Skip both the switch and its argument. */ + // } + // nObjects = argc - firstInput; - if (argc != 0) CommandLineArg::readArgs(newArgc, argv); + // if (argc != 0) CommandLineArg::readArgs(newArgc, argv); - if (argc == 0 || showHelp) { - cout << Help::ldHelp; - exit(0); - } + // if (argc == 0 || showHelp) { + // cout << Help::ldHelp; + // exit(0); + // } - if (firstInput == argc) { - cout << "Linker: no input files given.\n"; - exit(1); - } + // if (firstInput == argc) { + // cout << "Linker: no input files given.\n"; + // exit(1); + // } - ArchDef arch(archString); + // ArchDef arch(archString); - /* Read all of the objects, assign addresses to their chunks, and place them - in an address map.*/ - vector objects(nObjects); - vector chunks; - map gChunkMap; - Addr nextOffset(binOffset); + // /* Read all of the objects, assign addresses to their chunks, and place them + // in an address map.*/ + // vector objects(nObjects); + // vector chunks; + // map gChunkMap; + // Addr nextOffset(binOffset); - for (Size i = 0; i < nObjects; i++) { - map lChunkMap; + // for (Size i = 0; i < nObjects; i++) { + // map lChunkMap; - /* Read the object. */ - HOFReader hr(arch); - ifstream objFile(argv[firstInput + i]); - if (!objFile) { - cout << "Could not open \"" << argv[firstInput + i] - << "\" for reading.\n"; - exit(1); - } - objects[i] = hr.read(objFile); + // /* Read the object. */ + // HOFReader hr(arch); + // ifstream objFile(argv[firstInput + i]); + // if (!objFile) { + // cout << "Could not open \"" << argv[firstInput + i] + // << "\" for reading.\n"; + // exit(1); + // } + // objects[i] = hr.read(objFile); - /* Assign addresses to chunks. */ - Obj &obj = *objects[i]; - for (Size j = 0; j < obj.chunks.size(); j++) { - DataChunk *c = dynamic_cast(obj.chunks[j]); - if (c->alignment != 0 && nextOffset % c->alignment) - nextOffset += c->alignment - (nextOffset % c->alignment); - c->bind(nextOffset); - chunks.push_back(c); - if (obj.chunks[j]->name != "") { - if (c->isGlobal()) gChunkMap[c->name] = nextOffset; - else lChunkMap[c->name] = nextOffset; - } - nextOffset += (c->size); - } + // /* Assign addresses to chunks. */ + // Obj &obj = *objects[i]; + // for (Size j = 0; j < obj.chunks.size(); j++) { + // DataChunk *c = dynamic_cast(obj.chunks[j]); + // if (c->alignment != 0 && nextOffset % c->alignment) + // nextOffset += c->alignment - (nextOffset % c->alignment); + // c->bind(nextOffset); + // chunks.push_back(c); + // if (obj.chunks[j]->name != "") { + // if (c->isGlobal()) gChunkMap[c->name] = nextOffset; + // else lChunkMap[c->name] = nextOffset; + // } + // nextOffset += (c->size); + // } - /* Resolve local references. */ - for (Size i = 0; i < obj.chunks.size(); i++) { - DataChunk *dc = dynamic_cast(obj.chunks[i]); - for (Size j = 0; j < dc->refs.size(); j++) { - Ref &ref = *(dc->refs[j]); - if (lChunkMap.find(dc->refs[j]->name) != lChunkMap.end()) { - dc->refs[j]->bind(lChunkMap[dc->refs[j]->name], - dc->address + dc->refs[j]->ibase); - } - } - } - } + // /* Resolve local references. */ + // for (Size i = 0; i < obj.chunks.size(); i++) { + // DataChunk *dc = dynamic_cast(obj.chunks[i]); + // for (Size j = 0; j < dc->refs.size(); j++) { + // Ref &ref = *(dc->refs[j]); + // if (lChunkMap.find(dc->refs[j]->name) != lChunkMap.end()) { + // dc->refs[j]->bind(lChunkMap[dc->refs[j]->name], + // dc->address + dc->refs[j]->ibase); + // } + // } + // } + // } - /* Resolve references. */ - for (Size i = 0; i < chunks.size(); i++) { - DataChunk *dc = chunks[i]; - for (Size j = 0; j < dc->refs.size(); j++) { - Ref &ref = *(dc->refs[j]); - if (!ref.bound && (gChunkMap.find(ref.name) != gChunkMap.end())) { - ref.bind(gChunkMap[ref.name], dc->address + ref.ibase); - } else if (!ref.bound && mustResolveRefs) { - cout << "Undefined symbol: \"" << ref.name << "\"\n"; - exit(1); - } - } - } + // /* Resolve references. */ + // for (Size i = 0; i < chunks.size(); i++) { + // DataChunk *dc = chunks[i]; + // for (Size j = 0; j < dc->refs.size(); j++) { + // Ref &ref = *(dc->refs[j]); + // if (!ref.bound && (gChunkMap.find(ref.name) != gChunkMap.end())) { + // ref.bind(gChunkMap[ref.name], dc->address + ref.ibase); + // } else if (!ref.bound && mustResolveRefs) { + // cout << "Undefined symbol: \"" << ref.name << "\"\n"; + // exit(1); + // } + // } + // } - /* Write out the chunks. */ - ofstream outFile(outFileName.c_str()); - for (Size i = 0; i < chunks.size(); i++) { - if (outFile.tellp() > chunks[i]->address - binOffset) { - cout << "Linker internal error. Wrote past next chunk address.\n"; - exit(1); - } - while (outFile.tellp() < chunks[i]->address - binOffset) outFile.put('\0'); - outFile.seekp(chunks[i]->address - binOffset); - outFile.write((char*)&chunks[i]->contents[0], chunks[i]->contents.size()); - } + // /* Write out the chunks. */ + // ofstream outFile(outFileName.c_str()); + // for (Size i = 0; i < chunks.size(); i++) { + // if (outFile.tellp() > chunks[i]->address - binOffset) { + // cout << "Linker internal error. Wrote past next chunk address.\n"; + // exit(1); + // } + // while (outFile.tellp() < chunks[i]->address - binOffset) outFile.put('\0'); + // outFile.seekp(chunks[i]->address - binOffset); + // outFile.write((char*)&chunks[i]->contents[0], chunks[i]->contents.size()); + // } - /* Clean up. */ - for (Size i = 0; i < nObjects; i++) delete objects[i]; + // /* Clean up. */ + // for (Size i = 0; i < nObjects; i++) delete objects[i]; return 0; } diff --git a/src/include/enc.h b/src/include/enc.h index ea74f618..e90aa5d9 100644 --- a/src/include/enc.h +++ b/src/include/enc.h @@ -51,6 +51,17 @@ namespace Harp { private: Size n, o, r, p, i1, i2, i3; Word oMask, rMask, pMask, i1Mask, i2Mask, i3Mask; + + // FARES + Size inst_s, opcode_s, reg_s, func3_s; + Size shift_opcode, shift_rd, shift_rs1, shift_rs2, shift_func3, shift_func7; + Size shift_j_u_immed, shift_s_b_immed, shift_i_immed; + + + + Word reg_mask, func3_mask, func7_mask, opcode_mask, i_immed_mask, + s_immed_mask, b_immed_mask, u_immed_mask, j_immed_mask; + }; class ByteDecoder : public Decoder { diff --git a/src/include/instruction.h b/src/include/instruction.h index 4cba0f51..b85256de 100644 --- a/src/include/instruction.h +++ b/src/include/instruction.h @@ -4,6 +4,7 @@ #ifndef __INSTRUCTION_H #define __INSTRUCTION_H +#include #include #include "types.h" @@ -24,34 +25,52 @@ namespace Harp { class Instruction { public: - enum Opcode { NOP, DI, EI, TLBADD, TLBFLUSH, NEG, NOT, AND, - OR, XOR, ADD, SUB, MUL, DIV, MOD, SHL, - SHR, ANDI, ORI, XORI, ADDI, SUBI, MULI, DIVI, - MODI, SHLI, SHRI, JALI, JALR, JMPI, JMPR, CLONE, - JALIS, JALRS, JMPRT, LD, ST, LDI, RTOP, ANDP, - ORP, XORP, NOTP, ISNEG, ISZERO, HALT, TRAP, JMPRU, - SKEP, RETI, TLBRM, ITOF, FTOI, FADD, FSUB, FMUL, - FDIV, FNEG, WSPAWN, SPLIT, JOIN, BAR }; - enum ArgClass { - AC_NONE, AC_2REG, AC_2IMM, AC_3REG, AC_3PREG, AC_3IMM, AC_3REGSRC, - AC_1IMM, AC_1REG, AC_3IMMSRC, AC_PREG_REG, AC_2PREG, AC_2REGSRC - }; - enum InstType { - ITYPE_NULL, ITYPE_INTBASIC, ITYPE_INTMUL, ITYPE_INTDIV, ITYPE_STACK, ITYPE_BR, - ITYPE_CALL, ITYPE_RET, ITYPE_TRAP, ITYPE_FPBASIC, ITYPE_FPMUL, ITYPE_FPDIV - }; + enum Opcode + { + NOP = 0, + R_INST = 51, + L_INST = 3, + I_INST = 19, + S_INST = 35, + B_INST = 99, + LUI_INST = 55, + AUIPC_INST = 23, + JAL_INST = 111, + JALR_INST = 103, + SYS_INST = 115 + }; + + enum InstType { N_TYPE, R_TYPE, I_TYPE, S_TYPE, B_TYPE, U_TYPE, J_TYPE }; // We build a table of instruction information out of this. - static struct InstTableEntry { + struct InstTableEntry_t { const char *opString; bool controlFlow, relAddress, allSrcArgs, privileged; - ArgClass argClass; InstType iType; - } instTable[]; + + }; Instruction() : predicated(false), nRsrc(0), nPsrc(0), immsrcPresent(false), - rdestPresent(false), pdestPresent(false), refLiteral(NULL) {} + rdestPresent(false), pdestPresent(false), refLiteral(NULL) + { + + instTable = std::map + { + {Opcode::NOP, {"nop" , false, false, false, false, InstType::N_TYPE }}, + {Opcode::R_INST, {"r_type", false, false, false, false, InstType::R_TYPE }}, + {Opcode::L_INST, {"load" , false, false, false, false, InstType::I_TYPE }}, + {Opcode::I_INST, {"i_type", false, false, false, false, InstType::I_TYPE }}, + {Opcode::S_INST, {"store" , false, false, false, false, InstType::I_TYPE }}, + {Opcode::B_INST, {"branch", true , false, false, false, InstType::B_TYPE }}, + {Opcode::LUI_INST, {"lui" , false, false, false, false, InstType::U_TYPE }}, + {Opcode::AUIPC_INST, {"auipc" , false, false, false, false, InstType::U_TYPE }}, + {Opcode::JAL_INST, {"jal" , true , false, false, false, InstType::J_TYPE }}, + {Opcode::JALR_INST, {"jalr" , true , false, false, false, InstType::I_TYPE }}, + {Opcode::SYS_INST, {"SYS" , true , false, false, false, InstType::I_TYPE }} + }; + + } void executeOn(Warp &warp); friend std::ostream &operator<<(std::ostream &, Instruction &); @@ -61,6 +80,8 @@ namespace Harp { void setPred (RegNum pReg) { predicated = true; pred = pReg; } void setDestReg (RegNum destReg) { rdestPresent = true; rdest = destReg; } void setSrcReg (RegNum srcReg) { rsrc[nRsrc++] = srcReg; } + void setFunc3 (Word func3) { this->func3 = func3; } + void setFunc7 (Word func7) { this->func7 = func7; } void setDestPReg(RegNum dPReg) { pdestPresent = true; pdest = dPReg; } void setSrcPReg (RegNum srcPReg) { psrc[nPsrc++] = srcPReg; } Word *setSrcImm () { immsrcPresent = true; immsrc = 0xa5; return &immsrc;} @@ -85,7 +106,7 @@ namespace Harp { Ref *getRefLiteral() const { return refLiteral; } /* Getters used as table lookup. */ - bool hasRelImm() const { return instTable[op].relAddress; } + bool hasRelImm() const { return (*(instTable.find(op))).second.relAddress; } private: bool predicated; @@ -95,10 +116,22 @@ namespace Harp { RegNum rsrc[MAX_REG_SOURCES], psrc[MAX_PRED_SOURCES]; bool immsrcPresent; Word immsrc; + Word func3; + Word func7; bool rdestPresent, pdestPresent; RegNum rdest, pdest; Ref *refLiteral; + + public: + static std::map instTable; + }; }; #endif + + // static struct InstTableEntry { + // const char *opString; + // bool controlFlow, relAddress, allSrcArgs, privileged; + // InstType iType; + // }; \ No newline at end of file diff --git a/src/include/obj.h b/src/include/obj.h index bfea8576..e39fa324 100644 --- a/src/include/obj.h +++ b/src/include/obj.h @@ -16,195 +16,195 @@ #include "enc.h" #include "asm-tokens.h" -namespace Harp { - class Decoder; - class Encoder; +// namespace Harp { +// class Decoder; +// class Encoder; - class Ref { - public: - std::string name; - Ref(const std::string &n, bool r, Size ib = 0): - name(n), bound(false), relative(r), ibase(ib) { } - virtual ~Ref() { } - virtual void bind(Addr addr, Addr base = 0) = 0; - virtual Addr getAddr() const = 0; +// class Ref { +// public: +// std::string name; +// Ref(const std::string &n, bool r, Size ib = 0): +// name(n), bound(false), relative(r), ibase(ib) { } +// virtual ~Ref() { } +// virtual void bind(Addr addr, Addr base = 0) = 0; +// virtual Addr getAddr() const = 0; - bool bound, relative; - Size ibase; - }; +// bool bound, relative; +// Size ibase; +// }; - /* Used in not-yet-encoded code objects, plain old data. */ - class SimpleRef : public Ref { - public: - SimpleRef(const std::string &name, Addr &addr, bool rel = false) : - Ref(name, rel), addr(addr) { } - virtual void bind(Addr addr, Addr base = 0) { - std::cout << "Attempted to bind a SimpleRef.\n"; - exit(1); - } - virtual Addr getAddr() const { return this->addr; } - Byte *getAddrPtr() { return (Byte*)&addr; } +// /* Used in not-yet-encoded code objects, plain old data. */ +// class SimpleRef : public Ref { +// public: +// SimpleRef(const std::string &name, Addr &addr, bool rel = false) : +// Ref(name, rel), addr(addr) { } +// virtual void bind(Addr addr, Addr base = 0) { +// std::cout << "Attempted to bind a SimpleRef.\n"; +// exit(1); +// } +// virtual Addr getAddr() const { return this->addr; } +// Byte *getAddrPtr() { return (Byte*)&addr; } - private: - Addr &addr; - }; +// private: +// Addr &addr; +// }; - /* Used in already-encoded code objects. */ - class OffsetRef : public Ref { - public: - OffsetRef( - const std::string &name, std::vector &v, Size offset, Size bits, - Size ws, bool rel = false, Size ibase = 0 - ) : Ref(name, rel, ibase), data(v), offset(offset), bits(bits), wordSize(ws) - {} +// /* Used in already-encoded code objects. */ +// class OffsetRef : public Ref { +// public: +// OffsetRef( +// const std::string &name, std::vector &v, Size offset, Size bits, +// Size ws, bool rel = false, Size ibase = 0 +// ) : Ref(name, rel, ibase), data(v), offset(offset), bits(bits), wordSize(ws) +// {} - virtual void bind(Addr addr, Addr base = 0) { - Size bytes(bits/8), remainder(bits%8); +// virtual void bind(Addr addr, Addr base = 0) { +// Size bytes(bits/8), remainder(bits%8); - if (relative) { - addr = addr - base; - Word_s addr_s(addr); - if ((addr_s >> bits) != ~0ull && (addr_s >> bits) != 0) goto noFit; - } else { - Addr mask = (1ull< mask) goto noFit; - } +// if (relative) { +// addr = addr - base; +// Word_s addr_s(addr); +// if ((addr_s >> bits) != ~0ull && (addr_s >> bits) != 0) goto noFit; +// } else { +// Addr mask = (1ull< mask) goto noFit; +// } - { Byte mask((1ull<>= 8; - } - data[offset+i] &= ~mask; - data[offset+i] |= (addr&mask); - bound = true; - } +// { Byte mask((1ull<>= 8; +// } +// data[offset+i] &= ~mask; +// data[offset+i] |= (addr&mask); +// bound = true; +// } - return; - noFit: - std::cout << "Attempt to bind a " << bits << "-bit " - << (relative?"":"non-") << "relative symbol to an address" - " it cannot reach.\n"; - exit(1); - } +// return; +// noFit: +// std::cout << "Attempt to bind a " << bits << "-bit " +// << (relative?"":"non-") << "relative symbol to an address" +// " it cannot reach.\n"; +// exit(1); +// } - virtual Addr getAddr() const { - Size bytes = bits/8, remainder = bits%8; - Byte mask((1< &data; - Size offset, bits, wordSize; - }; +// private: +// std::vector &data; +// Size offset, bits, wordSize; +// }; - class Chunk { - public: - Chunk(std::string n, Size a = 0, Word f = 0) : - name(n), alignment(a), bound(false), flags(f), global(false) {} - virtual ~Chunk() { for (Size i = 0; i < refs.size(); i++) delete refs[i]; } - void bind(Addr a) { address = a; bound = true; } - void setGlobal() { global = true; } - bool isGlobal() const { return global; } - std::string name; - Size alignment; - bool bound, global; - Addr address; - Word flags; - std::vector refs; - }; +// class Chunk { +// public: +// Chunk(std::string n, Size a = 0, Word f = 0) : +// name(n), alignment(a), bound(false), flags(f), global(false) {} +// virtual ~Chunk() { for (Size i = 0; i < refs.size(); i++) delete refs[i]; } +// void bind(Addr a) { address = a; bound = true; } +// void setGlobal() { global = true; } +// bool isGlobal() const { return global; } +// std::string name; +// Size alignment; +// bool bound, global; +// Addr address; +// Word flags; +// std::vector refs; +// }; - class TextChunk : public Chunk { - public: - TextChunk(std::string n, Size a = 0, Word f = 0) - : Chunk(n, a, f), instructions() {} +// class TextChunk : public Chunk { +// public: +// TextChunk(std::string n, Size a = 0, Word f = 0) +// : Chunk(n, a, f), instructions() {} - ~TextChunk() { - for (Size i = 0; i < instructions.size(); i++) delete instructions[i]; - } +// ~TextChunk() { +// for (Size i = 0; i < instructions.size(); i++) delete instructions[i]; +// } - std::vector instructions; - }; +// std::vector instructions; +// }; - class DataChunk : public Chunk { - public: - DataChunk(std::string n, Size a = 0, Word f = 0) - : Chunk(n, a, f), size(0), contents() {} - Size size; - std::vector contents; /* 0 to size bytes in length. */ - }; +// class DataChunk : public Chunk { +// public: +// DataChunk(std::string n, Size a = 0, Word f = 0) +// : Chunk(n, a, f), size(0), contents() {} +// Size size; +// std::vector contents; /* 0 to size bytes in length. */ +// }; - class Obj { - public: - ~Obj() { for (Size i = 0; i < chunks.size(); i++) delete chunks[i]; } - std::vector chunks; - Size entry; - }; +// class Obj { +// public: +// ~Obj() { for (Size i = 0; i < chunks.size(); i++) delete chunks[i]; } +// std::vector chunks; +// Size entry; +// }; - class DynObj : public Obj { - public: - std::vector deps; - }; +// class DynObj : public Obj { +// public: +// std::vector deps; +// }; - class ObjReader { - public: - virtual Obj *read(std::istream &input) = 0; - private: - }; +// class ObjReader { +// public: +// virtual Obj *read(std::istream &input) = 0; +// private: +// }; - class ObjWriter { - public: - virtual void write(std::ostream &output, const Obj &o) = 0; - private: - }; +// class ObjWriter { +// public: +// virtual void write(std::ostream &output, const Obj &o) = 0; +// private: +// }; - class AsmReader : public ObjReader { - public: - AsmReader(ArchDef arch) : - wordSize(arch.getWordSize()), nRegs(arch.getNRegs()) {} - virtual Obj *read(std::istream &input); - private: - Size wordSize, nRegs; +// class AsmReader : public ObjReader { +// public: +// AsmReader(ArchDef arch) : +// wordSize(arch.getWordSize()), nRegs(arch.getNRegs()) {} +// virtual Obj *read(std::istream &input); +// private: +// Size wordSize, nRegs; - // Operand type sequences indexed by argument class - enum ArgType {AT_END, AT_REG, AT_PREG, AT_LIT}; - static ArgType operandtype_table[][4]; // ArgClass -> ArgType[arg_idx] - }; +// // Operand type sequences indexed by argument class +// enum ArgType {AT_END, AT_REG, AT_PREG, AT_LIT}; +// static ArgType operandtype_table[][4]; // ArgClass -> ArgType[arg_idx] +// }; - class HOFReader : public ObjReader { - public: - HOFReader(ArchDef &arch) : arch(arch) {} - Obj *read(std::istream &input); - private: - const ArchDef &arch; - }; +// class HOFReader : public ObjReader { +// public: +// HOFReader(ArchDef &arch) : arch(arch) {} +// Obj *read(std::istream &input); +// private: +// const ArchDef &arch; +// }; - class AsmWriter : public ObjWriter { - public: - AsmWriter(ArchDef arch): wordSize(arch.getWordSize()) {} - virtual void write(std::ostream &output, const Obj &obj); - private: - Size wordSize; - }; +// class AsmWriter : public ObjWriter { +// public: +// AsmWriter(ArchDef arch): wordSize(arch.getWordSize()) {} +// virtual void write(std::ostream &output, const Obj &obj); +// private: +// Size wordSize; +// }; - class HOFWriter : public ObjWriter { - public: - HOFWriter(ArchDef &arch) : arch(arch) {} - virtual void write(std::ostream &output, const Obj &obj); - private: - const ArchDef &arch; - }; -}; +// class HOFWriter : public ObjWriter { +// public: +// HOFWriter(ArchDef &arch) : arch(arch) {} +// virtual void write(std::ostream &output, const Obj &obj); +// private: +// const ArchDef &arch; +// }; +// }; #endif diff --git a/src/instruction.cpp b/src/instruction.cpp index 84cbc4d4..a6ea3957 100644 --- a/src/instruction.cpp +++ b/src/instruction.cpp @@ -20,93 +20,28 @@ using namespace std; /* It is important that this stays consistent with the Harp::Instruction::Opcode enum. */ -Instruction::InstTableEntry Instruction::instTable[] = { - //str cflow relad allsrc priv argcl itype - {"nop", false, false, false, false, AC_NONE, ITYPE_NULL }, - {"di", false, false, false, true, AC_NONE, ITYPE_NULL }, - {"ei", false, false, false, true, AC_NONE, ITYPE_NULL }, - {"tlbadd", false, false, true, true, AC_3REGSRC, ITYPE_NULL }, - {"tlbflush", false, false, false, true, AC_NONE, ITYPE_NULL }, - {"neg", false, false, false, false, AC_2REG, ITYPE_INTBASIC}, - {"not", false, false, false, false, AC_2REG, ITYPE_INTBASIC}, - {"and", false, false, false, false, AC_3REG, ITYPE_INTBASIC}, - {"or", false, false, false, false, AC_3REG, ITYPE_INTBASIC}, - {"xor", false, false, false, false, AC_3REG, ITYPE_INTBASIC}, - {"add", false, false, false, false, AC_3REG, ITYPE_INTBASIC}, - {"sub", false, false, false, false, AC_3REG, ITYPE_INTBASIC}, - {"mul", false, false, false, false, AC_3REG, ITYPE_INTMUL }, - {"div", false, false, false, false, AC_3REG, ITYPE_INTDIV }, - {"mod", false, false, false, false, AC_3REG, ITYPE_INTDIV }, - {"shl", false, false, false, false, AC_3REG, ITYPE_INTBASIC}, - {"shr", false, false, false, false, AC_3REG, ITYPE_INTBASIC}, - {"andi", false, false, false, false, AC_3IMM, ITYPE_INTBASIC}, - {"ori", false, false, false, false, AC_3IMM, ITYPE_INTBASIC}, - {"xori", false, false, false, false, AC_3IMM, ITYPE_INTBASIC}, - {"addi", false, false, false, false, AC_3IMM, ITYPE_INTBASIC}, - {"subi", false, false, false, false, AC_3IMM, ITYPE_INTBASIC}, - {"muli", false, false, false, false, AC_3IMM, ITYPE_INTMUL }, - {"divi", false, false, false, false, AC_3IMM, ITYPE_INTDIV }, - {"modi", false, false, false, false, AC_3IMM, ITYPE_INTDIV }, - {"shli", false, false, false, false, AC_3IMM, ITYPE_INTBASIC}, - {"shri", false, false, false, false, AC_3IMM, ITYPE_INTBASIC}, - {"jali", true, true, false, false, AC_2IMM, ITYPE_CALL }, - {"jalr", true, false, false, false, AC_2REG, ITYPE_CALL }, - {"jmpi", true, true, true, false, AC_1IMM, ITYPE_BR }, - {"jmpr", true, false, true, false, AC_1REG, ITYPE_RET }, - {"clone", true, false, false, false, AC_1REG, ITYPE_NULL }, - {"jalis", true, true, false, false, AC_3IMM, ITYPE_CALL }, - {"jalrs", true, false, false, false, AC_3REG, ITYPE_CALL }, - {"jmprt", true, false, true, false, AC_1REG, ITYPE_RET }, - {"ld", false, false, false, false, AC_3IMM, ITYPE_NULL }, - {"st", false, false, true, false, AC_3IMMSRC, ITYPE_NULL }, - {"ldi", false, false, false, false, AC_2IMM, ITYPE_NULL }, - {"rtop", false, false, false, false, AC_PREG_REG, ITYPE_NULL }, - {"andp", false, false, false, false, AC_3PREG, ITYPE_INTBASIC}, - {"orp", false, false, false, false, AC_3PREG, ITYPE_INTBASIC}, - {"xorp", false, false, false, false, AC_3PREG, ITYPE_INTBASIC}, - {"notp", false, false, false, false, AC_2PREG, ITYPE_INTBASIC}, - {"isneg", false, false, false, false, AC_PREG_REG, ITYPE_INTBASIC}, - {"iszero", false, false, false, false, AC_PREG_REG, ITYPE_INTBASIC}, - {"halt", false, false, false, false, AC_NONE, ITYPE_NULL }, - {"trap", true, false, false, false, AC_NONE, ITYPE_TRAP }, - {"jmpru", false, false, false, true, AC_1REG, ITYPE_RET }, - {"skep", false, false, false, true, AC_1REG, ITYPE_NULL }, - {"reti", true, false, false, true, AC_NONE, ITYPE_RET }, - {"tlbrm", false, false, false, true, AC_1REG, ITYPE_NULL }, - {"itof", false, false, false, false, AC_2REG, ITYPE_FPBASIC }, - {"ftoi", false, false, false, false, AC_2REG, ITYPE_FPBASIC }, - {"fadd", false, false, false, false, AC_3REG, ITYPE_FPBASIC }, - {"fsub", false, false, false, false, AC_3REG, ITYPE_FPBASIC }, - {"fmul", false, false, false, false, AC_3REG, ITYPE_FPMUL }, - {"fdiv", false, false, false, false, AC_3REG, ITYPE_FPDIV }, - {"fneg", false, false, false, false, AC_2REG, ITYPE_FPBASIC }, - {"wspawn", false, false, true, false, AC_3REG, ITYPE_NULL }, - {"split", false, false, true, false, AC_NONE, ITYPE_NULL }, - {"join", false, false, true, false, AC_NONE, ITYPE_NULL }, - {"bar", false, false, true, false, AC_2REGSRC, ITYPE_NULL }, - {NULL,false,false,false,false,AC_NONE,ITYPE_NULL}/////// End of table. -}; - ostream &Harp::operator<<(ostream& os, Instruction &inst) { os << dec; - if (inst.predicated) { - os << "@p" << dec << inst.pred << " ? "; - } + // if (inst.predicated) { + // os << "@p" << dec << inst.pred << " ? "; + // } - os << Instruction::instTable[inst.op].opString << ' '; - if (inst.rdestPresent) os << "%r" << dec << inst.rdest << ' '; - if (inst.pdestPresent) os << "@p" << inst.pdest << ' '; - for (int i = 0; i < inst.nRsrc; i++) { - os << "%r" << dec << inst.rsrc[i] << ' '; - } - for (int i = 0; i < inst.nPsrc; i++) { - os << "@p" << dec << inst.psrc[i] << ' '; - } - if (inst.immsrcPresent) { - if (inst.refLiteral) os << inst.refLiteral->name; - else os << "#0x" << hex << inst.immsrc; - } + // os << inst.instTable[inst.op].opString << ' '; + // if (inst.rdestPresent) os << "%r" << dec << inst.rdest << ' '; + // if (inst.pdestPresent) os << "@p" << inst.pdest << ' '; + // for (int i = 0; i < inst.nRsrc; i++) { + // os << "%r" << dec << inst.rsrc[i] << ' '; + // } + // for (int i = 0; i < inst.nPsrc; i++) { + // os << "@p" << dec << inst.psrc[i] << ' '; + // } + // if (inst.immsrcPresent) { + // if (inst.refLiteral) os << inst.refLiteral->name; + // else os << "#0x" << hex << inst.immsrc; + // } + + os << inst.instTable[inst.op].opString; os << ';'; return os; @@ -134,6 +69,11 @@ bool checkUnanimous(unsigned p, const std::vector > >& m, return true; } +Word signExt(Word w, Size bit, Word mask) { + if (w>>(bit-1)) w |= ~mask; + return w; +} + void Instruction::executeOn(Warp &c) { D(3, "Begin instruction execute."); @@ -165,7 +105,7 @@ void Instruction::executeOn(Warp &c) { // If we have a load, overwriting a register's contents, we have to make sure // ahead of time it will not fault. Otherwise we may perform an indirect load // by mistake. - if (op == LD && rdest == rsrc[0]) { + if (op == L_INST && rdest == rsrc[0]) { for (Size t = 0; t < c.activeThreads; t++) { if ((!predicated || c.pred[t][pred]) && c.tmask[t]) { Word memAddr = c.reg[t][rsrc[0]] + immsrc; @@ -183,261 +123,341 @@ void Instruction::executeOn(Warp &c) { // If this thread is masked out, don't execute the instruction, unless it's // a split or join. - if (((predicated && !pReg[pred]) || !c.tmask[t]) && - op != SPLIT && op != JOIN) continue; + // if (((predicated && !pReg[pred]) || !c.tmask[t]) && + // op != SPLIT && op != JOIN) continue; ++c.insts; - Word memAddr; + Word memAddr; + Word shift_by; switch (op) { + case NOP: break; - case DI: c.interruptEnable = false; - break; - case EI: c.interruptEnable = true; - break; - case TLBADD: c.core->mem.tlbAdd(reg[rsrc[0]], reg[rsrc[1]], reg[rsrc[2]]); - break; - case TLBFLUSH: c.core->mem.tlbFlush(); - break; - case ADD: reg[rdest] = reg[rsrc[0]] + reg[rsrc[1]]; + case R_INST: + switch (func3) + { + case 0: + if (func7) + { + reg[rdest] = reg[rsrc[0]] - reg[rsrc[1]]; reg[rdest].trunc(wordSz); - break; - case SUB: reg[rdest] = reg[rsrc[0]] - reg[rsrc[1]]; + } + else + { + reg[rdest] = reg[rsrc[0]] + reg[rsrc[1]]; reg[rdest].trunc(wordSz); - break; - case MUL: reg[rdest] = reg[rsrc[0]] * reg[rsrc[1]]; + } + break; + case 1: + reg[rdest] = reg[rsrc[0]] << reg[rsrc[1]]; reg[rdest].trunc(wordSz); - break; - case DIV: if (reg[rsrc[1]] == 0) throw DomainException(); - reg[rdest] = reg[rsrc[0]] / reg[rsrc[1]]; - break; - case SHL: reg[rdest] = reg[rsrc[0]] << reg[rsrc[1]]; + break; + case 2: + if ( Word_s(reg[rsrc[0]]) < Word_s(reg[rsrc[1]])) + { + reg[rdest] = 1; + } + else + { + reg[rdest] = 0; + } + break; + case 3: + if ( Word_u(reg[rsrc[0]]) < Word_u(reg[rsrc[1]])) + { + reg[rdest] = 1; + } + else + { + reg[rdest] = 0; + } + break; + case 4: + reg[rdest] = reg[rsrc[0]] ^ reg[rsrc[1]]; + break; + case 5: + if (func7) + { + reg[rdest] = Word_s(reg[rsrc[0]]) >> Word_s(reg[rsrc[1]]); reg[rdest].trunc(wordSz); - break; - case SHR: reg[rdest] = Word_s(reg[rsrc[0]]) >> reg[rsrc[1]]; + } + else + { + reg[rdest] = Word_u(reg[rsrc[0]]) >> Word_u(reg[rsrc[1]]); reg[rdest].trunc(wordSz); - break; - case MOD: if (reg[rsrc[1]] == 0) throw DomainException(); - reg[rdest] = reg[rsrc[0]] % reg[rsrc[1]]; - break; - case AND: reg[rdest] = reg[rsrc[0]] & reg[rsrc[1]]; - break; - case OR: reg[rdest] = reg[rsrc[0]] | reg[rsrc[1]]; - break; - case XOR: reg[rdest] = reg[rsrc[0]] ^ reg[rsrc[1]]; - break; - case NEG: reg[rdest] = -(Word_s)reg[rsrc[0]]; - reg[rdest].trunc(wordSz); - break; - case NOT: reg[rdest] = ~(Word_s)reg[rsrc[0]]; - reg[rdest].trunc(wordSz); - break; - case ADDI: reg[rdest] = reg[rsrc[0]] + immsrc; - reg[rdest].trunc(wordSz); - break; - case SUBI: reg[rdest] = reg[rsrc[0]] - immsrc; - reg[rdest].trunc(wordSz); - break; - case MULI: reg[rdest] = reg[rsrc[0]] * immsrc; - reg[rdest].trunc(wordSz); - break; - case DIVI: if (immsrc == 0) throw DomainException(); - reg[rdest] = reg[rsrc[0]] / immsrc; - break; - case MODI: if (immsrc == 0) throw DomainException(); - reg[rdest] = reg[rsrc[0]] % immsrc; - break; - case SHRI: reg[rdest] = Word_s(reg[rsrc[0]]) >> immsrc; - break; - case SHLI: reg[rdest] = reg[rsrc[0]] << immsrc; - reg[rdest].trunc(wordSz); - break; - case ANDI: reg[rdest] = reg[rsrc[0]] & immsrc; - break; - case ORI: reg[rdest] = reg[rsrc[0]] | immsrc; - break; - case XORI: reg[rdest] = reg[rsrc[0]] ^ immsrc; - break; - case JMPI: if (!pcSet) nextPc = c.pc + immsrc; - pcSet = true; - break; - case JALI: reg[rdest] = c.pc; - if (!pcSet) nextPc = c.pc + immsrc; - pcSet = true; - break; - case JALR: reg[rdest] = c.pc; - if (!pcSet) nextPc = reg[rsrc[0]]; - pcSet = true; - break; - case JMPR: if (!pcSet) nextPc = reg[rsrc[0]]; - pcSet = true; - break; - case CLONE: c.reg[reg[rsrc[0]]] = reg; - break; - case JALIS: nextActiveThreads = reg[rsrc[0]]; - reg[rdest] = c.pc; - if (!pcSet) nextPc = c.pc + immsrc; - pcSet = true; - break; - case JALRS: nextActiveThreads = reg[rsrc[0]]; - reg[rdest] = c.pc; - if (!pcSet) nextPc = reg[rsrc[1]]; - pcSet = true; - break; - case JMPRT: nextActiveThreads = 1; - if (!pcSet) nextPc = reg[rsrc[0]]; - pcSet = true; - break; - case LD: ++c.loads; - memAddr = reg[rsrc[0]] + immsrc; + } + break; + case 6: + reg[rdest] = reg[rsrc[0]] | reg[rsrc[1]]; + break; + case 7: + reg[rdest] = reg[rsrc[0]] & reg[rsrc[1]]; + break; + default: + cout << "ERROR: UNSUPPORTED R INST\n"; + exit(1); + } + break; + + case L_INST: + memAddr = (reg[rsrc[0]] + immsrc) & 0xFFFFFF00; + shift_by = (reg[rsrc[0]] + immsrc) & 0x000000FF; #ifdef EMU_INSTRUMENTATION - Harp::OSDomain::osDomain-> - do_mem(0, memAddr, c.core->mem.virtToPhys(memAddr), 8, true); + Harp::OSDomain::osDomain-> + do_mem(0, memAddr, c.core->mem.virtToPhys(memAddr), 8, true); #endif - reg[rdest] = c.core->mem.read(memAddr, c.supervisorMode); - c.memAccesses.push_back(Warp::MemAccess(false, memAddr)); - break; - case ST: ++c.stores; - memAddr = reg[rsrc[1]] + immsrc; - c.core->mem.write(memAddr, reg[rsrc[0]], c.supervisorMode); - c.memAccesses.push_back(Warp::MemAccess(true, memAddr)); -#ifdef EMU_INSTRUMENTATION - Harp::OSDomain::osDomain-> - do_mem(0, memAddr, c.core->mem.virtToPhys(memAddr), 8, true); -#endif - break; - case LDI: reg[rdest] = immsrc; - reg[rdest].trunc(wordSz); - break; - case RTOP: pReg[pdest] = reg[rsrc[0]]; - break; - case ISZERO: pReg[pdest] = !reg[rsrc[0]]; - break; - case NOTP: pReg[pdest] = !(pReg[psrc[0]]); - break; - case ANDP: pReg[pdest] = pReg[psrc[0]] & pReg[psrc[1]]; - break; - case ORP: pReg[pdest] = pReg[psrc[0]] | pReg[psrc[1]]; - break; - case XORP: pReg[pdest] = pReg[psrc[0]] != pReg[psrc[1]]; - break; - case ISNEG: pReg[pdest] = (1ll<<(wordSz*8 - 1))®[rsrc[0]]; - break; - case HALT: c.activeThreads = 0; - nextActiveThreads = 0; - break; - case TRAP: c.interrupt(0); - break; - case JMPRU: c.supervisorMode = false; - if (!pcSet) nextPc = reg[rsrc[0]]; - pcSet = true; - break; - case SKEP: c.core->interruptEntry = reg[rsrc[0]]; - break; - case RETI: if (t == 0) { - c.tmask = c.shadowTmask; - nextActiveThreads = c.shadowActiveThreads; - c.interruptEnable = c.shadowInterruptEnable; - c.supervisorMode = c.shadowSupervisorMode; - for (unsigned i = 0; i < reg.size(); ++i) - reg[i] = c.shadowReg[i]; - for (unsigned i = 0; i < pReg.size(); ++i) - pReg[i] = c.shadowPReg[i]; - if (!pcSet) { nextPc = c.shadowPc; pcSet = true; } - } - break; - case ITOF: reg[rdest] = Float(double(Word_s(reg[rsrc[0]])), wordSz); - break; - case FTOI: reg[rdest] = Word_s(double(Float(reg[rsrc[0]], wordSz))); - reg[rdest].trunc(wordSz); - break; - case FNEG: reg[rdest] = Float(-double(Float(reg[rsrc[0]],wordSz)),wordSz); - break; - case FADD: reg[rdest] = Float(double(Float(reg[rsrc[0]], wordSz)) + - double(Float(reg[rsrc[1]], wordSz)),wordSz); - break; - case FSUB: reg[rdest] = Float(double(Float(reg[rsrc[0]], wordSz)) - - double(Float(reg[rsrc[1]], wordSz)),wordSz); - break; - case FMUL: reg[rdest] = Float(double(Float(reg[rsrc[0]], wordSz)) * - double(Float(reg[rsrc[1]], wordSz)),wordSz); - break; - case FDIV: reg[rdest] = Float(double(Float(reg[rsrc[0]], wordSz)) / - double(Float(reg[rsrc[1]], wordSz)),wordSz); - break; - case SPLIT: if (sjOnce) { - sjOnce = false; - if (checkUnanimous(pred, c.pred, c.tmask)) { - DomStackEntry e(c.tmask); - e.uni = true; - c.domStack.push(e); - break; - } - DomStackEntry e(pred, c.pred, c.tmask, c.pc); - c.domStack.push(c.tmask); - c.domStack.push(e); - for (unsigned i = 0; i < e.tmask.size(); ++i) - c.tmask[i] = !e.tmask[i] && c.tmask[i]; - } - break; - case JOIN: if (sjOnce) { - sjOnce = false; - if (!c.domStack.empty() && c.domStack.top().uni) { - D(2, "Uni branch at join"); - c.tmask = c.domStack.top().tmask; - c.domStack.pop(); - break; - } - if (!c.domStack.top().fallThrough) { - if (!pcSet) nextPc = c.domStack.top().pc; - pcSet = true; - } - c.tmask = c.domStack.top().tmask; - c.domStack.pop(); - } - break; - case WSPAWN: if (sjOnce) { - sjOnce = false; - D(0, "Spawning a new warp."); - for (unsigned i = 0; i < c.core->w.size(); ++i) { - Warp &newWarp(c.core->w[i]); - if (newWarp.spawned == false) { - newWarp.pc = reg[rsrc[0]]; - newWarp.reg[0][rdest] = reg[rsrc[1]]; - newWarp.activeThreads = 1; - newWarp.supervisorMode = false; - newWarp.spawned = true; - break; - } - } - } - break; - case BAR: if (sjOnce) { - sjOnce = false; - Word id(reg[rsrc[0]]), n(reg[rsrc[1]]); - set &b(c.core->b[id]); - - // Add current warp to the barrier and halt. - b.insert(&c); - c.shadowActiveThreads = c.activeThreads; - nextActiveThreads = 0; - - D(2, "Barrier " << id << ' ' << b.size() << " of " << n); - - // If the barrier's full, reactivate warps waiting at it - if (b.size() == n) { - set::iterator it; - for (it = b.begin(); it != b.end(); ++it) - (*it)->activeThreads = (*it)->shadowActiveThreads; - c.core->b.erase(id); - nextActiveThreads = c.shadowActiveThreads; - } - } - break; + switch (func3) + { + case 0: + // LB + reg[rdest] = signExt((c.core->mem.read(memAddr, c.supervisorMode) >> shift_by) & 0xFF, 8, 0xFF); + break; + case 1: + // LH + reg[rdest] = signExt((c.core->mem.read(memAddr, c.supervisorMode) >> shift_by) & 0xFFFF, 16, 0xFF); + break; + case 2: + reg[rdest] = Word_s(c.core->mem.read(memAddr, c.supervisorMode) & 0xFFFFFFFF); + break; + case 4: + // LBU + reg[rdest] = Word_u((c.core->mem.read(memAddr, c.supervisorMode) >> shift_by) & 0xFF); + break; + case 5: + reg[rdest] = Word_s((c.core->mem.read(memAddr, c.supervisorMode) >> shift_by) & 0xFFFF); + default: + cout << "ERROR: UNSUPPORTED L INST\n"; + exit(1); + c.memAccesses.push_back(Warp::MemAccess(false, memAddr)); + } + break; + case I_INST: + break; + case S_INST: + break; + case B_INST: + break; + case LUI_INST: + break; + case AUIPC_INST: + break; + case JAL_INST: + break; + case JALR_INST: + break; + case SYS_INST: + break; default: cout << "ERROR: Unsupported instruction: " << *this << "\n"; exit(1); + + +// case SHL: +// break; +// case SHR: +// break; + + + + +// case ADDI: reg[rdest] = reg[rsrc[0]] + immsrc; +// reg[rdest].trunc(wordSz); +// break; +// case SUBI: reg[rdest] = reg[rsrc[0]] - immsrc; +// reg[rdest].trunc(wordSz); +// break; + +// case SHRI: reg[rdest] = Word_s(reg[rsrc[0]]) >> immsrc; +// break; +// case SHLI: reg[rdest] = reg[rsrc[0]] << immsrc; +// reg[rdest].trunc(wordSz); +// break; +// case ANDI: reg[rdest] = reg[rsrc[0]] & immsrc; +// break; +// case ORI: reg[rdest] = reg[rsrc[0]] | immsrc; +// break; +// case XORI: reg[rdest] = reg[rsrc[0]] ^ immsrc; +// break; +// case JMPI: if (!pcSet) nextPc = c.pc + immsrc; +// pcSet = true; +// break; +// case JALI: reg[rdest] = c.pc; +// if (!pcSet) nextPc = c.pc + immsrc; +// pcSet = true; +// break; +// case JALR: reg[rdest] = c.pc; +// if (!pcSet) nextPc = reg[rsrc[0]]; +// pcSet = true; +// break; +// case JMPR: if (!pcSet) nextPc = reg[rsrc[0]]; +// pcSet = true; +// break; + +// case JALIS: nextActiveThreads = reg[rsrc[0]]; +// reg[rdest] = c.pc; +// if (!pcSet) nextPc = c.pc + immsrc; +// pcSet = true; +// break; +// case JALRS: nextActiveThreads = reg[rsrc[0]]; +// reg[rdest] = c.pc; +// if (!pcSet) nextPc = reg[rsrc[1]]; +// pcSet = true; +// break; +// case JMPRT: nextActiveThreads = 1; +// if (!pcSet) nextPc = reg[rsrc[0]]; +// pcSet = true; +// break; +// case LD: ++c.loads; +// memAddr = reg[rsrc[0]] + immsrc; +// #ifdef EMU_INSTRUMENTATION +// Harp::OSDomain::osDomain-> +// do_mem(0, memAddr, c.core->mem.virtToPhys(memAddr), 8, true); +// #endif +// reg[rdest] = c.core->mem.read(memAddr, c.supervisorMode); +// c.memAccesses.push_back(Warp::MemAccess(false, memAddr)); +// break; +// case ST: ++c.stores; +// memAddr = reg[rsrc[1]] + immsrc; +// c.core->mem.write(memAddr, reg[rsrc[0]], c.supervisorMode); +// c.memAccesses.push_back(Warp::MemAccess(true, memAddr)); +// #ifdef EMU_INSTRUMENTATION +// Harp::OSDomain::osDomain-> +// do_mem(0, memAddr, c.core->mem.virtToPhys(memAddr), 8, true); +// #endif +// break; +// case LDI: reg[rdest] = immsrc; +// reg[rdest].trunc(wordSz); +// break; +// case RTOP: pReg[pdest] = reg[rsrc[0]]; +// break; +// case ISZERO: pReg[pdest] = !reg[rsrc[0]]; +// break; +// case NOTP: pReg[pdest] = !(pReg[psrc[0]]); +// break; +// case ANDP: pReg[pdest] = pReg[psrc[0]] & pReg[psrc[1]]; +// break; +// case ORP: pReg[pdest] = pReg[psrc[0]] | pReg[psrc[1]]; +// break; +// case XORP: pReg[pdest] = pReg[psrc[0]] != pReg[psrc[1]]; +// break; +// case ISNEG: pReg[pdest] = (1ll<<(wordSz*8 - 1))®[rsrc[0]]; +// break; +// case HALT: c.activeThreads = 0; +// nextActiveThreads = 0; +// break; +// case TRAP: c.interrupt(0); +// break; +// case JMPRU: c.supervisorMode = false; +// if (!pcSet) nextPc = reg[rsrc[0]]; +// pcSet = true; +// break; +// case SKEP: c.core->interruptEntry = reg[rsrc[0]]; +// break; +// case RETI: if (t == 0) { +// c.tmask = c.shadowTmask; +// nextActiveThreads = c.shadowActiveThreads; +// c.interruptEnable = c.shadowInterruptEnable; +// c.supervisorMode = c.shadowSupervisorMode; +// for (unsigned i = 0; i < reg.size(); ++i) +// reg[i] = c.shadowReg[i]; +// for (unsigned i = 0; i < pReg.size(); ++i) +// pReg[i] = c.shadowPReg[i]; +// if (!pcSet) { nextPc = c.shadowPc; pcSet = true; } +// } +// break; +// case ITOF: reg[rdest] = Float(double(Word_s(reg[rsrc[0]])), wordSz); +// break; +// case FTOI: reg[rdest] = Word_s(double(Float(reg[rsrc[0]], wordSz))); +// reg[rdest].trunc(wordSz); +// break; +// case FNEG: reg[rdest] = Float(-double(Float(reg[rsrc[0]],wordSz)),wordSz); +// break; +// case FADD: reg[rdest] = Float(double(Float(reg[rsrc[0]], wordSz)) + +// double(Float(reg[rsrc[1]], wordSz)),wordSz); +// break; +// case FSUB: reg[rdest] = Float(double(Float(reg[rsrc[0]], wordSz)) - +// double(Float(reg[rsrc[1]], wordSz)),wordSz); +// break; +// case FMUL: reg[rdest] = Float(double(Float(reg[rsrc[0]], wordSz)) * +// double(Float(reg[rsrc[1]], wordSz)),wordSz); +// break; +// case FDIV: reg[rdest] = Float(double(Float(reg[rsrc[0]], wordSz)) / +// double(Float(reg[rsrc[1]], wordSz)),wordSz); +// break; +// case SPLIT: if (sjOnce) { +// sjOnce = false; +// if (checkUnanimous(pred, c.pred, c.tmask)) { +// DomStackEntry e(c.tmask); +// e.uni = true; +// c.domStack.push(e); +// break; +// } +// DomStackEntry e(pred, c.pred, c.tmask, c.pc); +// c.domStack.push(c.tmask); +// c.domStack.push(e); +// for (unsigned i = 0; i < e.tmask.size(); ++i) +// c.tmask[i] = !e.tmask[i] && c.tmask[i]; +// } +// break; +// case JOIN: if (sjOnce) { +// sjOnce = false; +// if (!c.domStack.empty() && c.domStack.top().uni) { +// D(2, "Uni branch at join"); +// c.tmask = c.domStack.top().tmask; +// c.domStack.pop(); +// break; +// } +// if (!c.domStack.top().fallThrough) { +// if (!pcSet) nextPc = c.domStack.top().pc; +// pcSet = true; +// } +// c.tmask = c.domStack.top().tmask; +// c.domStack.pop(); +// } +// break; +// case WSPAWN: if (sjOnce) { +// sjOnce = false; +// D(0, "Spawning a new warp."); +// for (unsigned i = 0; i < c.core->w.size(); ++i) { +// Warp &newWarp(c.core->w[i]); +// if (newWarp.spawned == false) { +// newWarp.pc = reg[rsrc[0]]; +// newWarp.reg[0][rdest] = reg[rsrc[1]]; +// newWarp.activeThreads = 1; +// newWarp.supervisorMode = false; +// newWarp.spawned = true; +// break; +// } +// } +// } +// break; +// case BAR: if (sjOnce) { +// sjOnce = false; +// Word id(reg[rsrc[0]]), n(reg[rsrc[1]]); +// set &b(c.core->b[id]); + +// // Add current warp to the barrier and halt. +// b.insert(&c); +// c.shadowActiveThreads = c.activeThreads; +// nextActiveThreads = 0; + +// D(2, "Barrier " << id << ' ' << b.size() << " of " << n); + +// // If the barrier's full, reactivate warps waiting at it +// if (b.size() == n) { +// set::iterator it; +// for (it = b.begin(); it != b.end(); ++it) +// (*it)->activeThreads = (*it)->shadowActiveThreads; +// c.core->b.erase(id); +// nextActiveThreads = c.shadowActiveThreads; +// } +// } +// break; + +// default: +// cout << "ERROR: Unsupported instruction: " << *this << "\n"; +// exit(1); } } diff --git a/src/obj.cpp b/src/obj.cpp index 913c10eb..106f0e0b 100644 --- a/src/obj.cpp +++ b/src/obj.cpp @@ -17,732 +17,732 @@ #include -using namespace std; -using namespace Harp; -using namespace HarpTools; +// using namespace std; +// using namespace Harp; +// using namespace HarpTools; -extern struct rval_t { std::string s; uint64_t u; } yylval; -extern unsigned yyline; +// extern struct rval_t { std::string s; uint64_t u; } yylval; +// extern unsigned yyline; -static void asmReaderError(unsigned line, const string &message) { - cout << "Assembly reader error, line " << line << ": " << message << '\n'; - exit(1); -} +// static void asmReaderError(unsigned line, const string &message) { +// cout << "Assembly reader error, line " << line << ": " << message << '\n'; +// exit(1); +// } -static int skip_parens(const string &s, int i) { - int paren_level = 1; - do { - i++; - if (s[i] == '(') paren_level++; - if (s[i] == ')') paren_level--; - } while (paren_level > 0); +// static int skip_parens(const string &s, int i) { +// int paren_level = 1; +// do { +// i++; +// if (s[i] == '(') paren_level++; +// if (s[i] == ')') paren_level--; +// } while (paren_level > 0); - return i; -} +// return i; +// } -// Probably the worst recursive descent parser ever written, but it's an easy -// way to make our assembly language prettier. -static uint64_t rPE(bool &v, const string &s, const map &d, - int start, int end) -{ - D(2, "rPE(" << v << ", " << s << ", d, " << start << ", " << end << ");"); +// // Probably the worst recursive descent parser ever written, but it's an easy +// // way to make our assembly language prettier. +// static uint64_t rPE(bool &v, const string &s, const map &d, +// int start, int end) +// { +// D(2, "rPE(" << v << ", " << s << ", d, " << start << ", " << end << ");"); - if (end == start) { v = false; return 0; } +// if (end == start) { v = false; return 0; } - while (isspace(s[start])) start++; - while (isspace(s[end-1])) end--; +// while (isspace(s[start])) start++; +// while (isspace(s[end-1])) end--; - for (int i = start; i < end; i++) { - if (s[i] == '(') { i = skip_parens(s, i); continue; } +// for (int i = start; i < end; i++) { +// if (s[i] == '(') { i = skip_parens(s, i); continue; } - if (s[i] == '<') return rPE(v, s, d, start, i) << rPE(v, s, d, i+2, end); - if (s[i] == '>') return rPE(v, s, d, start, i) >> rPE(v, s, d, i+2, end); - } +// if (s[i] == '<') return rPE(v, s, d, start, i) << rPE(v, s, d, i+2, end); +// if (s[i] == '>') return rPE(v, s, d, start, i) >> rPE(v, s, d, i+2, end); +// } - for (int i = start; i < end; i++) { - if (s[i] == '(') { i = skip_parens(s, i); continue; } - if (s[i] == '+') return rPE(v, s, d, start, i) + rPE(v, s, d, i+1, end); - if (s[i] == '-') { - // If we've already failed, don't try this. - if (v == false) return 0; +// for (int i = start; i < end; i++) { +// if (s[i] == '(') { i = skip_parens(s, i); continue; } +// if (s[i] == '+') return rPE(v, s, d, start, i) + rPE(v, s, d, i+1, end); +// if (s[i] == '-') { +// // If we've already failed, don't try this. +// if (v == false) return 0; - // If it works as a binary -, return that. Otherwise, it's a unary - - uint64_t x(rPE(v, s, d, start, i) - rPE(v, s, d, i+1, end)); - if (v) return x; - else v = true; - } - if (s[i] == '|') return rPE(v, s, d, start, i) | rPE(v, s, d, i+1, end); - if (s[i] == '^') return rPE(v, s, d, start, i) ^ rPE(v, s, d, i+1, end); - } +// // If it works as a binary -, return that. Otherwise, it's a unary - +// uint64_t x(rPE(v, s, d, start, i) - rPE(v, s, d, i+1, end)); +// if (v) return x; +// else v = true; +// } +// if (s[i] == '|') return rPE(v, s, d, start, i) | rPE(v, s, d, i+1, end); +// if (s[i] == '^') return rPE(v, s, d, start, i) ^ rPE(v, s, d, i+1, end); +// } - for (int i = start; i < end; i++) { - if (s[i] == '(') { i = skip_parens(s, i); continue; } - if (s[i] == '*') return rPE(v, s, d, start, i) * rPE(v, s, d, i+1, end); - if (s[i] == '/') return rPE(v, s, d, start, i) / rPE(v, s, d, i+1, end); - if (s[i] == '%') return rPE(v, s, d, start, i) % rPE(v, s, d, i+1, end); - if (s[i] == '&') return rPE(v, s, d, start, i) & rPE(v, s, d, i+1, end); - } +// for (int i = start; i < end; i++) { +// if (s[i] == '(') { i = skip_parens(s, i); continue; } +// if (s[i] == '*') return rPE(v, s, d, start, i) * rPE(v, s, d, i+1, end); +// if (s[i] == '/') return rPE(v, s, d, start, i) / rPE(v, s, d, i+1, end); +// if (s[i] == '%') return rPE(v, s, d, start, i) % rPE(v, s, d, i+1, end); +// if (s[i] == '&') return rPE(v, s, d, start, i) & rPE(v, s, d, i+1, end); +// } - // Unary operators - if (s[start] == '-') return -rPE(v, s, d, start+1, end); - if (s[start] == '`') return log2(rPE(v, s, d, start+1, end)); +// // Unary operators +// if (s[start] == '-') return -rPE(v, s, d, start+1, end); +// if (s[start] == '`') return log2(rPE(v, s, d, start+1, end)); - if (isdigit(s[start])) { - unsigned long long u; - sscanf(s.substr(start, end-start).c_str(), "%lli", &u); - return u; - } +// if (isdigit(s[start])) { +// unsigned long long u; +// sscanf(s.substr(start, end-start).c_str(), "%lli", &u); +// return u; +// } - if (s[start] == '(') return rPE(v, s, d, start+1, end-1); +// if (s[start] == '(') return rPE(v, s, d, start+1, end-1); - string label(s.substr(start, end-start)); - map::const_iterator it(d.find(label)); - if (it != d.end()) return it->second; +// string label(s.substr(start, end-start)); +// map::const_iterator it(d.find(label)); +// if (it != d.end()) return it->second; - // If nothing else works, set valid to false. - v = 0; - return 0; -} +// // If nothing else works, set valid to false. +// v = 0; +// return 0; +// } -static uint64_t readParenExpression(bool &valid, const string &s, - const map &d) -{ - valid = true; - return rPE(valid, s, d, 0, s.length()); -} +// static uint64_t readParenExpression(bool &valid, const string &s, +// const map &d) +// { +// valid = true; +// return rPE(valid, s, d, 0, s.length()); +// } -AsmReader::ArgType AsmReader::operandtype_table[][4] = { - {AT_END}, // AC_NONE - {AT_REG, AT_REG, AT_END}, // AC_2REG - {AT_REG, AT_LIT, AT_END}, // AC_2IMM - {AT_REG, AT_REG, AT_REG, AT_END}, // AC_3REG - {AT_PREG, AT_PREG, AT_PREG, AT_END}, // AC_3PREG - {AT_REG, AT_REG, AT_LIT, AT_END}, // AC_3IMM - {AT_REG, AT_REG, AT_REG, AT_END}, // AC_3REGSRC - {AT_LIT, AT_END}, // AC_1IMM - {AT_REG, AT_END}, // AC_1REG - {AT_REG, AT_REG, AT_LIT, AT_END}, // AC_3IMMSRC - {AT_PREG, AT_REG, AT_END}, // AC_PREG_REG - {AT_PREG, AT_PREG, AT_END}, // AC_2PREG - {AT_REG, AT_REG, AT_END} // AC_2REGSRC -}; +// AsmReader::ArgType AsmReader::operandtype_table[][4] = { +// {AT_END}, // AC_NONE +// {AT_REG, AT_REG, AT_END}, // AC_2REG +// {AT_REG, AT_LIT, AT_END}, // AC_2IMM +// {AT_REG, AT_REG, AT_REG, AT_END}, // AC_3REG +// {AT_PREG, AT_PREG, AT_PREG, AT_END}, // AC_3PREG +// {AT_REG, AT_REG, AT_LIT, AT_END}, // AC_3IMM +// {AT_REG, AT_REG, AT_REG, AT_END}, // AC_3REGSRC +// {AT_LIT, AT_END}, // AC_1IMM +// {AT_REG, AT_END}, // AC_1REG +// {AT_REG, AT_REG, AT_LIT, AT_END}, // AC_3IMMSRC +// {AT_PREG, AT_REG, AT_END}, // AC_PREG_REG +// {AT_PREG, AT_PREG, AT_END}, // AC_2PREG +// {AT_REG, AT_REG, AT_END} // AC_2REGSRC +// }; -int lexerFloatBytes; -Obj *AsmReader::read(std::istream &input) { - lexerFloatBytes = wordSize; - FlexLexer *f = new yyFlexLexer(&input); - Obj *o = new Obj(); - std::vector::reverse_iterator cur; - bool permR(true), permW(false), permX(false), entry(false), nextPred(false), - global(false); +// int lexerFloatBytes; +// Obj *AsmReader::read(std::istream &input) { +// lexerFloatBytes = wordSize; +// FlexLexer *f = new yyFlexLexer(&input); +// Obj *o = new Obj(); +// std::vector::reverse_iterator cur; +// bool permR(true), permW(false), permX(false), entry(false), nextPred(false), +// global(false); - map defs; +// map defs; - /* Pre-defined defs. */ - defs["__WORD"] = wordSize; +// /* Pre-defined defs. */ +// defs["__WORD"] = wordSize; - map opMap; +// map opMap; - // Build opMap - for (size_t i = 0; Instruction::instTable[i].opString; i++) - opMap[std::string(Instruction::instTable[i].opString)] - = Instruction::Opcode(i); +// // Build opMap +// for (size_t i = 0; Instruction::instTable[i].opString; i++) +// opMap[std::string(Instruction::instTable[i].opString)] +// = Instruction::Opcode(i); - enum { - ST_INIT, ST_DEF1, ST_DEF2, ST_PERM, ST_WORD1, ST_SPACE, ST_STRING1, - ST_STRING2, ST_BYTE1, ST_BYTE2, ST_ALIGN, ST_INST1, ST_INST2 - } state(ST_INIT); +// enum { +// ST_INIT, ST_DEF1, ST_DEF2, ST_PERM, ST_WORD1, ST_SPACE, ST_STRING1, +// ST_STRING2, ST_BYTE1, ST_BYTE2, ST_ALIGN, ST_INST1, ST_INST2 +// } state(ST_INIT); - enum { OS_NOCHUNK, OS_TEXTCHUNK, OS_DATACHUNK } outstate(OS_NOCHUNK); - TextChunk *tc; - DataChunk *dc; - Instruction *curInst; - string string_arg, next_chunk_name; - bool chunkCreated = true; - Size next_chunk_align(0); - uint64_t num_arg; - RegNum nextPredNum; - Instruction::ArgClass ac; - int argPos; +// enum { OS_NOCHUNK, OS_TEXTCHUNK, OS_DATACHUNK } outstate(OS_NOCHUNK); +// TextChunk *tc; +// DataChunk *dc; +// Instruction *curInst; +// string string_arg, next_chunk_name; +// bool chunkCreated = true; +// Size next_chunk_align(0); +// uint64_t num_arg; +// RegNum nextPredNum; +// Instruction::ArgClass ac; +// int argPos; - AsmTokens t; - while ((t = (AsmTokens)f->yylex()) != 0) { - switch (t) { - case ASM_T_DIR_DEF: - if (state == ST_INIT) state = ST_DEF1; - else { asmReaderError(yyline, "Unexpected .def"); } - break; - case ASM_T_DIR_PERM: - if (state == ST_INIT) { - state = ST_PERM; - permR = permW = permX = false; - if (outstate != OS_NOCHUNK) { - outstate = OS_NOCHUNK; - entry = false; - global = false; - } - } else { asmReaderError(yyline, "Unexpected .perm"); } - break; - case ASM_T_DIR_BYTE: - if (state == ST_INIT) { - state = ST_BYTE1; - } else { asmReaderError(yyline, "Unexpected .byte"); } - break; - case ASM_T_DIR_WORD: - if (state == ST_INIT) { - state = ST_WORD1; - } else { asmReaderError(yyline, "Unexpected .word"); } - break; - case ASM_T_DIR_SPACE: - if (state == ST_INIT) state = ST_SPACE; - else asmReaderError(yyline, "Unexpected .space"); - break; - case ASM_T_DIR_STRING: - if (state == ST_INIT) { - state = ST_STRING1; - } else { asmReaderError(yyline, "Unexpected .string"); } - break; - case ASM_T_DIR_ALIGN: - if (state == ST_INIT) { - state = ST_ALIGN; - } else { asmReaderError(yyline, "Unexpected .align"); } - break; - case ASM_T_DIR_ENTRY: - outstate = OS_NOCHUNK; - entry = true; - break; - case ASM_T_DIR_GLOBAL: - outstate = OS_NOCHUNK; - global = true; - break; - case ASM_T_DIR_ARG_NUM: - switch (state) { - case ST_DEF2: defs[string_arg] = yylval.u; state = ST_INIT; break; - case ST_WORD1: { - if (outstate != OS_DATACHUNK) { - outstate = OS_DATACHUNK; - dc = new DataChunk(next_chunk_name, next_chunk_align? - next_chunk_align:wordSize, - flagsToWord(permR, permW, permX)); - next_chunk_align = 0; - o->chunks.push_back(dc); - if (entry) o->entry = o->chunks.size() - 1; - if (global) dc->setGlobal(); - chunkCreated = true; - } - dc->size += wordSize; - dc->contents.resize(dc->size); - wordToBytes(&*(dc->contents.end()-wordSize), yylval.u, wordSize); - } break; - case ST_SPACE: { - // TODO: the following statement is basically copied from above. Fix - // this. - if (outstate != OS_DATACHUNK) { - outstate = OS_DATACHUNK; - dc = new DataChunk(next_chunk_name, next_chunk_align? - next_chunk_align:wordSize, - flagsToWord(permR, permW, permX)); - next_chunk_align = 0; - o->chunks.push_back(dc); - if (entry) o->entry = o->chunks.size() - 1; - if (global) dc->setGlobal(); - chunkCreated = true; - } - size_t oldSize = dc->size; - dc->size += wordSize * yylval.u; - dc->contents.resize(dc->size); - for (size_t i = oldSize; i < dc->size; ++i) dc->contents[i] = 0; - } break; - case ST_BYTE1: - if (outstate != OS_DATACHUNK) { - // TODO: more of this pasted code - outstate = OS_DATACHUNK; - dc = new DataChunk(next_chunk_name, - next_chunk_align?next_chunk_align:wordSize, - flagsToWord(permR, permW, permX)); - next_chunk_align = 0; - o->chunks.push_back(dc); - if (entry) o->entry = o->chunks.size() - 1; - if (global) dc->setGlobal(); - chunkCreated = true; - } - dc->size++; - dc->contents.resize(dc->size); - *(dc->contents.end() - 1) = yylval.u; - state = ST_INIT; - break; - case ST_ALIGN: - next_chunk_align = yylval.u; - if (outstate != OS_NOCHUNK) { - outstate = OS_NOCHUNK; - entry = false; - global = false; - } - state = ST_INIT; - break; - default: asmReaderError(yyline, "Unexpected literal argument"); - } - break; - case ASM_T_DIR_ARG_STRING: - if (state == ST_STRING1) { - if (outstate != OS_DATACHUNK) { - // TODO: pasted code (see above) - outstate = OS_DATACHUNK; - dc = new DataChunk(next_chunk_name, - next_chunk_align?next_chunk_align:wordSize, - flagsToWord(permR, permW, permX)); - next_chunk_align = 0; - o->chunks.push_back(dc); - if (entry) o->entry = o->chunks.size() - 1; - if (global) dc->setGlobal(); - chunkCreated = true; - } - const char *s = yylval.s.c_str(); - do { - if (*s == '\\') { - switch (*(++s)) { - case 'n': dc->contents.push_back('\n'); break; - case '"': dc->contents.push_back(*s); break; - default: dc->contents.push_back(*s); break; - } - } else { - dc->contents.push_back(*s); - } - dc->size++; - } while(*(s++)); - } else { - asmReaderError(yyline, "Unexpected string literal."); - } - state = ST_INIT; - break; - case ASM_T_DIR_ARG_SYM: - switch (state) { - case ST_DEF1: string_arg = yylval.s; state = ST_DEF2; break; - default: asmReaderError(yyline, ""); - }; - break; - case ASM_T_DIR_ARG_R: - permR = true; - break; - case ASM_T_DIR_ARG_W: - permW = true; - break; - case ASM_T_DIR_ARG_X: - permX = true; - break; - case ASM_T_DIR_END: - if (state == ST_INST1 || state == ST_INST2) { - if (outstate == OS_TEXTCHUNK) { - tc->instructions.push_back(curInst); - } else { - asmReaderError(yyline, "Inst not in text chunk(internal error)"); - } - } - state = ST_INIT; - break; - case ASM_T_LABEL: - if (!chunkCreated) { - // We have an empty label; create an empty chunk. - dc = new DataChunk(next_chunk_name, 0, - flagsToWord(permR, permW, permX)); - next_chunk_align = 0; - o->chunks.push_back(dc); - if (entry) o->entry = o->chunks.size() - 1; - if (global) dc->setGlobal(); - } +// AsmTokens t; +// while ((t = (AsmTokens)f->yylex()) != 0) { +// switch (t) { +// case ASM_T_DIR_DEF: +// if (state == ST_INIT) state = ST_DEF1; +// else { asmReaderError(yyline, "Unexpected .def"); } +// break; +// case ASM_T_DIR_PERM: +// if (state == ST_INIT) { +// state = ST_PERM; +// permR = permW = permX = false; +// if (outstate != OS_NOCHUNK) { +// outstate = OS_NOCHUNK; +// entry = false; +// global = false; +// } +// } else { asmReaderError(yyline, "Unexpected .perm"); } +// break; +// case ASM_T_DIR_BYTE: +// if (state == ST_INIT) { +// state = ST_BYTE1; +// } else { asmReaderError(yyline, "Unexpected .byte"); } +// break; +// case ASM_T_DIR_WORD: +// if (state == ST_INIT) { +// state = ST_WORD1; +// } else { asmReaderError(yyline, "Unexpected .word"); } +// break; +// case ASM_T_DIR_SPACE: +// if (state == ST_INIT) state = ST_SPACE; +// else asmReaderError(yyline, "Unexpected .space"); +// break; +// case ASM_T_DIR_STRING: +// if (state == ST_INIT) { +// state = ST_STRING1; +// } else { asmReaderError(yyline, "Unexpected .string"); } +// break; +// case ASM_T_DIR_ALIGN: +// if (state == ST_INIT) { +// state = ST_ALIGN; +// } else { asmReaderError(yyline, "Unexpected .align"); } +// break; +// case ASM_T_DIR_ENTRY: +// outstate = OS_NOCHUNK; +// entry = true; +// break; +// case ASM_T_DIR_GLOBAL: +// outstate = OS_NOCHUNK; +// global = true; +// break; +// case ASM_T_DIR_ARG_NUM: +// switch (state) { +// case ST_DEF2: defs[string_arg] = yylval.u; state = ST_INIT; break; +// case ST_WORD1: { +// if (outstate != OS_DATACHUNK) { +// outstate = OS_DATACHUNK; +// dc = new DataChunk(next_chunk_name, next_chunk_align? +// next_chunk_align:wordSize, +// flagsToWord(permR, permW, permX)); +// next_chunk_align = 0; +// o->chunks.push_back(dc); +// if (entry) o->entry = o->chunks.size() - 1; +// if (global) dc->setGlobal(); +// chunkCreated = true; +// } +// dc->size += wordSize; +// dc->contents.resize(dc->size); +// wordToBytes(&*(dc->contents.end()-wordSize), yylval.u, wordSize); +// } break; +// case ST_SPACE: { +// // TODO: the following statement is basically copied from above. Fix +// // this. +// if (outstate != OS_DATACHUNK) { +// outstate = OS_DATACHUNK; +// dc = new DataChunk(next_chunk_name, next_chunk_align? +// next_chunk_align:wordSize, +// flagsToWord(permR, permW, permX)); +// next_chunk_align = 0; +// o->chunks.push_back(dc); +// if (entry) o->entry = o->chunks.size() - 1; +// if (global) dc->setGlobal(); +// chunkCreated = true; +// } +// size_t oldSize = dc->size; +// dc->size += wordSize * yylval.u; +// dc->contents.resize(dc->size); +// for (size_t i = oldSize; i < dc->size; ++i) dc->contents[i] = 0; +// } break; +// case ST_BYTE1: +// if (outstate != OS_DATACHUNK) { +// // TODO: more of this pasted code +// outstate = OS_DATACHUNK; +// dc = new DataChunk(next_chunk_name, +// next_chunk_align?next_chunk_align:wordSize, +// flagsToWord(permR, permW, permX)); +// next_chunk_align = 0; +// o->chunks.push_back(dc); +// if (entry) o->entry = o->chunks.size() - 1; +// if (global) dc->setGlobal(); +// chunkCreated = true; +// } +// dc->size++; +// dc->contents.resize(dc->size); +// *(dc->contents.end() - 1) = yylval.u; +// state = ST_INIT; +// break; +// case ST_ALIGN: +// next_chunk_align = yylval.u; +// if (outstate != OS_NOCHUNK) { +// outstate = OS_NOCHUNK; +// entry = false; +// global = false; +// } +// state = ST_INIT; +// break; +// default: asmReaderError(yyline, "Unexpected literal argument"); +// } +// break; +// case ASM_T_DIR_ARG_STRING: +// if (state == ST_STRING1) { +// if (outstate != OS_DATACHUNK) { +// // TODO: pasted code (see above) +// outstate = OS_DATACHUNK; +// dc = new DataChunk(next_chunk_name, +// next_chunk_align?next_chunk_align:wordSize, +// flagsToWord(permR, permW, permX)); +// next_chunk_align = 0; +// o->chunks.push_back(dc); +// if (entry) o->entry = o->chunks.size() - 1; +// if (global) dc->setGlobal(); +// chunkCreated = true; +// } +// const char *s = yylval.s.c_str(); +// do { +// if (*s == '\\') { +// switch (*(++s)) { +// case 'n': dc->contents.push_back('\n'); break; +// case '"': dc->contents.push_back(*s); break; +// default: dc->contents.push_back(*s); break; +// } +// } else { +// dc->contents.push_back(*s); +// } +// dc->size++; +// } while(*(s++)); +// } else { +// asmReaderError(yyline, "Unexpected string literal."); +// } +// state = ST_INIT; +// break; +// case ASM_T_DIR_ARG_SYM: +// switch (state) { +// case ST_DEF1: string_arg = yylval.s; state = ST_DEF2; break; +// default: asmReaderError(yyline, ""); +// }; +// break; +// case ASM_T_DIR_ARG_R: +// permR = true; +// break; +// case ASM_T_DIR_ARG_W: +// permW = true; +// break; +// case ASM_T_DIR_ARG_X: +// permX = true; +// break; +// case ASM_T_DIR_END: +// if (state == ST_INST1 || state == ST_INST2) { +// if (outstate == OS_TEXTCHUNK) { +// tc->instructions.push_back(curInst); +// } else { +// asmReaderError(yyline, "Inst not in text chunk(internal error)"); +// } +// } +// state = ST_INIT; +// break; +// case ASM_T_LABEL: +// if (!chunkCreated) { +// // We have an empty label; create an empty chunk. +// dc = new DataChunk(next_chunk_name, 0, +// flagsToWord(permR, permW, permX)); +// next_chunk_align = 0; +// o->chunks.push_back(dc); +// if (entry) o->entry = o->chunks.size() - 1; +// if (global) dc->setGlobal(); +// } - if (outstate != OS_NOCHUNK) { - entry = false; - global = false; - outstate = OS_NOCHUNK; - } - next_chunk_name = yylval.s; - chunkCreated = false; - break; - case ASM_T_PRED: - nextPred = true; - nextPredNum = yylval.u; - break; - case ASM_T_INST: - if (state == ST_INIT) { - map::iterator - opcIterator = opMap.find(yylval.s); +// if (outstate != OS_NOCHUNK) { +// entry = false; +// global = false; +// outstate = OS_NOCHUNK; +// } +// next_chunk_name = yylval.s; +// chunkCreated = false; +// break; +// case ASM_T_PRED: +// nextPred = true; +// nextPredNum = yylval.u; +// break; +// case ASM_T_INST: +// if (state == ST_INIT) { +// map::iterator +// opcIterator = opMap.find(yylval.s); - if (opcIterator == opMap.end()) - asmReaderError(yyline, "Invalid Instruction"); - Instruction::Opcode opc = opcIterator->second; - if (outstate != OS_TEXTCHUNK) { - tc = new TextChunk(next_chunk_name, next_chunk_align, - flagsToWord(permR, permW, permX)); - next_chunk_align = 0; - o->chunks.push_back(tc); - if (entry) o->entry = o->chunks.size() - 1; - if (global) tc->setGlobal(); - outstate = OS_TEXTCHUNK; - chunkCreated = true; - } - curInst = new Instruction(); - curInst->setOpcode(opc); - ac = Instruction::instTable[opc].argClass; - argPos = 0; - if (nextPred) { - nextPred = false; - curInst->setPred(nextPredNum); - } - state = Instruction::instTable[opc].allSrcArgs?ST_INST2:ST_INST1; - } else { asmReaderError(yyline, "Unexpected instruction"); } - break; - case ASM_T_PREG: - if (operandtype_table[ac][argPos++] != AT_PREG) - asmReaderError(yyline, "Unexpected predicate register"); - switch (state) { - case ST_INST1: curInst->setDestPReg(yylval.u); - state = ST_INST2; - break; - case ST_INST2: curInst->setSrcPReg(yylval.u); - break; - default: asmReaderError(yyline, "Unexpected predicate register"); - } - break; +// if (opcIterator == opMap.end()) +// asmReaderError(yyline, "Invalid Instruction"); +// Instruction::Opcode opc = opcIterator->second; +// if (outstate != OS_TEXTCHUNK) { +// tc = new TextChunk(next_chunk_name, next_chunk_align, +// flagsToWord(permR, permW, permX)); +// next_chunk_align = 0; +// o->chunks.push_back(tc); +// if (entry) o->entry = o->chunks.size() - 1; +// if (global) tc->setGlobal(); +// outstate = OS_TEXTCHUNK; +// chunkCreated = true; +// } +// curInst = new Instruction(); +// curInst->setOpcode(opc); +// ac = Instruction::instTable[opc].argClass; +// argPos = 0; +// if (nextPred) { +// nextPred = false; +// curInst->setPred(nextPredNum); +// } +// state = Instruction::instTable[opc].allSrcArgs?ST_INST2:ST_INST1; +// } else { asmReaderError(yyline, "Unexpected instruction"); } +// break; +// case ASM_T_PREG: +// if (operandtype_table[ac][argPos++] != AT_PREG) +// asmReaderError(yyline, "Unexpected predicate register"); +// switch (state) { +// case ST_INST1: curInst->setDestPReg(yylval.u); +// state = ST_INST2; +// break; +// case ST_INST2: curInst->setSrcPReg(yylval.u); +// break; +// default: asmReaderError(yyline, "Unexpected predicate register"); +// } +// break; - case ASM_T_REG_RA: - yylval.u = nRegs - 1; - goto continue_reg; +// case ASM_T_REG_RA: +// yylval.u = nRegs - 1; +// goto continue_reg; - case ASM_T_REG_FP: - yylval.u = nRegs - 3; - goto continue_reg; +// case ASM_T_REG_FP: +// yylval.u = nRegs - 3; +// goto continue_reg; - case ASM_T_REG_SP: - yylval.u = nRegs - 2; - goto continue_reg; +// case ASM_T_REG_SP: +// yylval.u = nRegs - 2; +// goto continue_reg; - case ASM_T_REG: - continue_reg: - if (operandtype_table[ac][argPos++] != AT_REG) - asmReaderError(yyline, "Unexpected register operand."); - switch (state) { - case ST_INST1: curInst->setDestReg(yylval.u); - state = ST_INST2; - break; - case ST_INST2: curInst->setSrcReg(yylval.u); - break; - default: asmReaderError(yyline, "Unexpected register"); - } - break; - case ASM_T_PEXP: { - // Decode the paren expression. - bool valid; - yylval.u = readParenExpression(valid, yylval.s, defs); - if (!valid) asmReaderError(yyline, "Invalid paren expression"); - } - case ASM_T_LIT: - if (operandtype_table[ac][argPos++] != AT_LIT) - asmReaderError(yyline, "Unexpected literal operand."); - switch (state) { - case ST_INST1: asmReaderError(yyline, "Unexpected literal"); - case ST_INST2: curInst->setSrcImm(yylval.u); - break; - default: asmReaderError(yyline, "Unexpected literal"); - } - break; - case ASM_T_SYM: - if (operandtype_table[ac][argPos++] != AT_LIT) - asmReaderError(yyline, "Unexpected symbol operand."); - switch (state) { - case ST_INST1: asmReaderError(yyline, "Unexpected symbol"); - case ST_INST2: if (defs.find(yylval.s) != defs.end()) { - curInst->setSrcImm(defs[yylval.s]); - } else { - Ref *r = new - SimpleRef(yylval.s, *curInst->setSrcImm(), - curInst->hasRelImm()); - tc->refs.push_back(r); - curInst->setImmRef(*r); - } - break; - default: asmReaderError(yyline, "Unexpected symbol"); - } - break; - default: asmReaderError(yyline, "Invalid state(internal error)"); - }; - } +// case ASM_T_REG: +// continue_reg: +// if (operandtype_table[ac][argPos++] != AT_REG) +// asmReaderError(yyline, "Unexpected register operand."); +// switch (state) { +// case ST_INST1: curInst->setDestReg(yylval.u); +// state = ST_INST2; +// break; +// case ST_INST2: curInst->setSrcReg(yylval.u); +// break; +// default: asmReaderError(yyline, "Unexpected register"); +// } +// break; +// case ASM_T_PEXP: { +// // Decode the paren expression. +// bool valid; +// yylval.u = readParenExpression(valid, yylval.s, defs); +// if (!valid) asmReaderError(yyline, "Invalid paren expression"); +// } +// case ASM_T_LIT: +// if (operandtype_table[ac][argPos++] != AT_LIT) +// asmReaderError(yyline, "Unexpected literal operand."); +// switch (state) { +// case ST_INST1: asmReaderError(yyline, "Unexpected literal"); +// case ST_INST2: curInst->setSrcImm(yylval.u); +// break; +// default: asmReaderError(yyline, "Unexpected literal"); +// } +// break; +// case ASM_T_SYM: +// if (operandtype_table[ac][argPos++] != AT_LIT) +// asmReaderError(yyline, "Unexpected symbol operand."); +// switch (state) { +// case ST_INST1: asmReaderError(yyline, "Unexpected symbol"); +// case ST_INST2: if (defs.find(yylval.s) != defs.end()) { +// curInst->setSrcImm(defs[yylval.s]); +// } else { +// Ref *r = new +// SimpleRef(yylval.s, *curInst->setSrcImm(), +// curInst->hasRelImm()); +// tc->refs.push_back(r); +// curInst->setImmRef(*r); +// } +// break; +// default: asmReaderError(yyline, "Unexpected symbol"); +// } +// break; +// default: asmReaderError(yyline, "Invalid state(internal error)"); +// }; +// } - if (!chunkCreated) { - // We have an empty label; create an empty chunk. - dc = new DataChunk(next_chunk_name, 0, - flagsToWord(permR, permW, permX)); - next_chunk_align = 0; - o->chunks.push_back(dc); - if (entry) o->entry = o->chunks.size() - 1; - if (global) dc->setGlobal(); - } +// if (!chunkCreated) { +// // We have an empty label; create an empty chunk. +// dc = new DataChunk(next_chunk_name, 0, +// flagsToWord(permR, permW, permX)); +// next_chunk_align = 0; +// o->chunks.push_back(dc); +// if (entry) o->entry = o->chunks.size() - 1; +// if (global) dc->setGlobal(); +// } - return o; -} +// return o; +// } -void AsmWriter::write(std::ostream &output, const Obj &obj) { - Word prevFlags(0); +// void AsmWriter::write(std::ostream &output, const Obj &obj) { +// Word prevFlags(0); - for (size_t j = 0; j < obj.chunks.size(); j++) { - Chunk * const &c = obj.chunks[j]; +// for (size_t j = 0; j < obj.chunks.size(); j++) { +// Chunk * const &c = obj.chunks[j]; - /* Write out the flags. */ - if (c->flags != prevFlags) { - bool r, w, x; - wordToFlags(r, w, x, c->flags); - output << ".perm "; - if (r) output << 'r'; - if (w) output << 'w'; - if (x) output << 'x'; - output << '\n'; - prevFlags = c->flags; - } +// /* Write out the flags. */ +// if (c->flags != prevFlags) { +// bool r, w, x; +// wordToFlags(r, w, x, c->flags); +// output << ".perm "; +// if (r) output << 'r'; +// if (w) output << 'w'; +// if (x) output << 'x'; +// output << '\n'; +// prevFlags = c->flags; +// } - /* Write align if set. */ - if (c->alignment) output << ".align 0x" << hex << c->alignment << '\n'; +// /* Write align if set. */ +// if (c->alignment) output << ".align 0x" << hex << c->alignment << '\n'; - TextChunk * const tc = dynamic_cast(c); - DataChunk * const dc = dynamic_cast(c); +// TextChunk * const tc = dynamic_cast(c); +// DataChunk * const dc = dynamic_cast(c); - if (tc) { - if (j == obj.entry) output << "\n.entry\n"; - if (c->isGlobal()) output << "\n.global\n"; - if (tc->name != "") output << tc->name << ':'; +// if (tc) { +// if (j == obj.entry) output << "\n.entry\n"; +// if (c->isGlobal()) output << "\n.global\n"; +// if (tc->name != "") output << tc->name << ':'; - for (size_t i = 0; i < tc->instructions.size(); i++) { - output << "\t" << *(tc->instructions[i]) << '\n'; - } - } else if (dc) { - if (c->name != "") output << c->name << ':' << endl; +// for (size_t i = 0; i < tc->instructions.size(); i++) { +// output << "\t" << *(tc->instructions[i]) << '\n'; +// } +// } else if (dc) { +// if (c->name != "") output << c->name << ':' << endl; - Size i; - for (i = 0; i < dc->contents.size();) { - Size tmpWordSize = (dc->contents.size() - i < wordSize) ? - dc->contents.size() - i : wordSize; +// Size i; +// for (i = 0; i < dc->contents.size();) { +// Size tmpWordSize = (dc->contents.size() - i < wordSize) ? +// dc->contents.size() - i : wordSize; - i += tmpWordSize; - Word w = 0; - for (size_t j = 0; j < tmpWordSize; j++) { - w <<= 8; - w |= dc->contents[i - j - 1]; - } +// i += tmpWordSize; +// Word w = 0; +// for (size_t j = 0; j < tmpWordSize; j++) { +// w <<= 8; +// w |= dc->contents[i - j - 1]; +// } - if (i == tmpWordSize && c->name != "") - output << " .word " << " 0x" << hex << w << endl; - else - output << " .word " << " 0x" << hex << w << endl; - } +// if (i == tmpWordSize && c->name != "") +// output << " .word " << " 0x" << hex << w << endl; +// else +// output << " .word " << " 0x" << hex << w << endl; +// } - if (i % wordSize) i += (wordSize - (i%wordSize)); +// if (i % wordSize) i += (wordSize - (i%wordSize)); - if (dc->size > i) { - Size fillSize = (dc->size - i)/wordSize; - output << ".word 0x" << hex << fillSize << '\n'; - } - } else { - cout << "Unrecognized chunk type in AsmWriter.\n"; - exit(1); - } - } -} +// if (dc->size > i) { +// Size fillSize = (dc->size - i)/wordSize; +// output << ".word 0x" << hex << fillSize << '\n'; +// } +// } else { +// cout << "Unrecognized chunk type in AsmWriter.\n"; +// exit(1); +// } +// } +// } -enum HOFFlag { HOF_GLOBAL = 1 }; +// enum HOFFlag { HOF_GLOBAL = 1 }; -Word getHofFlags(Chunk &c) { - Word w = 0; - if (c.isGlobal()) w |= HOF_GLOBAL; - return w; -} +// Word getHofFlags(Chunk &c) { +// Word w = 0; +// if (c.isGlobal()) w |= HOF_GLOBAL; +// return w; +// } -static void outputWord(std::ostream &out, Word w, - vector &tmp, Size wordSize) -{ - Size n(0); - writeWord(tmp, n, wordSize, w); - out.write((char*)&tmp[0], wordSize); -} +// static void outputWord(std::ostream &out, Word w, +// vector &tmp, Size wordSize) +// { +// Size n(0); +// writeWord(tmp, n, wordSize, w); +// out.write((char*)&tmp[0], wordSize); +// } -void HOFWriter::write(std::ostream &output, const Obj &obj) { - string archString(arch); - Size wordSize(arch.getWordSize()), n, offsetVectorPos; +// void HOFWriter::write(std::ostream &output, const Obj &obj) { +// string archString(arch); +// Size wordSize(arch.getWordSize()), n, offsetVectorPos; - vector tmp; - vector offsets(obj.chunks.size()); +// vector tmp; +// vector offsets(obj.chunks.size()); - /* Magic number, arch string, and padding. */ - output.write("HARP", 4); - output.write(archString.c_str(), archString.length()+1); - Size padBytes = (wordSize-(4+archString.length()+1)%wordSize)%wordSize; - for (Size i = 0; i < padBytes; i++) output.put(0); +// /* Magic number, arch string, and padding. */ +// output.write("HARP", 4); +// output.write(archString.c_str(), archString.length()+1); +// Size padBytes = (wordSize-(4+archString.length()+1)%wordSize)%wordSize; +// for (Size i = 0; i < padBytes; i++) output.put(0); - /* Write out the entry chunk index. */ - outputWord(output, obj.entry, tmp, wordSize); +// /* Write out the entry chunk index. */ +// outputWord(output, obj.entry, tmp, wordSize); - /* Write out the number of chunks. */ - outputWord(output, obj.chunks.size(), tmp, wordSize); +// /* Write out the number of chunks. */ +// outputWord(output, obj.chunks.size(), tmp, wordSize); - /* Skip the chunk size offset vector. */ - offsetVectorPos = output.tellp(); - output.seekp(output.tellp() + streampos(wordSize * obj.chunks.size())); +// /* Skip the chunk size offset vector. */ +// offsetVectorPos = output.tellp(); +// output.seekp(output.tellp() + streampos(wordSize * obj.chunks.size())); - /* Write out the chunks, keeping track of their offsets. */ - for (Size i = 0; i < obj.chunks.size(); i++) { - offsets[i] = output.tellp(); +// /* Write out the chunks, keeping track of their offsets. */ +// for (Size i = 0; i < obj.chunks.size(); i++) { +// offsets[i] = output.tellp(); - // Is it a data chunk? - DataChunk *dc = dynamic_cast(obj.chunks[i]); - if (!dc) { cout << "HOFWriter::write(): invalid chunk type.\n"; exit(1); } +// // Is it a data chunk? +// DataChunk *dc = dynamic_cast(obj.chunks[i]); +// if (!dc) { cout << "HOFWriter::write(): invalid chunk type.\n"; exit(1); } - D(1, "Writing chunk \"" << dc->name << "\", size=" << dc->contents.size()); +// D(1, "Writing chunk \"" << dc->name << "\", size=" << dc->contents.size()); - // Chunk name - output.write(dc->name.c_str(), dc->name.length() + 1); +// // Chunk name +// output.write(dc->name.c_str(), dc->name.length() + 1); - /* Padding */ - padBytes = (wordSize - (dc->name.length()+1)%wordSize)%wordSize; - for (Size i = 0; i < padBytes; i++) output.put(0); +// /* Padding */ +// padBytes = (wordSize - (dc->name.length()+1)%wordSize)%wordSize; +// for (Size i = 0; i < padBytes; i++) output.put(0); - /* Chunk alignment, flags, address, size (in RAM and disk) */ - outputWord(output, dc->alignment, tmp, wordSize); - outputWord(output, dc->flags, tmp, wordSize); - outputWord(output, getHofFlags(*dc), tmp, wordSize); - outputWord(output, dc->bound?dc->address:0, tmp, wordSize); - outputWord(output, dc->size, tmp, wordSize); - outputWord(output, dc->contents.size(), tmp, wordSize); +// /* Chunk alignment, flags, address, size (in RAM and disk) */ +// outputWord(output, dc->alignment, tmp, wordSize); +// outputWord(output, dc->flags, tmp, wordSize); +// outputWord(output, getHofFlags(*dc), tmp, wordSize); +// outputWord(output, dc->bound?dc->address:0, tmp, wordSize); +// outputWord(output, dc->size, tmp, wordSize); +// outputWord(output, dc->contents.size(), tmp, wordSize); - /* References */ - outputWord(output, dc->refs.size(), tmp, wordSize); - for (Size j = 0; j < dc->refs.size(); j++) { - OffsetRef *r = dynamic_cast(dc->refs[j]); - if (!r) { cout << "HOFWriter::write(): invalid ref type.\n"; exit(1); } - /* Reference name */ - output.write(r->name.c_str(), r->name.length() + 1); - /* Padding */ - padBytes = (wordSize - (r->name.length() + 1)%wordSize)%wordSize; - for (Size i = 0; i < padBytes; i++) output.put(0); - /* Compute flags word. */ - Word rFlags(0); - if (r->relative) rFlags |= 1; - /* Output flags word. */ - outputWord(output, rFlags, tmp, wordSize); - /* Offset from which relative branches are computed. */ - outputWord(output, r->ibase, tmp, wordSize); - /* Reference offset in block. */ - outputWord(output, r->getOffset(), tmp, wordSize); - /* Reference size in bits. */ - outputWord(output, r->getBits(), tmp, wordSize); - } +// /* References */ +// outputWord(output, dc->refs.size(), tmp, wordSize); +// for (Size j = 0; j < dc->refs.size(); j++) { +// OffsetRef *r = dynamic_cast(dc->refs[j]); +// if (!r) { cout << "HOFWriter::write(): invalid ref type.\n"; exit(1); } +// /* Reference name */ +// output.write(r->name.c_str(), r->name.length() + 1); +// /* Padding */ +// padBytes = (wordSize - (r->name.length() + 1)%wordSize)%wordSize; +// for (Size i = 0; i < padBytes; i++) output.put(0); +// /* Compute flags word. */ +// Word rFlags(0); +// if (r->relative) rFlags |= 1; +// /* Output flags word. */ +// outputWord(output, rFlags, tmp, wordSize); +// /* Offset from which relative branches are computed. */ +// outputWord(output, r->ibase, tmp, wordSize); +// /* Reference offset in block. */ +// outputWord(output, r->getOffset(), tmp, wordSize); +// /* Reference size in bits. */ +// outputWord(output, r->getBits(), tmp, wordSize); +// } - /* Chunk data. */ - output.write((char*)&(dc->contents[0]), dc->contents.size()); +// /* Chunk data. */ +// output.write((char*)&(dc->contents[0]), dc->contents.size()); - /* Chunk padding. */ - padBytes = (wordSize - dc->contents.size()%wordSize)%wordSize; - for (Size i = 0; i < padBytes; i++) output.put(0); - } +// /* Chunk padding. */ +// padBytes = (wordSize - dc->contents.size()%wordSize)%wordSize; +// for (Size i = 0; i < padBytes; i++) output.put(0); +// } - /* Write out the chunk offset vector. */ - output.seekp(offsetVectorPos); - for (Size i = 0; i < obj.chunks.size(); i++) { - outputWord(output, offsets[i], tmp, wordSize); - } -} +// /* Write out the chunk offset vector. */ +// output.seekp(offsetVectorPos); +// for (Size i = 0; i < obj.chunks.size(); i++) { +// outputWord(output, offsets[i], tmp, wordSize); +// } +// } -static Word inputWord(std::istream &input, Size wordSize, vector &tmp) { - Size n(0), pos(input.tellg()); - if (tmp.size() < wordSize) tmp.resize(wordSize); +// static Word inputWord(std::istream &input, Size wordSize, vector &tmp) { +// Size n(0), pos(input.tellg()); +// if (tmp.size() < wordSize) tmp.resize(wordSize); - /* Seek to the next word-aligned place. */ - if (input.tellg()%wordSize) { - input.seekg(input.tellg() + - streampos((wordSize - input.tellg()%wordSize)%wordSize)); - } +// /* Seek to the next word-aligned place. */ +// if (input.tellg()%wordSize) { +// input.seekg(input.tellg() + +// streampos((wordSize - input.tellg()%wordSize)%wordSize)); +// } - input.read((char*)&tmp[0], wordSize); +// input.read((char*)&tmp[0], wordSize); - return readWord(tmp, n, wordSize); -} +// return readWord(tmp, n, wordSize); +// } -static string inputString(std::istream &input) { - string s; - char c; +// static string inputString(std::istream &input) { +// string s; +// char c; - while (input && (c = input.get()) != '\0') s += c; +// while (input && (c = input.get()) != '\0') s += c; - return s; -} +// return s; +// } -Obj *HOFReader::read(std::istream &input) { - Size wordSize(arch.getWordSize()); - Obj *o = new Obj(); +// Obj *HOFReader::read(std::istream &input) { +// Size wordSize(arch.getWordSize()); +// Obj *o = new Obj(); - vector tmp(4); +// vector tmp(4); - input.read((char*)&tmp[0], 4); - if (tmp[0] != 'H' || tmp[1] != 'A' || tmp[2] != 'R' || tmp[3] != 'P') { - cout << "Bad magic number in HOFReader::read().\n"; - exit(1); - } +// input.read((char*)&tmp[0], 4); +// if (tmp[0] != 'H' || tmp[1] != 'A' || tmp[2] != 'R' || tmp[3] != 'P') { +// cout << "Bad magic number in HOFReader::read().\n"; +// exit(1); +// } - string archString(inputString(input)); - ArchDef fileArch(archString); - if (fileArch != arch) { - cout << "File arch " << archString << " does not match reader arch " - << string(arch) << " in HOFReader::read().\n"; - exit(1); - } +// string archString(inputString(input)); +// ArchDef fileArch(archString); +// if (fileArch != arch) { +// cout << "File arch " << archString << " does not match reader arch " +// << string(arch) << " in HOFReader::read().\n"; +// exit(1); +// } - o->entry = inputWord(input, wordSize, tmp); +// o->entry = inputWord(input, wordSize, tmp); - Size nChunks(inputWord(input, wordSize, tmp)); +// Size nChunks(inputWord(input, wordSize, tmp)); - vector chunkOffsets(nChunks); +// vector chunkOffsets(nChunks); - /* Read in the chunk offsets. */ - for (Size i = 0; i < nChunks; i++) { - chunkOffsets[i] = inputWord(input, wordSize, tmp); - } +// /* Read in the chunk offsets. */ +// for (Size i = 0; i < nChunks; i++) { +// chunkOffsets[i] = inputWord(input, wordSize, tmp); +// } - /* Read in the chunks. */ - o->chunks.resize(nChunks); - for (Size i = 0; i < nChunks; i++) { - input.seekg(chunkOffsets[i]); - string name(inputString(input)); - Word alignment(inputWord(input, wordSize, tmp)), - flags(inputWord(input, wordSize, tmp)), - hofFlags(inputWord(input, wordSize, tmp)), - address(inputWord(input, wordSize, tmp)), - size(inputWord(input, wordSize, tmp)), - dSize(inputWord(input, wordSize, tmp)), - nRefs(inputWord(input, wordSize, tmp)); - DataChunk *dc = new DataChunk(name, alignment, flags); - if (hofFlags & HOF_GLOBAL) dc->setGlobal(); - dc->address = address; - dc->bound = address?true:false; - dc->contents.resize(dSize); +// /* Read in the chunks. */ +// o->chunks.resize(nChunks); +// for (Size i = 0; i < nChunks; i++) { +// input.seekg(chunkOffsets[i]); +// string name(inputString(input)); +// Word alignment(inputWord(input, wordSize, tmp)), +// flags(inputWord(input, wordSize, tmp)), +// hofFlags(inputWord(input, wordSize, tmp)), +// address(inputWord(input, wordSize, tmp)), +// size(inputWord(input, wordSize, tmp)), +// dSize(inputWord(input, wordSize, tmp)), +// nRefs(inputWord(input, wordSize, tmp)); +// DataChunk *dc = new DataChunk(name, alignment, flags); +// if (hofFlags & HOF_GLOBAL) dc->setGlobal(); +// dc->address = address; +// dc->bound = address?true:false; +// dc->contents.resize(dSize); - /* Get the refs. */ - for (Size j = 0; j < nRefs; j++) { - string rName(inputString(input)); - Word rFlags(inputWord(input, wordSize, tmp)), - ibase(inputWord(input, wordSize, tmp)), - offset(inputWord(input, wordSize, tmp)), - bits(inputWord(input, wordSize, tmp)); - OffsetRef *r = - new OffsetRef(rName, dc->contents, offset, bits, wordSize, rFlags&1, - ibase); - dc->refs.push_back(r); - } +// /* Get the refs. */ +// for (Size j = 0; j < nRefs; j++) { +// string rName(inputString(input)); +// Word rFlags(inputWord(input, wordSize, tmp)), +// ibase(inputWord(input, wordSize, tmp)), +// offset(inputWord(input, wordSize, tmp)), +// bits(inputWord(input, wordSize, tmp)); +// OffsetRef *r = +// new OffsetRef(rName, dc->contents, offset, bits, wordSize, rFlags&1, +// ibase); +// dc->refs.push_back(r); +// } - D(1, "Reading chunk \"" << name << "\", size " << dSize); +// D(1, "Reading chunk \"" << name << "\", size " << dSize); - /* Get the contents. */ - input.read((char*)&dc->contents[0], dSize); - dc->size = size; +// /* Get the contents. */ +// input.read((char*)&dc->contents[0], dSize); +// dc->size = size; - o->chunks[i] = dc; - } +// o->chunks[i] = dc; +// } - return o; -} +// return o; +// }