1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { LiteralBuffer } from "./base/literal"; 17import { DebugPosInfo, VariableDebugInfo } from "./debuginfo"; 18 19export class Metadata { 20 public attribute: string; 21 22 constructor( 23 attribute: string = "" 24 ) { 25 this.attribute = attribute; 26 } 27} 28 29export class Signature { 30 public p: number; // parameters 31 public rt: string | undefined; // return type is always 'any', so we ignore it in json 32 33 constructor(params: number = 0, retType?: string | undefined) { 34 this.p = params; 35 this.rt = retType; 36 } 37} 38 39export class Ins { 40 public o: number | undefined; // op 41 public r: Array<number> | undefined; // resgs 42 public id: Array<string> | undefined; // ids 43 public im: Array<number> | undefined; // imms 44 public l: string | undefined; // label 45 46 public d: DebugPosInfo | undefined; // debug position info 47 constructor( 48 op: number | undefined = undefined, 49 regs: Array<number> | undefined = undefined, 50 ids: Array<string> | undefined = undefined, 51 imms: Array<number> | undefined = undefined, 52 label: string | undefined = undefined, 53 dbg_pos: DebugPosInfo | undefined = undefined, 54 ) { 55 this.o = op; 56 this.r = regs; 57 this.id = ids; 58 this.im = imms; 59 this.l = label; 60 this.d = dbg_pos; 61 } 62} 63 64export class Function { 65 public n: string; // name 66 public s: Signature; // signature 67 public r: number; // regs number 68 public i: Array<Ins>; // ins 69 public l: Array<string> | undefined; // labels 70 public ca_tab: Array<CatchTable> | undefined; // catch tabels 71 public v: Array<VariableDebugInfo> | undefined; // variables 72 public sf: string; // source file 73 public sc: string | undefined; // source code 74 public ct: number | undefined; // call type 75 public ti: Array<number> | undefined; // typeinfo: record type index array, starts from reg_0 76 public es2t: Array<ExportedSymbol2Type> | undefined; // exportedSymbol2Types 77 public ds2t: Array<DeclaredSymbol2Type> | undefined; // declaredSymbol2Types 78 public kind: number; 79 public icSize: number; 80 81 constructor( 82 name: string, 83 signature: Signature, 84 regs_num: number = 0, 85 ins: Array<Ins> = [], 86 labs: Array<string> | undefined = undefined, 87 vars: Array<VariableDebugInfo> | undefined = undefined, 88 catchTables: Array<CatchTable> | undefined = undefined, 89 sourceFiles: string = "", 90 sourceCodes: string | undefined = undefined, 91 callType: number | undefined = undefined, 92 typeInfo: Array<number> | undefined = undefined, 93 exportedSymbol2Types: Array<ExportedSymbol2Type> | undefined = undefined, 94 declaredSymbol2Types: Array<DeclaredSymbol2Type> | undefined = undefined, 95 kind: number, 96 icSize: number 97 ) { 98 this.n = name; 99 this.s = signature; 100 this.i = ins; 101 this.l = labs; 102 this.r = regs_num; 103 this.ca_tab = catchTables; 104 this.v = vars; 105 this.sf = sourceFiles; 106 this.sc = sourceCodes; 107 this.ct = callType; 108 this.ti = typeInfo; 109 this.es2t = exportedSymbol2Types; 110 this.ds2t = declaredSymbol2Types; 111 this.kind = kind; 112 this.icSize = icSize; 113 } 114} 115 116export class Record { 117 public name: string; 118 119 constructor(name: string) { 120 this.name = name; 121 } 122} 123 124export class Program { 125 public functions: Array<Function>; 126 public records: Array<Record>; 127 public strings: Set<string>; 128 public strings_arr: Array<string>; 129 public literalArrays: Array<LiteralBuffer>; 130 public module_mode: boolean; 131 public debug_mode: boolean; 132 public log_enabled: boolean; 133 public opt_level: number; 134 public opt_log_level: string; 135 136 constructor() { 137 this.functions = []; 138 this.records = []; 139 this.strings = new Set(); 140 this.strings_arr = []; 141 this.literalArrays = []; 142 this.module_mode = false; 143 this.debug_mode = false; 144 this.log_enabled = false; 145 this.opt_level = 1; 146 this.opt_log_level = "error"; 147 } 148 149 finalize(): void { 150 this.strings_arr = Array.from(this.strings); 151 } 152} 153 154export class CatchTable { 155 public tb_lab: string; // try begine label 156 public te_lab: string; // try end label 157 public cb_lab: string; // catch begin label 158 159 constructor( 160 tryBeginLabel: string, 161 tryEndLabel: string, 162 catchBeginLabel: string 163 ) { 164 this.tb_lab = tryBeginLabel; 165 this.te_lab = tryEndLabel; 166 this.cb_lab = catchBeginLabel; 167 } 168} 169 170export class TypeOfVreg { 171 // @ts-ignore 172 private vregNum: number; 173 // @ts-ignore 174 private typeIndex: number; 175 176 constructor(vregNum: number, typeIndex: number) { 177 this.vregNum = vregNum; 178 this.typeIndex = typeIndex; 179 } 180} 181 182export class ExportedSymbol2Type { 183 // @ts-ignore 184 private symbol: string; 185 // @ts-ignore 186 private type: number; 187 188 constructor(symbol: string, type: number) { 189 this.symbol = symbol; 190 this.type = type; 191 } 192} 193 194export class DeclaredSymbol2Type { 195 // @ts-ignore 196 private symbol: string; 197 // @ts-ignore 198 private type: number; 199 200 constructor(symbol: string, type: number) { 201 this.symbol = symbol; 202 this.type = type; 203 } 204} 205 206export class RegularImportEntry { 207 public localName: string; 208 public importName: string; 209 public moduleRequest: number; 210 211 constructor(localName: string, importName: string, moduleRequest: number) { 212 this.localName = localName; 213 this.importName = importName; 214 this.moduleRequest = moduleRequest; 215 } 216} 217 218export class NamespaceImportEntry { 219 public localName: string; 220 public moduleRequest: number; 221 222 constructor(localName: string, moduleRequest: number) { 223 this.localName = localName; 224 this.moduleRequest = moduleRequest; 225 } 226} 227 228export class LocalExportEntry { 229 public localName: string; 230 public exportName: string; 231 232 constructor(localName: string, exportName: string) { 233 this.localName = localName; 234 this.exportName = exportName; 235 } 236} 237 238export class IndirectExportEntry { 239 public exportName: string; 240 public importName: string; 241 public moduleRequest: number; 242 243 constructor(exportName: string, importName: string, moduleRequest: number) { 244 this.exportName = exportName; 245 this.importName = importName; 246 this.moduleRequest = moduleRequest; 247 } 248} 249 250export class ModuleRecord { 251 public moduleName: string = ""; 252 public moduleRequests: Array<string> = []; 253 public regularImportEntries: Array<RegularImportEntry> = []; 254 public namespaceImportEntries: Array<NamespaceImportEntry> = []; 255 public localExportEntries: Array<LocalExportEntry> = []; 256 public indirectExportEntries: Array<IndirectExportEntry> = []; 257 public starExportEntries: Array<number> = []; 258} 259 260export interface Emmiter { 261 generate_program: (filename: string, program: Program) => string; 262} 263