Page Menu
Home
Xreate
Search
Configure Global Search
Log In
Docs
Questions
Repository
Issues
Patches
Internal API
Files
F3999468
logging.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Jul 9, 2:21 AM
Size
2 KB
Mime Type
text/x-c++
Expires
Sat, Jul 11, 2:21 AM (1 d, 7 h)
Engine
blob
Format
Raw Data
Handle
273107
Attached To
rXR Xreate
logging.cpp
View Options
/*
* logging.cpp
*
* Created on: Jun 23, 2015
* Author: pgess
*/
#include "logging.h"
#include "instructions/instr-containers.h"
#include "utils.h"
using namespace std;
using namespace llvm;
namespace xreate {
void Logging::init(ClaspLayer* clasp){
auto model = clasp->query(Config::get("logging.id"));
if(!model) return;
for (ClaspLayer::ModelIterator rec = model->first; rec!=model->second; ++rec){
std::tuple<SymbolPacked> _v =ClaspLayer::parse<SymbolPacked>(rec->second);
Symbol v = clasp->unpack(get<0>(_v));
SymbolAttachments::put<IsLogging>(v, true);
}
}
void
Logging::process(const Expression& expression, PassContext context, const std::string& varDecl){
if (varDecl.size()){
Symbol v = context.scope->findSymbol(varDecl);
if (SymbolAttachments::get<IsLogging>(v, false)){
CompilePass::FunctionUnit* f = compiler->getFunctionUnit(CompilePass::FunctionQuery(string(context.function->getName())));
CompilePass::CodeScopeUnit* u = f->getScopeUnit(context.scope);
CompilePass::Context compilation{f, u, compiler};
inject(v, compilation);
}
}
return AbstractPass<void>::process(expression, context, varDecl);
}
void Logging::inject(const Symbol& symbol, const CompilePass::Context& context){
llvm::Value* source = context.scope->compileSymbol(symbol);
ExpandedType typSource = man->root->expandType(CodeScope::findDefinition(symbol));
string format = "";
switch (typSource->__value) {
case TypePrimitive::Int : case TypePrimitive::Num : case TypePrimitive::I32: case TypePrimitive::I8:
format = "%d\n";
break;
case TypePrimitive::String:
format = "%s\n";
break;
default:
assert(false && "No appropriate type for logging");
}
containers::Instructions compiler(context);
LLVMLayer* llvm = context.pass->man->llvm;
llvm->builder.SetInsertPoint(*source->use_begin());
llvm::Value* formatRaw = compiler.compileConstantStringAsPChar(format, "logformat");
llvm->builder.CreateCall2(refPrintf, formatRaw, source);
}
void
Logging::initOutput(){
LLVMLayer* llvm = man->llvm;
refPrintf = llvm->module->getFunction("printf");
if (!refPrintf) {
PointerType* typPtrI8 = PointerType::get(IntegerType::get(llvm->module->getContext(), 8), 0);
std::vector<Type*>argsPrintf{typPtrI8};
FunctionType* signaturePrintf = FunctionType::get(
/*Result=*/IntegerType::get(llvm->module->getContext(), 32),
/*Params=*/argsPrintf,
/*isVarArg=*/true);
refPrintf = llvm::Function::Create(
/*Type=*/signaturePrintf,
/*Linkage=*/GlobalValue::ExternalLinkage,
/*Name=*/"printf", llvm->module); // (external, no body)
refPrintf->setCallingConv(CallingConv::C);
}
}
Logging::Logging(PassManager* manager)
: AbstractPass<void>(manager)
{
initOutput();
init(man->clasp);
}
void
Logging::initDependencies(CompilePass* pass){
compiler = pass;
}
} /* namespace xreate */
Event Timeline
Log In to Comment