How to clear output buffer using LLVM javacpp? How to execute fflush using LLVM javacpp?

174 views Asked by At

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.

0

There are 0 answers