26 makeFromJsonFunction = NULL;
27 getJsonFunction = NULL;
28 getInterprocessJsonFunction = NULL;
29 makeFromStringFunction = NULL;
30 getStringFunction = NULL;
31 getInterprocessStringFunction = NULL;
32 getSummaryFunction = NULL;
33 areEqualFunction = NULL;
34 isLessThanFunction = NULL;
35 retainFunction = NULL;
36 releaseFunction = NULL;
48 if (! isType(typeName,
module))
58 bool VuoCompilerType::isType(
string typeName, Module *module)
60 return (
module->getNamedValue(typeName +
"_makeFromJson") != NULL);
66 void VuoCompilerType::parse(
void)
73 getInterprocessJsonFunction =
parser->
getFunction(typeName +
"_getInterprocessJson");
78 if (! makeFromJsonFunction)
79 VUserLog(
"Error: Couldn't find %s_makeFromJson() function.", typeName.c_str());
80 if (! getJsonFunction)
81 VUserLog(
"Error: Couldn't find %s_getJson() function.", typeName.c_str());
82 if (! getSummaryFunction)
83 VUserLog(
"Error: Couldn't find %s_getSummary() function.", typeName.c_str());
87 parseOrGenerateValueFromStringFunction();
88 parseOrGenerateStringFromValueFunction(
false);
89 if (getInterprocessJsonFunction)
90 parseOrGenerateStringFromValueFunction(
true);
92 parseOrGenerateRetainOrReleaseFunction(
true);
93 parseOrGenerateRetainOrReleaseFunction(
false);
99 set<string> VuoCompilerType::globalsToRename(
void)
122 void VuoCompilerType::parseOrGenerateValueFromStringFunction(
void)
131 PointerType *pointerToCharType = PointerType::get(IntegerType::get(
module->getContext(), 8), 0);
132 FunctionType *makeFromJsonFunctionType = makeFromJsonFunction->getFunctionType();
134 Type *returnType = makeFromJsonFunctionType->getReturnType();
136 vector<Type *> functionParams;
138 functionParams.push_back(makeFromJsonFunctionType->getParamType(0));
139 functionParams.push_back(pointerToCharType);
141 FunctionType *functionType = FunctionType::get(returnType, functionParams,
false);
142 function = Function::Create(functionType, GlobalValue::ExternalLinkage, functionName,
module);
145 function->setAttributes(makeFromJsonFunction->getAttributes().getParamAttributes(1));
148 if (function->isDeclaration())
150 BasicBlock *block = BasicBlock::Create(
module->getContext(),
"",
function);
152 Function::arg_iterator args =
function->arg_begin();
153 Value *result = NULL;
157 result->setName(
"result");
165 CallInst *jsonTokenerParseReturn = CallInst::Create(jsonTokenerParseFunction, str,
"", block);
167 vector<Value *> makeFromJsonArgs;
169 makeFromJsonArgs.push_back(result);
170 makeFromJsonArgs.push_back(jsonTokenerParseReturn);
171 Value *makeFromJsonReturn = CallInst::Create(makeFromJsonFunction, makeFromJsonArgs,
"", block);
173 CallInst::Create(jsonObjectPutFunction, jsonTokenerParseReturn,
"", block);
175 Value *returnValue = (isReturnInParam ? NULL : makeFromJsonReturn);
176 ReturnInst::Create(
module->getContext(), returnValue, block);
179 makeFromStringFunction =
function;
200 void VuoCompilerType::parseOrGenerateStringFromValueFunction(
bool isInterprocess)
202 string functionName = (
getBase()->
getModuleKey() + (isInterprocess ?
"_getInterprocessString" :
"_getString")).c_str();
205 Function *chosenJsonFromValueFunction = (isInterprocess ? getInterprocessJsonFunction : getJsonFunction);
209 PointerType *pointerToCharType = PointerType::get(IntegerType::get(
module->getContext(), 8), 0);
210 FunctionType *getJsonFunctionType = chosenJsonFromValueFunction->getFunctionType();
212 Type *returnType = pointerToCharType;
214 vector<Type *> functionParams;
215 for (
int i = 0; i < getJsonFunctionType->getNumParams(); ++i)
216 functionParams.push_back(getJsonFunctionType->getParamType(i));
218 FunctionType *functionType = FunctionType::get(returnType, functionParams,
false);
219 function = Function::Create(functionType, GlobalValue::ExternalLinkage, functionName,
module);
221 AttributeSet paramAttributeSet = chosenJsonFromValueFunction->getAttributes().getParamAttributes(1);
222 for (
int i = 0; i < getJsonFunctionType->getNumParams(); ++i)
226 if (function->isDeclaration())
228 BasicBlock *block = BasicBlock::Create(
module->getContext(),
"",
function);
231 for (Function::arg_iterator args = function->arg_begin(); args != function->arg_end(); ++args, ++argIndex)
234 ostringstream argName;
235 argName <<
"value" << argIndex;
236 value->setName(argName.str().c_str());
243 vector<Value *> getJsonArgs;
244 for (Function::arg_iterator args = function->arg_begin(); args != function->arg_end(); ++args)
247 getJsonArgs.push_back(value);
249 CallInst *getJsonReturn = CallInst::Create(chosenJsonFromValueFunction, getJsonArgs,
"", block);
251 vector<Value *> jsonObjectToJsonStringExtArgs;
252 jsonObjectToJsonStringExtArgs.push_back(getJsonReturn);
253 jsonObjectToJsonStringExtArgs.push_back(ConstantInt::get(
module->getContext(), APInt(32, JSON_C_TO_STRING_PLAIN)));
254 CallInst *jsonObjectToJsonStringExtReturn = CallInst::Create(jsonObjectToJsonStringExtFunction, jsonObjectToJsonStringExtArgs,
"", block);
256 CallInst *strdupReturn = CallInst::Create(strdupFunction, jsonObjectToJsonStringExtReturn,
"", block);
258 CallInst::Create(jsonObjectPutFunction, getJsonReturn,
"", block);
260 ReturnInst::Create(
module->getContext(), strdupReturn, block);
264 getInterprocessStringFunction =
function;
266 getStringFunction =
function;
285 void VuoCompilerType::parseOrGenerateRetainOrReleaseFunction(
bool isRetain)
287 string functionName = (
getBase()->
getModuleKey() + (isRetain ?
"_retain" :
"_release")).c_str();
292 Type *secondParamType = NULL;
294 vector<Type *> functionParams;
295 functionParams.push_back(firstParamType);
297 functionParams.push_back(secondParamType);
298 FunctionType *functionType = FunctionType::get(Type::getVoidTy(
module->getContext()), functionParams,
false);
299 function = Function::Create(functionType, GlobalValue::ExternalLinkage, functionName,
module);
303 if (function->isDeclaration())
305 BasicBlock *block = BasicBlock::Create(
module->getContext(),
"",
function);
307 Function::arg_iterator args =
function->arg_begin();
309 arg->setName(
"value");
318 ReturnInst::Create(
module->getContext(), block);
322 retainFunction =
function;
324 releaseFunction =
function;
341 vector<Value *> functionArgs;
342 functionArgs.push_back(arg);
346 returnVariable =
new BitCastInst(returnVariable, PointerType::get(llvmType, 0),
"", block);
348 return new LoadInst(returnVariable,
"",
false, block);
352 Value *returnValue = CallInst::Create(
function, arg,
"", block);
367 return generateFunctionCallWithTypeParameter(
module, block, arg, getStringFunction);
380 if (getInterprocessStringFunction)
381 return generateFunctionCallWithTypeParameter(
module, block, arg, getInterprocessStringFunction);
396 return generateFunctionCallWithTypeParameter(
module, block, arg, getSummaryFunction);
405 generateFunctionCallWithTypeParameter(
module, block, arg, retainFunction);
414 generateFunctionCallWithTypeParameter(
module, block, arg, releaseFunction);
420 Value * VuoCompilerType::generateFunctionCallWithTypeParameter(Module *module, BasicBlock *block, Value *arg, Function *sourceFunction)
424 Value *secondArgument = NULL;
425 Value **secondArgumentIfNeeded = (
function->getFunctionType()->getNumParams() == 2 ? &secondArgument : NULL);
428 vector<Value *> args;
431 args.push_back(secondArgument);
432 return CallInst::Create(
function, args,
"", block);
450 *secondType = (getJsonFunction->getFunctionType()->getNumParams() == 2 ? getJsonFunction->getFunctionType()->getParamType(1) : NULL);
451 return getJsonFunction->getFunctionType()->getParamType(0);
461 return getJsonFunction->getAttributes().getParamAttributes(1);
480 return getInterprocessStringFunction != NULL;
488 return areEqualFunction != NULL && isLessThanFunction != NULL;