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 16 #ifndef ES2PANDA_COMPILER_IR_EMITTER_H 17 #define ES2PANDA_COMPILER_IR_EMITTER_H 18 19 #include <assembly-literals.h> 20 #include <compiler/core/emitter/moduleRecordEmitter.h> 21 #include <ir/astNode.h> 22 #include <lexer/token/sourceLocation.h> 23 #include <macros.h> 24 #include <typescript/extractor/typeRecorder.h> 25 #include <util/patchFix.h> 26 #include <util/ustring.h> 27 28 #include <list> 29 #include <mutex> 30 #include <string> 31 #include <unordered_map> 32 #include <unordered_set> 33 #include <vector> 34 35 namespace panda::pandasm { 36 struct Program; 37 struct Function; 38 struct Ins; 39 struct Record; 40 } // namespace panda::pandasm 41 42 namespace panda::es2panda::ir { 43 class Statement; 44 } // namespace panda::es2panda::ir 45 46 namespace panda::es2panda::binder { 47 class Scope; 48 } // namespace panda::es2panda::binder 49 50 namespace panda::es2panda::compiler { 51 class PandaGen; 52 class LiteralBuffer; 53 class DebugInfo; 54 class Label; 55 class IRNode; 56 class CompilerContext; 57 58 using Literal = panda::pandasm::LiteralArray::Literal; 59 60 class FunctionEmitter { 61 public: 62 explicit FunctionEmitter(ArenaAllocator *allocator, const PandaGen *pg); 63 ~FunctionEmitter() = default; 64 NO_COPY_SEMANTIC(FunctionEmitter); 65 NO_MOVE_SEMANTIC(FunctionEmitter); 66 Function()67 panda::pandasm::Function *Function() 68 { 69 return func_; 70 } 71 LiteralBuffers()72 auto &LiteralBuffers() 73 { 74 return literalBuffers_; 75 } 76 77 void Generate(util::PatchFix *patchFixHelper); 78 const ArenaSet<util::StringView> &Strings() const; 79 80 private: 81 void GenInstructionDebugInfo(const IRNode *ins, panda::pandasm::Ins *pandaIns); 82 void GenFunctionInstructions(); 83 void GenFunctionCatchTables(); 84 void GenScopeVariableInfo(const binder::Scope *scope); 85 void GenSourceFileDebugInfo(); 86 void GenVariablesDebugInfo(); 87 void GenFunctionKind(); 88 void GenIcSize(); 89 util::StringView SourceCode() const; 90 lexer::LineIndex &GetLineIndex() const; 91 92 void GenLiteralBuffers(); 93 void GenBufferLiterals(const LiteralBuffer *buff); 94 void GenFunctionSource(); 95 96 const PandaGen *pg_; 97 panda::pandasm::Function *func_ {}; 98 ArenaVector<std::pair<int32_t, std::vector<Literal>>> literalBuffers_; 99 size_t offset_ {0}; 100 }; 101 102 class Emitter { 103 public: 104 explicit Emitter(const CompilerContext *context); 105 ~Emitter(); 106 NO_COPY_SEMANTIC(Emitter); 107 NO_MOVE_SEMANTIC(Emitter); 108 109 void AddFunction(FunctionEmitter *func, CompilerContext *context); 110 void AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerContext *context); 111 void FillTypeInfoRecord(CompilerContext *context, bool typeFlag, int64_t typeSummaryIndex, 112 const std::string &recordName) const; 113 void FillTypeLiteralBuffers(const extractor::TypeRecorder *recorder) const; 114 static void GenBufferLiterals(ArenaVector<std::pair<int32_t, std::vector<Literal>>> &literalBuffers, 115 const LiteralBuffer *buff); 116 static void DumpAsm(const panda::pandasm::Program *prog); 117 panda::pandasm::Program *Finalize(bool dumpDebugInfo, util::PatchFix *patchFixHelper); 118 panda::pandasm::Program *GetProgram() const; 119 void GenJsonContentRecord(const CompilerContext *context); 120 void GenRecordNameInfo() const; 121 void GenTypeInfoRecord() const; GetEmitterLock()122 std::mutex &GetEmitterLock() 123 { 124 return m_; 125 }; 126 127 private: 128 void SetCommonjsField(bool isCommonjs); 129 void SetPkgNameField(std::string pkgName); 130 void GenCommonjsRecord() const; 131 void GenESTypeAnnotationRecord() const; 132 133 std::mutex m_; 134 panda::pandasm::Program *prog_; 135 panda::pandasm::Record *rec_; 136 }; 137 } // namespace panda::es2panda::compiler 138 139 #endif 140