|
| 1 | +#ifndef _WASM_CODEGEN_H_ |
| 2 | +#define _WASM_CODEGEN_H_ |
| 3 | + |
| 4 | +#include "c_ast.h" |
| 5 | +#include <memory> |
| 6 | +#include <sstream> |
| 7 | +#include <string> |
| 8 | +#include <unordered_map> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +namespace cparser { |
| 12 | + |
| 13 | +// WebAssembly type representation |
| 14 | +enum class WasmType |
| 15 | +{ |
| 16 | + I32, |
| 17 | + I64, |
| 18 | + F32, |
| 19 | + F64, |
| 20 | + VOID |
| 21 | +}; |
| 22 | + |
| 23 | +// WebAssembly instruction representation |
| 24 | +struct WasmInstruction |
| 25 | +{ |
| 26 | + std::string opcode; |
| 27 | + std::vector<std::string> operands; |
| 28 | + |
| 29 | + WasmInstruction(const std::string& op) : opcode(op) |
| 30 | + {} |
| 31 | + WasmInstruction(const std::string& op, const std::vector<std::string>& ops) |
| 32 | + : opcode(op), operands(ops) |
| 33 | + {} |
| 34 | + |
| 35 | + std::string toString() const; |
| 36 | +}; |
| 37 | + |
| 38 | +// Local variable information |
| 39 | +struct LocalVariable |
| 40 | +{ |
| 41 | + std::string name; |
| 42 | + WasmType type; |
| 43 | + size_t index; |
| 44 | + size_t size; |
| 45 | + |
| 46 | + LocalVariable() : name(""), type(WasmType::I32), index(0), size(0) |
| 47 | + {} |
| 48 | + LocalVariable(const std::string& n, WasmType t, size_t idx, size_t sz = 0) |
| 49 | + : name(n), type(t), index(idx), size(sz) |
| 50 | + {} |
| 51 | +}; |
| 52 | + |
| 53 | +// Function information |
| 54 | +struct WasmFunction |
| 55 | +{ |
| 56 | + std::string name; |
| 57 | + std::vector<WasmType> params; |
| 58 | + std::vector<std::string> paramNames; |
| 59 | + WasmType returnType; |
| 60 | + std::vector<LocalVariable> locals; |
| 61 | + std::vector<WasmInstruction> instructions; |
| 62 | + size_t localCount; |
| 63 | + |
| 64 | + WasmFunction(const std::string& n) : name(n), returnType(WasmType::VOID), localCount(0) |
| 65 | + {} |
| 66 | + |
| 67 | + std::string toString() const; |
| 68 | +}; |
| 69 | + |
| 70 | +// Memory layout information |
| 71 | +struct MemoryLayout |
| 72 | +{ |
| 73 | + std::unordered_map<std::string, size_t> globalOffsets; |
| 74 | + std::unordered_map<std::string, size_t> stringOffsets; |
| 75 | + size_t currentOffset; |
| 76 | + |
| 77 | + MemoryLayout() : currentOffset(0) |
| 78 | + {} |
| 79 | +}; |
| 80 | + |
| 81 | +class WasmCodeGenerator |
| 82 | +{ |
| 83 | + private: |
| 84 | + std::vector<WasmFunction> functions; |
| 85 | + std::vector<std::string> globals; |
| 86 | + std::vector<std::string> dataSegments; |
| 87 | + MemoryLayout memoryLayout; |
| 88 | + WasmFunction* currentFunction; |
| 89 | + std::unordered_map<std::string, LocalVariable> currentLocals; |
| 90 | + size_t labelCounter; |
| 91 | + size_t memorySize; |
| 92 | + |
| 93 | + // Type mapping utilities |
| 94 | + WasmType mapCTypeToWasm(std::shared_ptr<ASTNodeVariableType> type); |
| 95 | + size_t getTypeSize(std::shared_ptr<ASTNodeVariableType> type); |
| 96 | + bool isIntegerType(std::shared_ptr<ASTNodeVariableType> type); |
| 97 | + bool isFloatType(std::shared_ptr<ASTNodeVariableType> type); |
| 98 | + bool isPointerType(std::shared_ptr<ASTNodeVariableType> type); |
| 99 | + |
| 100 | + // Code generation utilities |
| 101 | + void emitInstruction(const std::string& opcode); |
| 102 | + void emitInstruction(const std::string& opcode, const std::string& operand); |
| 103 | + void emitInstruction(const std::string& opcode, const std::vector<std::string>& operands); |
| 104 | + std::string generateLabel(); |
| 105 | + |
| 106 | + // Memory management |
| 107 | + size_t allocateGlobalMemory(const std::string& name, size_t size); |
| 108 | + size_t allocateStringLiteral(const std::string& str); |
| 109 | + |
| 110 | + // Local variable management |
| 111 | + size_t allocateLocal(const std::string& name, WasmType type, size_t size = 0); |
| 112 | + LocalVariable* findLocal(const std::string& name); |
| 113 | + |
| 114 | + // Expression generation |
| 115 | + void generateExpression(std::shared_ptr<ASTNodeExpr> expr); |
| 116 | + void generateIdentifierExpr(std::shared_ptr<ASTNodeExprIdentifier> expr); |
| 117 | + void generateIntegerExpr(std::shared_ptr<ASTNodeExprInteger> expr); |
| 118 | + void generateFloatExpr(std::shared_ptr<ASTNodeExprFloat> expr); |
| 119 | + void generateStringExpr(std::shared_ptr<ASTNodeExprString> expr); |
| 120 | + void generateBinaryOpExpr(std::shared_ptr<ASTNodeExprBinaryOp> expr); |
| 121 | + void generateUnaryOpExpr(std::shared_ptr<ASTNodeExprUnaryOp> expr); |
| 122 | + void generateFunctionCallExpr(std::shared_ptr<ASTNodeExprFunctionCall> expr); |
| 123 | + void generateIndexingExpr(std::shared_ptr<ASTNodeExprIndexing> expr); |
| 124 | + void generateMemberAccessExpr(std::shared_ptr<ASTNodeExprMemberAccess> expr); |
| 125 | + void generatePointerMemberAccessExpr(std::shared_ptr<ASTNodeExprPointerMemberAccess> expr); |
| 126 | + void generateCastExpr(std::shared_ptr<ASTNodeExprCast> expr); |
| 127 | + void generateSizeofExpr(std::shared_ptr<ASTNodeExprSizeof> expr); |
| 128 | + void generateConditionalExpr(std::shared_ptr<ASTNodeExprConditional> expr); |
| 129 | + |
| 130 | + // Statement generation |
| 131 | + void generateStatement(std::shared_ptr<ASTNodeStat> stmt); |
| 132 | + void generateCompoundStatement(std::shared_ptr<ASTNodeStatCompound> stmt); |
| 133 | + void generateExpressionStatement(std::shared_ptr<ASTNodeStatExpr> stmt); |
| 134 | + void generateIfStatement(std::shared_ptr<ASTNodeStatIF> stmt); |
| 135 | + void generateForStatement(std::shared_ptr<ASTNodeStatFor> stmt); |
| 136 | + void generateForDeclStatement(std::shared_ptr<ASTNodeStatForDecl> stmt); |
| 137 | + void generateDoWhileStatement(std::shared_ptr<ASTNodeStatDoWhile> stmt); |
| 138 | + void generateSwitchStatement(std::shared_ptr<ASTNodeStatSwitch> stmt); |
| 139 | + void generateReturnStatement(std::shared_ptr<ASTNodeStatReturn> stmt); |
| 140 | + void generateBreakStatement(std::shared_ptr<ASTNodeStatBreak> stmt); |
| 141 | + void generateContinueStatement(std::shared_ptr<ASTNodeStatContinue> stmt); |
| 142 | + void generateGotoStatement(std::shared_ptr<ASTNodeStatGoto> stmt); |
| 143 | + void generateLabelStatement(std::shared_ptr<ASTNodeStatLabel> stmt); |
| 144 | + void generateCaseStatement(std::shared_ptr<ASTNodeStatCase> stmt); |
| 145 | + void generateDefaultStatement(std::shared_ptr<ASTNodeStatDefault> stmt); |
| 146 | + |
| 147 | + // Declaration generation |
| 148 | + void generateDeclaration(std::shared_ptr<ASTNodeDeclaration> decl); |
| 149 | + void generateInitDeclarator(std::shared_ptr<ASTNodeInitDeclarator> decl); |
| 150 | + void generateFunctionDefinition(std::shared_ptr<ASTNodeFunctionDefinition> funcDef); |
| 151 | + |
| 152 | + // Type casting utilities |
| 153 | + void generateTypeConversion(WasmType fromType, WasmType toType); |
| 154 | + void generateImplicitCast(std::shared_ptr<ASTNodeVariableType> fromType, |
| 155 | + std::shared_ptr<ASTNodeVariableType> toType); |
| 156 | + |
| 157 | + // Arithmetic and logical operations |
| 158 | + void generateArithmeticOp(const std::string& op, WasmType type); |
| 159 | + void generateComparisonOp(const std::string& op, WasmType type); |
| 160 | + void generateLogicalOp(const std::string& op); |
| 161 | + void generateBitwiseOp(const std::string& op, WasmType type); |
| 162 | + |
| 163 | + // Memory operations |
| 164 | + void generateLoad(WasmType type, size_t offset = 0); |
| 165 | + void generateStore(WasmType type, size_t offset = 0); |
| 166 | + void generateMemoryAddress(std::shared_ptr<ASTNodeExpr> expr); |
| 167 | + |
| 168 | + // Control flow utilities |
| 169 | + void generateConditionalJump(const std::string& trueLabel, const std::string& falseLabel); |
| 170 | + void generateLoop(const std::string& loopLabel, const std::string& breakLabel); |
| 171 | + |
| 172 | + public: |
| 173 | + WasmCodeGenerator(); |
| 174 | + ~WasmCodeGenerator() = default; |
| 175 | + |
| 176 | + // Main generation methods |
| 177 | + std::string generateModule(std::shared_ptr<ASTNodeTranslationUnit> translationUnit); |
| 178 | + std::string generateFunction(std::shared_ptr<ASTNodeFunctionDefinition> funcDef); |
| 179 | + |
| 180 | + // Utility methods |
| 181 | + void reset(); |
| 182 | + std::string getWasmModule() const; |
| 183 | + |
| 184 | + // Error handling |
| 185 | + void reportError(const std::string& message); |
| 186 | +}; |
| 187 | + |
| 188 | +} // namespace cparser |
| 189 | + |
| 190 | +#endif // _WASM_CODEGEN_H_ |
0 commit comments