1 // =================================
 2 // Copyright (c) 2021 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 #include <cmajor/symbols/Trap.hpp>
 7 #include <cmajor/symbols/SymbolTable.hpp>
 8 
 9 namespace cmajor { namespace symbols {
10 
11 TrapFunction::TrapFunction(SymbolTable& symbolTable) : FunctionSymbol(SymbolType::trapSpan()boost::uuids::nil_uuid()U"trap")
12 {
13     SetGroupName(U"trap");
14     SetCDecl();
15     SetVarArg();
16     SetAccess(SymbolAccess::public_);
17     ParameterSymbol* b0Param = new ParameterSymbol(Span()boost::uuids::nil_uuid()U"b0");
18     b0Param->SetType(symbolTable.GetTypeByName(U"byte"));
19     AddMember(b0Param);
20     ParameterSymbol* b1Param = new ParameterSymbol(Span()boost::uuids::nil_uuid()U"b1");
21     b1Param->SetType(symbolTable.GetTypeByName(U"byte"));
22     AddMember(b1Param);
23     ParameterSymbol* b2Param = new ParameterSymbol(Span()boost::uuids::nil_uuid()U"b2");
24     b2Param->SetType(symbolTable.GetTypeByName(U"byte"));
25     AddMember(b2Param);
26     SetReturnType(symbolTable.GetTypeByName(U"long"));
27 }
28 
29 TrapFunction::TrapFunction(const Span& span_const boost::uuids::uuid& sourceModuleId_const std::u32string& name_) : FunctionSymbol(SymbolType::trapspan_sourceModuleId_name_)
30 {
31 }
32 
33 void TrapFunction::GenerateCall(Emitter& emitterstd::std::vector<GenObject*>&genObjectsOperationFlagsflagsconstSpan&spanconstboost::uuids::uuid& moduleId)
34 {
35     int na = genObjects.size();
36     for (int i = 0; i < na; ++i)
37     {
38         GenObject* genObject = genObjects[i];
39         genObject->Load(emitterflags & OperationFlags::functionCallFlags);
40     }
41     std::vector<void*> args;
42     args.resize(na);
43     for (int i = 0; i < na; ++i)
44     {
45         void* arg = emitter.Stack().Pop();
46         args[na - i - 1] = arg;
47     }
48     emitter.Stack().Push(emitter.GenerateTrap(args));
49 }
50 
51 void InitTrap(SymbolTable& symbolTable)
52 {
53     symbolTable.AddFunctionSymbolToGlobalScope(new TrapFunction(symbolTable));
54 }
55 
56 } } // namespace cmajor::symbols