Skip to content

Commit ef2ab2c

Browse files
committed
Add ModuleInst and FIR create code.
1 parent e911d76 commit ef2ab2c

File tree

10 files changed

+901
-204
lines changed

10 files changed

+901
-204
lines changed

compiler/generator/c/c_code_container.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ void CCodeContainer::produceClass()
257257
*fOut << "#define exp10f __exp10f" << endl;
258258
*fOut << "#define exp10 __exp10" << endl;
259259
*fOut << "#endif" << endl;
260-
260+
261261
*fOut << "static inline int max_i(int a, int b) { return (a > b) ? a : b; }" << endl;
262262
*fOut << "static inline int min_i(int a, int b) { return (a < b) ? a : b; }" << endl;
263263

@@ -652,7 +652,7 @@ void CScalarCodeContainer1::produceClass()
652652
*fOut << "#define exp10f __exp10f" << endl;
653653
*fOut << "#define exp10 __exp10" << endl;
654654
*fOut << "#endif" << endl;
655-
655+
656656
*fOut << "static inline int max_i(int a, int b) { return (a > b) ? a : b; }" << endl;
657657
*fOut << "static inline int min_i(int a, int b) { return (a < b) ? a : b; }" << endl;
658658

compiler/generator/c/c_code_container.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class CCodeContainer : public virtual CodeContainer {
9898
tab(n, *fOut);
9999
}
100100
}
101-
101+
102102
void printMathHeader()
103103
{
104104
// For mathematical functions

compiler/generator/c/c_instructions.hh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,29 @@ class CInstVisitor : public TextInstVisitor {
454454
}
455455
}
456456

457+
virtual void visit(ModuleInst* inst)
458+
{
459+
*fOut << "typedef struct {";
460+
fTab++;
461+
tab(fTab, *fOut);
462+
inst->fDSPStruct->accept(this);
463+
tab(fTab, *fOut);
464+
back(1, *fOut);
465+
*fOut << "} " << inst->fName << ";";
466+
tab(fTab, *fOut);
467+
// TODO
468+
// fTab--;
469+
inst->fGlobals->accept(this);
470+
tab(fTab, *fOut);
471+
472+
for (const auto& it : inst->fFunctions) {
473+
tab(fTab, *fOut);
474+
it->accept(this);
475+
}
476+
fTab--;
477+
tab(fTab, *fOut);
478+
}
479+
457480
static void cleanup() { gFunctionSymbolTable.clear(); }
458481
};
459482

compiler/generator/cpp/cpp_instructions.hh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,26 @@ class CPPInstVisitor : public TextInstVisitor {
514514
TextInstVisitor::visit(inst);
515515
}
516516

517+
virtual void visit(ModuleInst* inst)
518+
{
519+
*fOut << "class " << inst->fName << " : public dsp {";
520+
fTab++;
521+
tab(fTab, *fOut);
522+
tab(fTab, *fOut);
523+
inst->fDSPStruct->accept(this);
524+
tab(fTab, *fOut);
525+
inst->fGlobals->accept(this);
526+
for (const auto& it : inst->fFunctions) {
527+
tab(fTab, *fOut);
528+
it->accept(this);
529+
}
530+
fTab--;
531+
back(1, *fOut);
532+
tab(fTab, *fOut);
533+
*fOut << "}";
534+
tab(fTab, *fOut);
535+
}
536+
517537
static void cleanup() { gFunctionSymbolTable.clear(); }
518538
};
519539

compiler/generator/fir/fir_instructions.hh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,29 @@ class FIRInstVisitor : public InstVisitor, public CStringTypeManager {
699699
tab(fTab, *fOut);
700700
}
701701

702+
virtual void visit(ModuleInst* inst)
703+
{
704+
*fOut << "ModuleInst ";
705+
*fOut << inst->fName;
706+
tab(fTab, *fOut);
707+
inst->fDSPStruct->accept(this);
708+
tab(fTab, *fOut);
709+
inst->fGlobals->accept(this);
710+
if (inst->fFunctions.size() > 0) {
711+
fTab++;
712+
tab(fTab, *fOut);
713+
for (const auto& it : inst->fFunctions) {
714+
it->accept(this);
715+
}
716+
fTab--;
717+
back(1, *fOut);
718+
} else {
719+
tab(fTab, *fOut);
720+
}
721+
*fOut << "EndModuleInst";
722+
tab(fTab, *fOut);
723+
}
724+
702725
virtual void visit(::SwitchInst* inst)
703726
{
704727
*fOut << "SwitchInst ";

compiler/generator/instructions.hh

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ struct DropInst;
147147
// Null statement
148148
struct NullStatementInst;
149149

150+
// FIR module
151+
struct ModuleInst;
152+
150153
// ======
151154
// Types
152155
// ======
@@ -336,6 +339,9 @@ struct InstVisitor : public virtual Garbageable {
336339
// Block
337340
virtual void visit(BlockInst* inst) {}
338341

342+
// Module
343+
virtual void visit(ModuleInst* inst) {}
344+
339345
// Addresses
340346
virtual void visit(NamedAddress* address) {}
341347
virtual void visit(IndexedAddress* address) {}
@@ -418,6 +424,9 @@ struct CloneVisitor : public virtual Garbageable {
418424
// Block
419425
virtual StatementInst* visit(BlockInst* inst) = 0;
420426

427+
// Module
428+
virtual StatementInst* visit(ModuleInst* inst) = 0;
429+
421430
// User interface
422431
virtual StatementInst* visit(AddMetaDeclareInst* inst) = 0;
423432
virtual StatementInst* visit(OpenboxInst* inst) = 0;
@@ -1029,6 +1038,35 @@ struct BlockInst : public StatementInst {
10291038
ValueInst* getReturnValue();
10301039
};
10311040

1041+
// ===========
1042+
// FIR module
1043+
// ===========
1044+
1045+
struct ModuleInst : public StatementInst {
1046+
std::string fName;
1047+
BlockInst* fDSPStruct;
1048+
BlockInst* fGlobals;
1049+
std::list<StatementInst*> fFunctions;
1050+
1051+
ModuleInst(const std::string& name, BlockInst* dsp_struct, BlockInst* globals,
1052+
const std::list<StatementInst*>& functions = {})
1053+
: fName(name), fDSPStruct(dsp_struct), fGlobals(globals), fFunctions(functions)
1054+
{
1055+
}
1056+
1057+
void pushFunction(StatementInst* fun)
1058+
{
1059+
faustassert(fun);
1060+
fFunctions.push_back(fun);
1061+
}
1062+
1063+
std::string getName() { return fName; };
1064+
1065+
void accept(InstVisitor* visitor) { visitor->visit(this); }
1066+
1067+
StatementInst* clone(CloneVisitor* cloner) { return cloner->visit(this); }
1068+
};
1069+
10321070
// =============
10331071
// Declarations
10341072
// =============
@@ -1937,6 +1975,18 @@ class BasicCloneVisitor : public CloneVisitor {
19371975
return cloned;
19381976
}
19391977

1978+
// Module
1979+
virtual StatementInst* visit(ModuleInst* inst)
1980+
{
1981+
ModuleInst* cloned =
1982+
new ModuleInst(inst->fName, static_cast<BlockInst*>(inst->fDSPStruct->clone(this)),
1983+
static_cast<BlockInst*>(inst->fGlobals->clone(this)));
1984+
for (const auto& it : inst->fFunctions) {
1985+
cloned->pushFunction(it->clone(this));
1986+
}
1987+
return cloned;
1988+
}
1989+
19401990
// User interface
19411991
virtual StatementInst* visit(AddMetaDeclareInst* inst)
19421992
{
@@ -2150,6 +2200,15 @@ struct DispatchVisitor : public InstVisitor {
21502200
}
21512201
}
21522202

2203+
virtual void visit(ModuleInst* inst)
2204+
{
2205+
inst->fDSPStruct->accept(this);
2206+
inst->fGlobals->accept(this);
2207+
for (const auto& it : inst->fFunctions) {
2208+
it->accept(this);
2209+
}
2210+
}
2211+
21532212
// Typed
21542213
virtual void visit(FunTyped* typed)
21552214
{
@@ -2585,6 +2644,13 @@ struct IB {
25852644
}
25862645
static BlockInst* genBlockInst() { return new BlockInst(); }
25872646

2647+
static ModuleInst* genModuleInst(const std::string& name, BlockInst* dsp_struct,
2648+
BlockInst* globals,
2649+
const std::list<StatementInst*>& functions = {})
2650+
{
2651+
return new ModuleInst(name, dsp_struct, globals, functions);
2652+
}
2653+
25882654
// Types
25892655
static BasicTyped* genBasicTyped(Typed::VarType type); // moved in instructions.cpp
25902656

@@ -3168,6 +3234,158 @@ struct IB {
31683234
}
31693235
return genDeclareFunInst(name, genFunTyped(named_args, genBasicTyped(rtype)));
31703236
}
3237+
3238+
static Names genMethod(const std::string& obj, bool ismethod)
3239+
{
3240+
Names args;
3241+
if (!ismethod) {
3242+
args.push_back(IB::genNamedTyped(obj, Typed::kObj_ptr));
3243+
}
3244+
return args;
3245+
}
3246+
3247+
static Values genObjArg(const std::string& obj, bool ismethod)
3248+
{
3249+
Values args;
3250+
if (!ismethod) {
3251+
args.push_back(genLoadFunArgsVar(obj));
3252+
}
3253+
return args;
3254+
}
3255+
3256+
static DeclareFunInst* generateGetIO(const std::string& name, const std::string& obj, int io,
3257+
bool ismethod, FunTyped::FunAttribute funtype)
3258+
{
3259+
Names args = genMethod(obj, ismethod);
3260+
BlockInst* block = genBlockInst();
3261+
block->pushBackInst(genRetInst(genInt32NumInst(io)));
3262+
3263+
// Creates function
3264+
FunTyped* fun_type = genFunTyped(args, genInt32Typed(), funtype);
3265+
return genDeclareFunInst(name, fun_type, block);
3266+
}
3267+
3268+
static DeclareFunInst* generateGetInputs(const std::string& name, const std::string& obj,
3269+
bool ismethod, FunTyped::FunAttribute funtype,
3270+
int inputs)
3271+
{
3272+
return generateGetIO(name, obj, inputs, ismethod, funtype);
3273+
}
3274+
3275+
static DeclareFunInst* generateGetOutputs(const std::string& name, const std::string& obj,
3276+
bool ismethod, FunTyped::FunAttribute funtype,
3277+
int outputs)
3278+
{
3279+
return generateGetIO(name, obj, outputs, ismethod, funtype);
3280+
}
3281+
3282+
static DeclareFunInst* generateGetSampleRate(const std::string& name, const std::string& obj,
3283+
bool ismethod, bool isvirtual)
3284+
{
3285+
Names args = genMethod(obj, ismethod);
3286+
3287+
BlockInst* block = genBlockInst();
3288+
block->pushBackInst(genRetInst(genLoadStructVar("fSampleRate")));
3289+
3290+
// Creates function
3291+
FunTyped* fun_type = genFunTyped(args, genInt32Typed(),
3292+
(isvirtual) ? FunTyped::kVirtual : FunTyped::kDefault);
3293+
return genDeclareFunInst(name, fun_type, block);
3294+
}
3295+
3296+
static DeclareFunInst* generateInit(const std::string& name, const std::string& obj,
3297+
bool ismethod, bool isvirtual)
3298+
{
3299+
Names args = genMethod(obj, ismethod);
3300+
args.push_back(genNamedTyped("sample_rate", Typed::kInt32));
3301+
3302+
BlockInst* block = genBlockInst();
3303+
{
3304+
Values args1 = genObjArg(obj, ismethod);
3305+
args1.push_back(genLoadFunArgsVar("sample_rate"));
3306+
block->pushBackInst(genVoidFunCallInst("classInit", args1));
3307+
}
3308+
3309+
{
3310+
Values args1 = genObjArg(obj, ismethod);
3311+
args1.push_back(genLoadFunArgsVar("sample_rate"));
3312+
block->pushBackInst(genVoidFunCallInst("instanceInit", args1));
3313+
}
3314+
3315+
// Creates function
3316+
return genVoidFunction(name, args, block, isvirtual);
3317+
}
3318+
3319+
static DeclareFunInst* generateInstanceInit(const std::string& name, const std::string& obj,
3320+
bool ismethod, bool isvirtual)
3321+
{
3322+
Names args = genMethod(obj, ismethod);
3323+
args.push_back(genNamedTyped("sample_rate", Typed::kInt32));
3324+
3325+
BlockInst* block = genBlockInst();
3326+
{
3327+
Values args1 = genObjArg(obj, ismethod);
3328+
args1.push_back(genLoadFunArgsVar("sample_rate"));
3329+
block->pushBackInst(genVoidFunCallInst("instanceConstants", args1));
3330+
}
3331+
3332+
{
3333+
Values args1 = genObjArg(obj, ismethod);
3334+
block->pushBackInst(genVoidFunCallInst("instanceResetUserInterface", args1));
3335+
}
3336+
3337+
{
3338+
Values args1 = genObjArg(obj, ismethod);
3339+
block->pushBackInst(genVoidFunCallInst("instanceClear", args1));
3340+
}
3341+
3342+
// Creates function
3343+
return genVoidFunction(name, args, block, isvirtual);
3344+
}
3345+
3346+
static DeclareFunInst* generateInstanceResetUserInterface(const std::string& name,
3347+
const std::string& obj, bool ismethod,
3348+
bool isvirtual,
3349+
BlockInst* reset_block)
3350+
{
3351+
Names args;
3352+
if (!ismethod) {
3353+
args.push_back(genNamedTyped(obj, Typed::kObj_ptr));
3354+
}
3355+
3356+
// Creates function
3357+
FunTyped* fun_type = IB::genFunTyped(args, genVoidTyped(), FunTyped::kDefault);
3358+
return genDeclareFunInst(name, fun_type, reset_block);
3359+
}
3360+
3361+
static DeclareFunInst* generateInstanceClear(const std::string& name, const std::string& obj,
3362+
bool ismethod, bool isvirtual);
3363+
3364+
static DeclareFunInst* generateInstanceClear(const std::string& name, const std::string& obj,
3365+
bool ismethod, bool isvirtual,
3366+
BlockInst* clear_block)
3367+
{
3368+
Names args = genMethod(obj, ismethod);
3369+
3370+
// Creates function
3371+
return IB::genVoidFunction(name, args, clear_block, isvirtual);
3372+
}
3373+
3374+
static DeclareFunInst* generateComputeFun(const std::string& name, const std::string& obj,
3375+
bool ismethod, bool isvirtual,
3376+
BlockInst* compute_block)
3377+
{
3378+
Names args = genMethod(obj, ismethod);
3379+
args.push_back(genNamedTyped("count", Typed::kInt32));
3380+
args.push_back(genNamedTyped("inputs", Typed::kFloatMacro_ptr_ptr));
3381+
args.push_back(genNamedTyped("outputs", Typed::kFloatMacro_ptr_ptr));
3382+
3383+
// Explicit return
3384+
compute_block->pushBackInst(genRetInst());
3385+
3386+
// Creates function
3387+
return IB::genVoidFunction(name, args, compute_block, isvirtual);
3388+
}
31713389
};
31723390

31733391
/*

0 commit comments

Comments
 (0)