diff --git a/ts2panda/src/base/literal.ts b/ts2panda/src/base/literal.ts index c98f498528c47dc2c52b40c681fece3044b22751..f4483a1d37bf95a7d4fc5afff689c1606025a7c7 100755 --- a/ts2panda/src/base/literal.ts +++ b/ts2panda/src/base/literal.ts @@ -26,44 +26,44 @@ export enum LiteralTag { } export class Literal { - private tag: LiteralTag; - private value: any; + private t: LiteralTag; + private v: any; - constructor(tag: LiteralTag, value: any) { - this.tag = tag; - this.value = value; + constructor(t: LiteralTag, v: any) { + this.t = t; + this.v = v; } getTag() { - return this.tag; + return this.t; } getValue() { - return this.value; + return this.v; } } export class LiteralBuffer { - private literalBuffer: Literal[] = []; + private lb: Literal[] = []; constructor() { }; addLiterals(...literals: Array) { - this.literalBuffer.push(...literals); + this.lb.push(...literals); } getLiterals() { - return this.literalBuffer; + return this.lb; } isEmpty() { - return this.literalBuffer.length == 0; + return this.lb.length == 0; } getLiteral(index: number) { - if (index >= this.literalBuffer.length || this.literalBuffer.length <=0) { + if (index >= this.lb.length || this.lb.length <=0) { return ; } - return this.literalBuffer[index]; + return this.lb[index]; } } \ No newline at end of file diff --git a/ts2panda/src/debuginfo.ts b/ts2panda/src/debuginfo.ts index 5f9ab13dd2036caf8a620e76c6022499d2e8d64d..9ec81d26181de36bf3329008f91f30bd1d5c7c2b 100644 --- a/ts2panda/src/debuginfo.ts +++ b/ts2panda/src/debuginfo.ts @@ -31,11 +31,11 @@ import { } from "./variable"; export class DebugPosInfo { - private boundLeft: number | undefined = 0; - private boundRight: number | undefined = 0; - private lineNum: number = -1; - private columnNum: number = -1; - private wholeLine: string | undefined = ""; + private bl: number | undefined = 0; // bound left + private br: number | undefined = 0; // bound right + private l: number = -1; // line number + private c: number = -1; // column number + private w: string | undefined = ""; // whole line private nodeKind: NodeKind | undefined = NodeKind.FirstNodeOfFunction; constructor() { } @@ -53,80 +53,80 @@ export class DebugPosInfo { } public setBoundLeft(boundLeft: number): void { - this.boundLeft = boundLeft; + this.bl = boundLeft; } public getBoundLeft(): number | undefined { - return this.boundLeft; + return this.bl; } public setBoundRight(boundRight: number): void { - this.boundRight = boundRight; + this.br = boundRight; } public getBoundRight(): number | undefined { - return this.boundRight; + return this.br; } public setSourecLineNum(lineNum: number): void { - this.lineNum = lineNum; + this.l = lineNum; } public getSourceLineNum(): number { - return this.lineNum; + return this.l; } public setSourecColumnNum(columnNum: number): void { - this.columnNum = columnNum; + this.c = columnNum; } public getSourceColumnNum(): number { - return this.columnNum; + return this.c; } public setWholeLine(wholeLine: string): void { - this.wholeLine = wholeLine; + this.w = wholeLine; } public getWholeLine(): string | undefined { - return this.wholeLine; + return this.w; } public ClearMembersForReleaseBuild(): void { this.ClearMembersForDebugBuild(); - this.boundLeft = undefined; - this.boundRight = undefined; + this.bl = undefined; + this.br = undefined; } public ClearMembersForDebugBuild(): void { - this.wholeLine = undefined; + this.w = undefined; this.nodeKind = undefined; } } export class VariableDebugInfo { // @ts-ignore - private name = ""; + private n = ""; // name // @ts-ignore - private variable: Variable | undefined; + private v: Variable | undefined; // variables // @ts-ignore - private signature = ""; + private s = ""; // signature // @ts-ignore - private signatureType = ""; + private st = ""; // signature type // @ts-ignore - private reg: number = -1; + private r: number = -1; private start: number = -1; // @ts-ignore - private length: number = -1; + private len: number = -1; constructor(name: string, signature: string, signatureType: string, reg: number, start: number = 0, length: number = 0) { - this.name = name; - this.signature = signature; - this.signatureType = signatureType; - this.reg = reg; + this.n = name; + this.s = signature; + this.st = signatureType; + this.r = reg; this.start = start; - this.length = length; + this.len = length; } public setStart(start: number): void { @@ -138,7 +138,7 @@ export class VariableDebugInfo { } public setLength(length: number): void { - this.length = length; + this.len = length; } } diff --git a/ts2panda/src/pandasm.ts b/ts2panda/src/pandasm.ts index b537cab85bbc617e00978dc6f588e96621a5bd4a..3b1ecdfa8d60bce4139fcda2002afe7a06eaffad 100644 --- a/ts2panda/src/pandasm.ts +++ b/ts2panda/src/pandasm.ts @@ -27,84 +27,83 @@ export class Metadata { } export class Signature { - public params: number; - public retType: string | undefined; // return type is always 'any', so we ignore it in json + public p: number; // parameters + public rt: string | undefined; // return type is always 'any', so we ignore it in json constructor(params: number = 0, retType?: string | undefined) { - this.params = params; - this.retType = retType; + this.p = params; + this.rt = retType; } } export class Ins { - public op: string; - public regs: Array | undefined; - public ids: Array | undefined; - public imms: Array | undefined; - public label: string | undefined; + public o: number | undefined; // op + public r: Array | undefined; // resgs + public id: Array | undefined; // ids + public im: Array | undefined; // imms + public l: string | undefined; // label - public debug_pos_info: DebugPosInfo | undefined; + public d: DebugPosInfo | undefined; // debug position info constructor( - op: string, + op: number | undefined = undefined, regs: Array | undefined = undefined, ids: Array | undefined = undefined, imms: Array | undefined = undefined, label: string | undefined = undefined, - debug_pos_info: DebugPosInfo | undefined = undefined, + dbg_pos: DebugPosInfo | undefined = undefined, ) { - this.op = op; - this.regs = regs; - this.ids = ids; - this.imms = imms; - this.label = label; - this.debug_pos_info = debug_pos_info; + this.o = op; + this.r = regs; + this.id = ids; + this.im = imms; + this.l = label; + this.d = dbg_pos; } } export class Function { - public name: string; - public signature: Signature; - public regs_num: number; - public ins: Array; - public labels: Array; - public metadata: Metadata; - public catchTables: Array; - public variables: Array | undefined; - public sourceFile: string; - public sourceCode: string | undefined; - public callType: number; - public typeInfo: Array; - public exportedSymbol2Types: Array | undefined; - public declaredSymbol2Types: Array | undefined; + public n: string; // name + public s: Signature; // signature + public r: number; // regs number + public i: Array; // ins + public l: Array | undefined; // labels + public ca_tab: Array | undefined; // catch tabels + public v: Array | undefined; // variables + public sf: string; // source file + public sc: string | undefined; // source code + public ct: number | undefined; // call type + public ti: Array | undefined; // typeinfo: record type index array, starts from reg_0 + public es2t: Array | undefined; // exportedSymbol2Types + public ds2t: Array | undefined; // declaredSymbol2Types constructor( name: string, signature: Signature, regs_num: number = 0, ins: Array = [], - labels: Array = [], - variables: Array | undefined = undefined, - sourceFile: string = "", - sourceCode: string | undefined = undefined, - callType: number = 0, - typeInfo: Array, + labs: Array | undefined = undefined, + vars: Array | undefined = undefined, + catchTables: Array | undefined = undefined, + sourceFiles: string = "", + sourceCodes: string | undefined = undefined, + callType: number | undefined = undefined, + typeInfo: Array | undefined = undefined, exportedSymbol2Types: Array | undefined = undefined, declaredSymbol2Types: Array | undefined = undefined ) { - this.name = name; - this.signature = signature; - this.ins = ins; - this.labels = labels; - this.regs_num = regs_num; - this.metadata = new Metadata(); - this.catchTables = []; - this.variables = variables; - this.sourceFile = sourceFile; - this.sourceCode = sourceCode; - this.callType = callType; - this.typeInfo = typeInfo; - this.exportedSymbol2Types = exportedSymbol2Types; - this.declaredSymbol2Types = declaredSymbol2Types; + this.n = name; + this.s = signature; + this.i = ins; + this.l = labs; + this.r = regs_num; + this.ca_tab = catchTables; + this.v = vars; + this.sf = sourceFiles; + this.sc = sourceCodes; + this.ct = callType; + this.ti = typeInfo; + this.es2t = exportedSymbol2Types; + this.ds2t = declaredSymbol2Types; } } @@ -163,18 +162,18 @@ export class Program { } export class CatchTable { - public tryBeginLabel: string; - public tryEndLabel: string; - public catchBeginLabel: string; + public tb_lab: string; // try begine label + public te_lab: string; // try end label + public cb_lab: string; // catch begin label constructor( tryBeginLabel: string, tryEndLabel: string, catchBeginLabel: string ) { - this.tryBeginLabel = tryBeginLabel; - this.tryEndLabel = tryEndLabel; - this.catchBeginLabel = catchBeginLabel; + this.tb_lab = tryBeginLabel; + this.te_lab = tryEndLabel; + this.cb_lab = catchBeginLabel; } } diff --git a/ts2panda/src/ts2panda.ts b/ts2panda/src/ts2panda.ts index 369ee26e75549b9e23fff19fe9a015658a2ac61c..3793512593e7bd0d465e68b0030f883551e774b0 100644 --- a/ts2panda/src/ts2panda.ts +++ b/ts2panda/src/ts2panda.ts @@ -18,6 +18,7 @@ import { DebugPosInfo } from "./debuginfo"; import { Imm, IRNode, + IRNodeKind, Label, OperandType, VReg @@ -38,7 +39,6 @@ import { isRangeInst, getRangeStartVregPos } from "./base/util"; -import { TypeOfVreg } from "./pandasm"; import { LiteralBuffer } from "./base/literal"; const dollarSign: RegExp = /\$/g; @@ -53,7 +53,7 @@ const JsonType = { }; export class Ts2Panda { static strings: Set = new Set(); - static labelPrefix = "LABEL_"; + static labelPrefix = "L_"; static jsonString: string = ""; constructor() { @@ -68,7 +68,7 @@ export class Ts2Panda { let labels: Array = []; pg.getInsns().forEach((insn: IRNode) => { - let insOpcode = insn.mnemonic; + let insOpcode = insn.kind >= IRNodeKind.VREG ? undefined : insn.kind; let insRegs: Array = []; let insIds: Array = []; let insImms: Array = []; @@ -123,25 +123,24 @@ export class Ts2Panda { return { insns: insns, regsNum: (pg.getTotalRegsNum() - pg.getParametersCount()), - labels: labels + labels: labels.length == 0 ? undefined : labels }; } static dumpStringsArray(ts2abc: any) { let strings_arr = Array.from(Ts2Panda.strings); - strings_arr.forEach(function(str) { - let strObject = { - "type": JsonType.string, - "string": str - } - let jsonStrUnicode = escapeUnicode(JSON.stringify(strObject, null, 2)); - jsonStrUnicode = "$" + jsonStrUnicode.replace(dollarSign, '#$') + "$"; - if (CmdOptions.isEnableDebugLog()) { - Ts2Panda.jsonString += jsonStrUnicode; - } - ts2abc.stdio[3].write(jsonStrUnicode + '\n'); - }); + let strObject = { + "t": JsonType.string, + "s": strings_arr + } + + let jsonStrUnicode = escapeUnicode(JSON.stringify(strObject, null, 2)); + jsonStrUnicode = "$" + jsonStrUnicode.replace(dollarSign, '#$') + "$"; + if (CmdOptions.isEnableDebugLog()) { + Ts2Panda.jsonString += jsonStrUnicode; + } + ts2abc.stdio[3].write(jsonStrUnicode + '\n'); } static dumpTypeLiteralArrayBuffer() { @@ -168,8 +167,8 @@ export class Ts2Panda { literalArrays.forEach(function(literalArray) { let literalArrayObject = { - "type": JsonType.literal_arr, - "literalArray": literalArray + "t": JsonType.literal_arr, + "lit_arr": literalArray } let jsonLiteralArrUnicode = escapeUnicode(JSON.stringify(literalArrayObject, null, 2)); jsonLiteralArrUnicode = "$" + jsonLiteralArrUnicode.replace(dollarSign, '#$') + "$"; @@ -182,7 +181,7 @@ export class Ts2Panda { static dumpCmdOptions(ts2abc: any): void { let options = { - "type": JsonType.options, + "t": JsonType.options, "module_mode": CmdOptions.isModules(), "debug_mode": CmdOptions.isDebugMode(), "log_enabled": CmdOptions.isEnableDebugLog(), @@ -205,10 +204,9 @@ export class Ts2Panda { let sourceFile = pg.getSourceFileDebugInfo(); let callType = pg.getCallType(); let typeRecord = pg.getLocals(); - let typeInfo = new Array(); + let typeInfo = new Array(); typeRecord.forEach((vreg) => { - let typeOfVreg = new TypeOfVreg(vreg.num, vreg.getTypeIndex()); - typeInfo.push(typeOfVreg); + typeInfo.push(vreg.getTypeIndex()); if (CmdOptions.enableTypeLog()) { console.log("---------------------------------------"); console.log("- vreg name:", vreg.getVariableName()); @@ -244,6 +242,25 @@ export class Ts2Panda { sourceCode = undefined; } + let catchTableArr; + let catchTables = generateCatchTables(pg.getCatchMap()); + if (!catchTables) { + catchTableArr = undefined; + } else { + catchTableArr = []; + catchTables.forEach((catchTable) => { + let catchBeginLabel = catchTable.getCatchBeginLabel(); + let labelPairs = catchTable.getLabelPairs(); + labelPairs.forEach((labelPair) => { + catchTableArr.push(new CatchTable( + Ts2Panda.labelPrefix + labelPair.getBeginLabel().id, + Ts2Panda.labelPrefix + labelPair.getEndLabel().id, + Ts2Panda.labelPrefix + catchBeginLabel.id + )); + }); + }); + } + let func = new Function( funcName, funcSignature, @@ -251,6 +268,7 @@ export class Ts2Panda { funcInsnsAndRegsNum.insns, funcInsnsAndRegsNum.labels, variables, + catchTableArr, sourceFile, sourceCode, callType, @@ -258,24 +276,12 @@ export class Ts2Panda { exportedSymbol2Types, declaredSymbol2Types ); - let catchTables = generateCatchTables(pg.getCatchMap()); - catchTables.forEach((catchTable) => { - let catchBeginLabel = catchTable.getCatchBeginLabel(); - let labelPairs = catchTable.getLabelPairs(); - labelPairs.forEach((labelPair) => { - func.catchTables.push(new CatchTable( - Ts2Panda.labelPrefix + labelPair.getBeginLabel().id, - Ts2Panda.labelPrefix + labelPair.getEndLabel().id, - Ts2Panda.labelPrefix + catchBeginLabel.id - )); - }); - }); LOGD(func); let funcObject = { - "type": JsonType.function, - "func_body": func + "t": JsonType.function, + "fb": func } let jsonFuncUnicode = escapeUnicode(JSON.stringify(funcObject, null, 2)); jsonFuncUnicode = "$" + jsonFuncUnicode.replace(dollarSign, '#$') + "$"; diff --git a/ts2panda/templates/irnodes.ts.erb b/ts2panda/templates/irnodes.ts.erb index 98a1216e44b308398687c2de27fe799aa5f1bd12..4ebb71f3c00b5ceca36613e66fe0bb4648e5306c 100755 --- a/ts2panda/templates/irnodes.ts.erb +++ b/ts2panda/templates/irnodes.ts.erb @@ -23,14 +23,14 @@ import { } from "./debuginfo"; export enum IRNodeKind { +% Panda::instructions.group_by(&:mnemonic).each do |mnemonic, group| + <%= get_node_kind(mnemonic) %>, +% end VREG, IMM, LABEL, VIRTUALINS_DYN, DEFINE_GLOBAL_VAR, -% Panda::instructions.group_by(&:mnemonic).each do |mnemonic, group| - <%= get_node_kind(mnemonic) %>, -% end } export function getInstructionSize(opcode: IRNodeKind) { diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index 6d5e7b14a7497f4edb2dff5b16972799ccc0d49e..a44a9e6871d1bd7fbad7d4c80b1dc9755055537b 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -49,12 +49,12 @@ namespace { constexpr std::size_t BOUND_RIGHT = 0; constexpr std::size_t LINE_NUMBER = 0; constexpr bool IS_DEFINED = true; - // Temprorary map to simplify debuging - std::unordered_map g_opcodeMap = { -#define OPLIST(opcode, name, optype, width, flags, def_idx, use_idxs) {name, panda::pandasm::Opcode::opcode}, + int g_opCodeIndex = 0; + std::unordered_map g_opcodeMap = { +#define OPLIST(opcode, name, optype, width, flags, def_idx, use_idxs) {g_opCodeIndex++, panda::pandasm::Opcode::opcode}, PANDA_INSTRUCTION_LIST(OPLIST) #undef OPLIST - {"", panda::pandasm::Opcode::INVALID}, + {-1, panda::pandasm::Opcode::INVALID}, }; } @@ -221,7 +221,7 @@ static void ParseLiteral(const Json::Value &literal, std::vector(literal["tag"].asUInt()); + uint8_t tagValue = static_cast(literal["t"].asUInt()); tagLiteral.tag_ = panda::panda_file::LiteralTag::TAGVALUE; tagLiteral.value_ = tagValue; @@ -230,32 +230,32 @@ static void ParseLiteral(const Json::Value &literal, std::vector(panda::panda_file::LiteralTag::BOOL): { valueLiteral.tag_ = panda::panda_file::LiteralTag::BOOL; - valueLiteral.value_ = literal["value"].asBool(); + valueLiteral.value_ = literal["v"].asBool(); break; } case static_cast(panda::panda_file::LiteralTag::INTEGER): { valueLiteral.tag_ = panda::panda_file::LiteralTag::INTEGER; - valueLiteral.value_ = static_cast(literal["value"].asInt()); + valueLiteral.value_ = static_cast(literal["v"].asInt()); break; } case static_cast(panda::panda_file::LiteralTag::DOUBLE): { valueLiteral.tag_ = panda::panda_file::LiteralTag::DOUBLE; - valueLiteral.value_ = literal["value"].asDouble(); + valueLiteral.value_ = literal["v"].asDouble(); break; } case static_cast(panda::panda_file::LiteralTag::STRING): { valueLiteral.tag_ = panda::panda_file::LiteralTag::STRING; - valueLiteral.value_ = ParseString(literal["value"].asString()); + valueLiteral.value_ = ParseString(literal["v"].asString()); break; } case static_cast(panda::panda_file::LiteralTag::METHOD): { valueLiteral.tag_ = panda::panda_file::LiteralTag::METHOD; - valueLiteral.value_ = ParseString(literal["value"].asString()); + valueLiteral.value_ = ParseString(literal["v"].asString()); break; } case static_cast(panda::panda_file::LiteralTag::GENERATORMETHOD): { valueLiteral.tag_ = panda::panda_file::LiteralTag::GENERATORMETHOD; - valueLiteral.value_ = ParseString(literal["value"].asString()); + valueLiteral.value_ = ParseString(literal["v"].asString()); break; } case static_cast(panda::panda_file::LiteralTag::ACCESSOR): { @@ -265,7 +265,7 @@ static void ParseLiteral(const Json::Value &literal, std::vector(panda::panda_file::LiteralTag::METHODAFFILIATE): { valueLiteral.tag_ = panda::panda_file::LiteralTag::METHODAFFILIATE; - valueLiteral.value_ = static_cast(literal["value"].asUInt()); + valueLiteral.value_ = static_cast(literal["v"].asUInt()); break; } case static_cast(panda::panda_file::LiteralTag::NULLVALUE): { @@ -325,9 +325,8 @@ static panda::pandasm::Record ParseRecord(const Json::Value &record) static void ParseInstructionOpCode(const Json::Value &ins, panda::pandasm::Ins &pandaIns) { - // read opcode as string (can be changed in future) - if (ins.isMember("op") && ins["op"].isString()) { - std::string opcode = ins["op"].asString(); + if (ins.isMember("o") && ins["o"].isInt()) { + auto opcode = ins["o"].asInt(); if (g_opcodeMap.find(opcode) != g_opcodeMap.end()) { pandaIns.opcode = g_opcodeMap[opcode]; } @@ -336,8 +335,8 @@ static void ParseInstructionOpCode(const Json::Value &ins, panda::pandasm::Ins & static void ParseInstructionRegs(const Json::Value &ins, panda::pandasm::Ins &pandaIns) { - if (ins.isMember("regs") && ins["regs"].isArray()) { - auto regs = ins["regs"]; + if (ins.isMember("r") && ins["r"].isArray()) { + auto regs = ins["r"]; for (Json::ArrayIndex i = 0; i < regs.size(); ++i) { pandaIns.regs.emplace_back(regs[i].asUInt()); } @@ -346,8 +345,8 @@ static void ParseInstructionRegs(const Json::Value &ins, panda::pandasm::Ins &pa static void ParseInstructionIds(const Json::Value &ins, panda::pandasm::Ins &pandaIns) { - if (ins.isMember("ids") && ins["ids"].isArray()) { - auto ids = ins["ids"]; + if (ins.isMember("id") && ins["id"].isArray()) { + auto ids = ins["id"]; for (Json::ArrayIndex i = 0; i < ids.size(); ++i) { if (ids[i].isString()) { pandaIns.ids.emplace_back(ParseString(ids[i].asString())); @@ -358,8 +357,8 @@ static void ParseInstructionIds(const Json::Value &ins, panda::pandasm::Ins &pan static void ParseInstructionImms(const Json::Value &ins, panda::pandasm::Ins &pandaIns) { - if (ins.isMember("imms") && ins["imms"].isArray()) { - auto imms = ins["imms"]; + if (ins.isMember("im") && ins["im"].isArray()) { + auto imms = ins["im"]; for (Json::ArrayIndex i = 0; i < imms.size(); ++i) { double imsValue = imms[i].asDouble(); Logd("imm: %lf ", imsValue); @@ -375,8 +374,8 @@ static void ParseInstructionImms(const Json::Value &ins, panda::pandasm::Ins &pa static void ParseInstructionLabel(const Json::Value &ins, panda::pandasm::Ins &pandaIns) { - if (ins.isMember("label") && ins["label"].isString()) { - std::string label = ins["label"].asString(); + if (ins.isMember("l") && ins["l"].isString()) { + std::string label = ins["l"].asString(); if (label.length() != 0) { Logd("label:\t%s", label.c_str()); pandaIns.set_label = true; @@ -389,28 +388,31 @@ static void ParseInstructionLabel(const Json::Value &ins, panda::pandasm::Ins &p static void ParseInstructionDebugInfo(const Json::Value &ins, panda::pandasm::Ins &pandaIns) { panda::pandasm::debuginfo::Ins insDebug; - if (ins.isMember("debug_pos_info") && ins["debug_pos_info"].isObject()) { - auto debugPosInfo = ins["debug_pos_info"]; + if (ins.isMember("d") && ins["d"].isObject()) { + auto debugPosInfo = ins["d"]; if (GetDebugModeEnabled()) { - if (debugPosInfo.isMember("boundLeft") && debugPosInfo["boundLeft"].isInt()) { - insDebug.bound_left = debugPosInfo["boundLeft"].asUInt(); + if (debugPosInfo.isMember("bl") && debugPosInfo["bl"].isInt()) { + insDebug.bound_left = debugPosInfo["bl"].asUInt(); } - if (debugPosInfo.isMember("boundRight") && debugPosInfo["boundRight"].isInt()) { - insDebug.bound_right = debugPosInfo["boundRight"].asUInt(); + if (debugPosInfo.isMember("br") && debugPosInfo["br"].isInt()) { + insDebug.bound_right = debugPosInfo["br"].asUInt(); } - if (debugPosInfo.isMember("wholeLine") && debugPosInfo["wholeLine"].isString()) { - insDebug.whole_line = debugPosInfo["wholeLine"].asString(); + // whole line + if (debugPosInfo.isMember("w") && debugPosInfo["w"].isString()) { + insDebug.whole_line = debugPosInfo["w"].asString(); } - if (debugPosInfo.isMember("columnNum") && debugPosInfo["columnNum"].isInt()) { - insDebug.column_number = debugPosInfo["columnNum"].asInt(); + // column number + if (debugPosInfo.isMember("c") && debugPosInfo["c"].isInt()) { + insDebug.column_number = debugPosInfo["c"].asInt(); } } - if (debugPosInfo.isMember("lineNum") && debugPosInfo["lineNum"].isInt()) { - insDebug.line_number = debugPosInfo["lineNum"].asInt(); + // line number + if (debugPosInfo.isMember("l") && debugPosInfo["l"].isInt()) { + insDebug.line_number = debugPosInfo["l"].asInt(); } } @@ -435,36 +437,36 @@ static int ParseVariablesDebugInfo(const Json::Value &function, panda::pandasm:: return RETURN_SUCCESS; } - if (function.isMember("variables") && function["variables"].isArray()) { - for (Json::ArrayIndex i = 0; i < function["variables"].size(); ++i) { - if (!function["variables"][i].isObject()) { + if (function.isMember("v") && function["v"].isArray()) { + for (Json::ArrayIndex i = 0; i < function["v"].size(); ++i) { + if (!function["v"][i].isObject()) { continue; } panda::pandasm::debuginfo::LocalVariable variableDebug; - auto variable = function["variables"][i]; - if (variable.isMember("name") && variable["name"].isString()) { - variableDebug.name = variable["name"].asString(); + auto variable = function["v"][i]; + if (variable.isMember("n") && variable["n"].isString()) { + variableDebug.name = variable["n"].asString(); // name } - if (variable.isMember("signature") && variable["signature"].isString()) { - variableDebug.signature = variable["signature"].asString(); + if (variable.isMember("s") && variable["s"].isString()) { + variableDebug.signature = variable["s"].asString(); // signature } - if (variable.isMember("signatureType") && variable["signatureType"].isString()) { - variableDebug.signature_type = variable["signatureType"].asString(); + if (variable.isMember("st") && variable["st"].isString()) { + variableDebug.signature_type = variable["st"].asString(); // signature type } - if (variable.isMember("reg") && variable["reg"].isInt()) { - variableDebug.reg = variable["reg"].asInt(); + if (variable.isMember("r") && variable["r"].isInt()) { + variableDebug.reg = variable["r"].asInt(); // regs } if (variable.isMember("start") && variable["start"].isInt()) { - variableDebug.start = variable["start"].asUInt(); + variableDebug.start = variable["start"].asUInt(); // start } - if (variable.isMember("length") && variable["length"].isInt()) { - variableDebug.length = variable["length"].asUInt(); + if (variable.isMember("len") && variable["len"].isInt()) { + variableDebug.length = variable["len"].asUInt(); // length } pandaFunc.local_variable_debug.push_back(variableDebug); @@ -476,13 +478,13 @@ static int ParseVariablesDebugInfo(const Json::Value &function, panda::pandasm:: static int ParseSourceFileDebugInfo(const Json::Value &function, panda::pandasm::Function &pandaFunc) { - if (function.isMember("sourceFile") && function["sourceFile"].isString()) { - pandaFunc.source_file = function["sourceFile"].asString(); + if (function.isMember("sf") && function["sf"].isString()) { + pandaFunc.source_file = function["sf"].asString(); } if (GetDebugModeEnabled()) { - if (function.isMember("sourceCode") && function["sourceCode"].isString()) { - pandaFunc.source_code = function["sourceCode"].asString(); + if (function.isMember("sc") && function["sc"].isString()) { + pandaFunc.source_code = function["sc"].asString(); } } @@ -493,20 +495,17 @@ static panda::pandasm::Function::CatchBlock ParsecatchBlock(const Json::Value &c { panda::pandasm::Function::CatchBlock pandaCatchBlock; - if (catch_block.isMember("tryBeginLabel") && catch_block["tryBeginLabel"].isString()) { - pandaCatchBlock.try_begin_label = catch_block["tryBeginLabel"].asString(); + if (catch_block.isMember("tb_lab") && catch_block["tb_lab"].isString()) { + pandaCatchBlock.try_begin_label = catch_block["tb_lab"].asString(); } - if (catch_block.isMember("tryEndLabel") && catch_block["tryEndLabel"].isString()) { - pandaCatchBlock.try_end_label = catch_block["tryEndLabel"].asString(); + if (catch_block.isMember("te_lab") && catch_block["te_lab"].isString()) { + pandaCatchBlock.try_end_label = catch_block["te_lab"].asString(); } - if (catch_block.isMember("catchBeginLabel") && catch_block["catchBeginLabel"].isString()) { - pandaCatchBlock.catch_begin_label = catch_block["catchBeginLabel"].asString(); - } - - if (catch_block.isMember("catchBeginLabel") && catch_block["catchBeginLabel"].isString()) { - pandaCatchBlock.catch_end_label = catch_block["catchBeginLabel"].asString(); + if (catch_block.isMember("cb_lab") && catch_block["cb_lab"].isString()) { + pandaCatchBlock.catch_begin_label = catch_block["cb_lab"].asString(); + pandaCatchBlock.catch_end_label = catch_block["cb_lab"].asString(); } return pandaCatchBlock; @@ -515,24 +514,24 @@ static panda::pandasm::Function::CatchBlock ParsecatchBlock(const Json::Value &c panda::pandasm::Function GetFunctionDefintion(const Json::Value &function) { std::string funcName = ""; - if (function.isMember("name") && function["name"].isString()) { - funcName = function["name"].asString(); + if (function.isMember("n") && function["n"].isString()) { + funcName = function["n"].asString(); } std::string funcRetType = ""; auto params = std::vector(); - if (function.isMember("signature") && function["signature"].isObject()) { - auto signature = function["signature"]; - if (signature.isMember("retType") && signature["retType"].isString()) { - funcRetType = signature["retType"].asString(); + if (function.isMember("s") && function["s"].isObject()) { + auto signature = function["s"]; + if (signature.isMember("rt") && signature["rt"].isString()) { + funcRetType = signature["rt"].asString(); } else { funcRetType = "any"; } Logd("parsing function: %s return type: %s \n", funcName.c_str(), funcRetType.c_str()); - if (signature.isMember("params") && signature["params"].isInt()) { - auto paramNum = signature["params"].asUInt(); + if (signature.isMember("p") && signature["p"].isInt()) { + auto paramNum = signature["p"].asUInt(); for (Json::ArrayIndex i = 0; i < paramNum; ++i) { params.emplace_back(panda::pandasm::Type("any", 0), LANG_EXT); } @@ -540,8 +539,8 @@ panda::pandasm::Function GetFunctionDefintion(const Json::Value &function) } uint32_t regsNum = 0; - if (function.isMember("regs_num") && function["regs_num"].isInt()) { - regsNum = function["regs_num"].asUInt(); + if (function.isMember("r") && function["r"].isInt()) { + regsNum = function["r"].asUInt(); } auto pandaFunc = MakeFuncDefintion(funcName, funcRetType); @@ -551,23 +550,10 @@ panda::pandasm::Function GetFunctionDefintion(const Json::Value &function) return pandaFunc; } -static void ParseFunctionMetadata(const Json::Value &function, panda::pandasm::Function &pandaFunc) -{ - if (function.isMember("metadata") && function["metadata"].isObject()) { - auto metadata = function["metadata"]; - if (metadata.isMember("attribute") && metadata["attribute"].isString()) { - std::string fnMetadataAttribute = metadata["attribute"].asString(); - if (fnMetadataAttribute.length() > 0) { - pandaFunc.metadata->SetAttribute(fnMetadataAttribute); - } - } - } -} - static void ParseFunctionInstructions(const Json::Value &function, panda::pandasm::Function &pandaFunc) { - if (function.isMember("ins") && function["ins"].isArray()) { - auto ins = function["ins"]; + if (function.isMember("i") && function["i"].isArray()) { + auto ins = function["i"]; for (Json::ArrayIndex i = 0; i < ins.size(); ++i) { if (!ins[i].isObject()) { continue; @@ -582,8 +568,8 @@ static void ParseFunctionInstructions(const Json::Value &function, panda::pandas static void ParseFunctionLabels(const Json::Value &function, panda::pandasm::Function &pandaFunc) { - if (function.isMember("labels") && function["labels"].isArray()) { - auto labels = function["labels"]; + if (function.isMember("l") && function["l"].isArray()) { + auto labels = function["l"]; for (Json::ArrayIndex i = 0; i < labels.size(); ++i) { auto labelName = labels[i].asString(); auto pandaLabel = MakeLabel(labelName); @@ -596,8 +582,8 @@ static void ParseFunctionLabels(const Json::Value &function, panda::pandasm::Fun static void ParseFunctionCatchTables(const Json::Value &function, panda::pandasm::Function &pandaFunc) { - if (function.isMember("catchTables") && function["catchTables"].isArray()) { - auto catchTables = function["catchTables"]; + if (function.isMember("ca_tab") && function["ca_tab"].isArray()) { + auto catchTables = function["ca_tab"]; for (Json::ArrayIndex i = 0; i < catchTables.size(); ++i) { auto catchTable = catchTables[i]; if (!catchTable.isObject()) { @@ -613,16 +599,16 @@ static void ParseFunctionCatchTables(const Json::Value &function, panda::pandasm static void ParseFunctionCallType(const Json::Value &function, panda::pandasm::Function &pandaFunc) { std::string funcName = ""; - if (function.isMember("name") && function["name"].isString()) { - funcName = function["name"].asString(); + if (function.isMember("n") && function["n"].isString()) { + funcName = function["n"].asString(); } if (funcName == "func_main_0") { return ; } uint32_t callType = 0; - if (function.isMember("callType") && function["callType"].isInt()) { - callType = function["callType"].asUInt(); + if (function.isMember("ct") && function["ct"].isInt()) { + callType = function["ct"].asUInt(); } panda::pandasm::AnnotationData callTypeAnnotation("_ESCallTypeAnnotation"); std::string annotationName = "callType"; @@ -636,28 +622,16 @@ static void ParseFunctionCallType(const Json::Value &function, panda::pandasm::F static void ParseFunctionTypeInfo(const Json::Value &function, panda::pandasm::Function &pandaFunc) { - if (function.isMember("typeInfo") && function["typeInfo"].isArray()) { - auto typeInfo = function["typeInfo"]; + if (function.isMember("ti") && function["ti"].isArray()) { + auto typeInfo = function["ti"]; panda::pandasm::AnnotationData funcAnnotation("_ESTypeAnnotation"); std::vector elements; - for (Json::ArrayIndex i = 0; i < typeInfo.size(); i++) { - auto type = typeInfo[i]; - if (!type.isObject()) { - continue; - } - - uint32_t vregNum = 0; - if (type.isMember("vregNum") && type["vregNum"].isInt()) { - vregNum = type["vregNum"].asUInt(); - } - uint32_t typeIndex = 0; - if (type.isMember("typeIndex") && type["typeIndex"].isInt()) { - typeIndex = type["typeIndex"].asUInt(); - } + for (Json::ArrayIndex i = 0; i < typeInfo.size(); i++) { + auto typeIndex = typeInfo[i].asUInt(); panda::pandasm::ScalarValue vNum( - panda::pandasm::ScalarValue::Create(vregNum)); + panda::pandasm::ScalarValue::Create(i)); elements.emplace_back(std::move(vNum)); panda::pandasm::ScalarValue tIndex( panda::pandasm::ScalarValue::Create(typeIndex)); @@ -677,15 +651,15 @@ static void ParseFunctionTypeInfo(const Json::Value &function, panda::pandasm::F static void ParseFunctionExportedType(const Json::Value &function, panda::pandasm::Function &pandaFunc) { std::string funcName = ""; - if (function.isMember("name") && function["name"].isString()) { - funcName = function["name"].asString(); + if (function.isMember("n") && function["n"].isString()) { + funcName = function["n"].asString(); if (funcName != "func_main_0") { return; } } - if (function.isMember("exportedSymbol2Types") && function["exportedSymbol2Types"].isArray()) { - auto exportedTypes = function["exportedSymbol2Types"]; + if (function.isMember("es2t") && function["es2t"].isArray()) { + auto exportedTypes = function["es2t"]; panda::pandasm::AnnotationData funcAnnotation("_ESTypeAnnotation"); std::vector symbolElements; std::vector symbolTypeElements; @@ -733,15 +707,15 @@ static void ParseFunctionExportedType(const Json::Value &function, panda::pandas static void ParseFunctionDeclaredType(const Json::Value &function, panda::pandasm::Function &pandaFunc) { std::string funcName = ""; - if (function.isMember("name") && function["name"].isString()) { - funcName = function["name"].asString(); + if (function.isMember("n") && function["n"].isString()) { + funcName = function["n"].asString(); if (funcName != "func_main_0") { return; } } - if (function.isMember("declaredSymbol2Types") && function["declaredSymbol2Types"].isArray()) { - auto declaredTypes = function["declaredSymbol2Types"]; + if (function.isMember("ds2t") && function["ds2t"].isArray()) { + auto declaredTypes = function["ds2t"]; panda::pandasm::AnnotationData funcAnnotation("_ESTypeAnnotation"); std::vector symbolElements; std::vector symbolTypeElements; @@ -789,7 +763,6 @@ static void ParseFunctionDeclaredType(const Json::Value &function, panda::pandas static panda::pandasm::Function ParseFunction(const Json::Value &function) { auto pandaFunc = GetFunctionDefintion(function); - ParseFunctionMetadata(function, pandaFunc); ParseFunctionInstructions(function, pandaFunc); ParseVariablesDebugInfo(function, pandaFunc); ParseSourceFileDebugInfo(function, pandaFunc); @@ -924,26 +897,29 @@ static void ParseOptions(const Json::Value &rootValue, panda::pandasm::Program & static void ParseSingleFunc(const Json::Value &rootValue, panda::pandasm::Program &prog) { - auto function = ParseFunction(rootValue["func_body"]); + auto function = ParseFunction(rootValue["fb"]); prog.function_table.emplace(function.name.c_str(), std::move(function)); } static void ParseSingleRec(const Json::Value &rootValue, panda::pandasm::Program &prog) { - auto record = ParseRecord(rootValue["rec_body"]); + auto record = ParseRecord(rootValue["rb"]); prog.record_table.emplace(record.name.c_str(), std::move(record)); } static void ParseSingleStr(const Json::Value &rootValue, panda::pandasm::Program &prog) { - prog.strings.insert(ParseString(rootValue["string"].asString())); + auto strArr = rootValue["s"]; + for (Json::ArrayIndex i = 0; i < strArr.size(); ++i) { + prog.strings.insert(ParseString(strArr[i].asString())); + } } static void ParseSingleLiteralBuf(const Json::Value &rootValue, panda::pandasm::Program &prog) { std::vector literalArray; - auto literalBuffer = rootValue["literalArray"]; - auto literals = literalBuffer["literalBuffer"]; + auto literalBuffer = rootValue["lit_arr"]; + auto literals = literalBuffer["lb"]; for (Json::ArrayIndex i = 0; i < literals.size(); ++i) { ParseLiteral(literals[i], literalArray); } @@ -960,30 +936,30 @@ static int ParseSmallPieceJson(const std::string &subJson, panda::pandasm::Progr return RETURN_FAILED; } int type = -1; - if (rootValue.isMember("type") && rootValue["type"].isInt()) { - type = rootValue["type"].asInt(); + if (rootValue.isMember("t") && rootValue["t"].isInt()) { + type = rootValue["t"].asInt(); } switch (type) { case static_cast(JsonType::FUNCTION): { - if (rootValue.isMember("func_body") && rootValue["func_body"].isObject()) { + if (rootValue.isMember("fb") && rootValue["fb"].isObject()) { ParseSingleFunc(rootValue, prog); } break; } case static_cast(JsonType::RECORD): { - if (rootValue.isMember("rec_body") && rootValue["rec_body"].isObject()) { + if (rootValue.isMember("rb") && rootValue["rb"].isObject()) { ParseSingleRec(rootValue, prog); } break; } case static_cast(JsonType::STRING): { - if (rootValue.isMember("string") && rootValue["string"].isString()) { + if (rootValue.isMember("s") && rootValue["s"].isArray()) { ParseSingleStr(rootValue, prog); } break; } case static_cast(JsonType::LITERALBUFFER): { - if (rootValue.isMember("literalArray") && rootValue["literalArray"].isObject()) { + if (rootValue.isMember("lit_arr") && rootValue["lit_arr"].isObject()) { ParseSingleLiteralBuf(rootValue, prog); } break; @@ -1070,7 +1046,7 @@ bool GenerateProgram(const std::string &data, std::string output, int optLevel, #endif if (!panda::pandasm::AsmEmitter::Emit(output.c_str(), prog, nullptr)) { - std::cerr << "Failed to emit binary data" << std::endl; + std::cerr << "Failed to emit binary data: " << panda::pandasm::AsmEmitter::GetLastError() << std::endl; return false; }