Skip to content

Commit 5391e56

Browse files
committed
wasm codegen init
1 parent 9388d3a commit 5391e56

6 files changed

Lines changed: 2023 additions & 1 deletion

File tree

cparser/include/c_ast.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,15 @@ class ASTNodeExprUnaryOp : public ASTNodeExpr
425425
this->contain(expr);
426426
}
427427

428+
inline UnaryOperatorType get_operator() const
429+
{
430+
return m_operator;
431+
}
432+
inline std::shared_ptr<ASTNodeExpr> get_expr() const
433+
{
434+
return m_expr;
435+
}
436+
428437
virtual void check_constraints(std::shared_ptr<SemanticReporter> reporter) override;
429438
virtual std::optional<long long> get_integer_constant() const override;
430439
virtual std::optional<long double> get_float_constant() const override;
@@ -443,6 +452,11 @@ class ASTNodeExprSizeof : public ASTNodeExpr
443452
this->contain(type);
444453
}
445454

455+
inline std::shared_ptr<ASTNodeVariableType> get_type() const
456+
{
457+
return m_type;
458+
}
459+
446460
virtual void check_constraints(std::shared_ptr<SemanticReporter> reporter) override;
447461
virtual std::optional<long long> get_integer_constant() const override;
448462
virtual std::optional<long double> get_float_constant() const override;
@@ -464,6 +478,15 @@ class ASTNodeExprCast : public ASTNodeExpr
464478
this->contain(type, expr);
465479
}
466480

481+
inline std::shared_ptr<ASTNodeVariableType> get_type() const
482+
{
483+
return m_type;
484+
}
485+
inline std::shared_ptr<ASTNodeExpr> get_expr() const
486+
{
487+
return m_expr;
488+
}
489+
467490
virtual void check_constraints(std::shared_ptr<SemanticReporter> reporter) override;
468491
virtual std::optional<long long> get_integer_constant() const override;
469492
virtual std::optional<long double> get_float_constant() const override;
@@ -529,6 +552,11 @@ class ASTNodeExprBinaryOp : public ASTNodeExpr
529552
return this->m_right;
530553
}
531554

555+
inline BinaryOperatorType get_operator() const
556+
{
557+
return m_operator;
558+
}
559+
532560
virtual void check_constraints(std::shared_ptr<SemanticReporter> reporter) override;
533561
virtual std::optional<long long> get_integer_constant() const override;
534562
virtual std::optional<long double> get_float_constant() const override;
@@ -553,6 +581,19 @@ class ASTNodeExprConditional : public ASTNodeExpr
553581
this->contain(cond, true_expr, false_expr);
554582
}
555583

584+
inline std::shared_ptr<ASTNodeExpr> get_cond() const
585+
{
586+
return m_cond;
587+
}
588+
inline std::shared_ptr<ASTNodeExpr> get_true() const
589+
{
590+
return m_true;
591+
}
592+
inline std::shared_ptr<ASTNodeExpr> get_false() const
593+
{
594+
return m_false;
595+
}
596+
556597
virtual void check_constraints(std::shared_ptr<SemanticReporter> reporter) override;
557598
virtual std::optional<long long> get_integer_constant() const override;
558599
virtual std::optional<long double> get_float_constant() const override;
@@ -2159,6 +2200,10 @@ class ASTNodeFunctionDefinition : public ASTNodeExternalDeclaration
21592200
this->contain(funcid, decl, body);
21602201
}
21612202

2203+
inline std::shared_ptr<TokenID> get_id() const
2204+
{
2205+
return _id;
2206+
}
21622207
inline std::shared_ptr<ASTNodeVariableTypeFunction> decl()
21632208
{
21642209
return this->_decl;

cparser/include/wasm_codegen.h

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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

Comments
 (0)