0

I am using this javacpp to build a compiler with LLVM in Java. I was able to generate code for input and output.

INPUT: First I have a LLVMValueRef which is the symbol, say the target variable that will receive the input. Then I have a LLVMValueRef that is the scanf function. I set the arguments and build the function call passing then. It works just fine.

    LLVMValueRef valueRef = symbol.getLlvmValueRef();
    LLVMValueRef scanfFunction = LLVMGetNamedFunction(moduleRef, "scanf");
    LLVMValueRef[] scanfArgs = { str, valueRef };
    LLVMBuildCall(builderRef, scanfFunction, new PointerPointer(scanfArgs), 2, "scanf");

OUTPUT: Similarly I am able to print out whatever the string I want. I have printt, which I can get with LLVMGetNamedFunction, and after that I build the fuction call passing all the arguments that composes the string to be printed.

    LLVMValueRef printFunction = LLVMGetNamedFunction(moduleRef, "printf");
    LLVMBuildCall(builderRef, printFunction, new PointerPointer(args), printArgs.size(), "printf");

MY PROBLEM:

If I have a printf before a scanf, the scanf always comes first. I think it has something with the buffer, similar to this problem: C/C++ printf() before scanf() issue

So I tried to fflush it after build every printf call. Like this:

    LLVMValueRef[] fflushArgs = { LLVMConstNull(LLVMPointerType(LLVMInt8Type(), 0)) };
    LLVMValueRef fflushFunction = LLVMGetNamedFunction(moduleRef, "fflush");
    LLVMBuildCall(builderRef, fflushFunction, new PointerPointer(fflushArgs), 1, "fflush");

My intention is to call fflush(null).

However, I get this error:

LLVM ERROR: Tried to execute an unknown external function: fflush

So, I have access to printf and scanf, but I can not use fflush

How can I be able to use FFLUSH? Is there another way to clear this buffer? What can I do to have printf and scanf happening in the correct order? Thanks for all your help.

MariaH
  • 331
  • 1
  • 8
  • 21
  • Did you declare `printf` and `scanf` before you used them? If so, you should do the same for `fflush`. – sepp2k Nov 24 '18 at 14:20
  • No, I just use them. I just do this: LLVMValueRef printFunction = LLVMGetNamedFunction(moduleRef, "printf"); LLVMValueRef scanfFunction = LLVMGetNamedFunction(moduleRef, "scanf"); and it works for them, but not for fflush when I do (similarly): LLVMValueRef fflushFunction = LLVMGetNamedFunction(moduleRef, "fflush"); – MariaH Nov 24 '18 at 23:53
  • Oh, on second look this looks like an error message you'd get out of lli. Does that mean that generating the LLVM works fine and you only get the error when running it? If so, does it work if you compile it instead of using lli? Or if you're not using lli, how are you running it? Have you looked at this: https://groups.google.com/forum/#!topic/llvm-dev/hhQv8U9aQLI – sepp2k Nov 25 '18 at 03:24

0 Answers0