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 <util/patchFix.h> 25 #include <util/ustring.h> 26 27 #include <list> 28 #include <mutex> 29 #include <string> 30 #include <unordered_map> 31 #include <unordered_set> 32 #include <vector> 33 34 namespace panda::pandasm { 35 struct Program; 36 struct Function; 37 struct Ins; 38 struct Record; 39 } // namespace panda::pandasm 40 41 namespace panda::es2panda::ir { 42 class Statement; 43 } // namespace panda::es2panda::ir 44 45 namespace panda::es2panda::binder { 46 class Scope; 47 } // namespace panda::es2panda::binder 48 49 namespace panda::es2panda::compiler { 50 class PandaGen; 51 class LiteralBuffer; 52 class DebugInfo; 53 class Label; 54 class IRNode; 55 class CompilerContext; 56 57 using Literal = panda::pandasm::LiteralArray::Literal; 58 59 class FunctionEmitter { 60 public: 61 explicit FunctionEmitter(ArenaAllocator *allocator, const PandaGen *pg); 62 ~FunctionEmitter() = default; 63 NO_COPY_SEMANTIC(FunctionEmitter); 64 NO_MOVE_SEMANTIC(FunctionEmitter); 65 Function()66 panda::pandasm::Function *Function() 67 { 68 return func_; 69 } 70 LiteralBuffers()71 auto &LiteralBuffers() 72 { 73 return literalBuffers_; 74 } 75 76 void Generate(util::PatchFix *patchFixHelper); 77 const ArenaSet<util::StringView> &Strings() const; 78 79 private: 80 uint32_t UpdateForReturnIns(const ir::AstNode *astNode, panda::pandasm::Ins *pandaIns); 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 GenConcurrentFunctionModuleRequests(); 95 void GenConcurrentModuleRequestsAnnotation(); 96 void GenSlotNumberAnnotation(); 97 98 const PandaGen *pg_; 99 panda::pandasm::Function *func_ {}; 100 ArenaVector<std::pair<int32_t, std::vector<Literal>>> literalBuffers_; 101 size_t offset_ {0}; 102 }; 103 104 class Emitter { 105 public: 106 explicit Emitter(CompilerContext *context); 107 ~Emitter(); 108 NO_COPY_SEMANTIC(Emitter); 109 NO_MOVE_SEMANTIC(Emitter); 110 111 void AddFunction(FunctionEmitter *func, CompilerContext *context); 112 void AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerContext *context); 113 void AddScopeNamesRecord(CompilerContext *context); 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; GetEmitterLock()121 std::mutex &GetEmitterLock() 122 { 123 return m_; 124 }; 125 GetConstantLocalExportSlots()126 const std::unordered_set<uint32_t> &GetConstantLocalExportSlots() 127 { 128 return constant_local_export_slots_; 129 } 130 131 private: 132 void SetCommonjsField(bool isCommonjs); 133 void SetPkgNameField(const std::string &pkgName); 134 void GenCommonjsRecord() const; 135 void AddConcurrentModuleRequestsRecord(); 136 void AddHasTopLevelAwaitRecord(bool hasTLA, const CompilerContext *context); 137 void AddSharedModuleRecord(const CompilerContext *context); 138 void AddSlotNumberRecord(); 139 void AddModuleRequestPhaseRecord(ModuleRecordEmitter *module, CompilerContext *context); 140 141 std::mutex m_; 142 panda::pandasm::Program *prog_; 143 panda::pandasm::Record *rec_; 144 // Constant local export module variable slots. 145 // Used by the branch elimination optimization to identify initial constants. 146 // Should be replaced by analyzing the stconstmodulevar instruction in the 147 // bytecode file after it is introduced 148 std::unordered_set<uint32_t> constant_local_export_slots_; 149 }; 150 } // namespace panda::es2panda::compiler 151 152 #endif 153