Casting `[<length> x i8]*` to `i8 *` - clang

I'm trying to replace an argument to an existing function call. The initial argument is a global string and i'm trying to replace it with a local string.
No matter what i try i get a Call parameter type does not match function signature! runtime error.
Specifically i want to replace the argument to the printf function - declare dso_local i32 #printf(i8*, ...) #1. The call to it in the code looks like this:
#message = dso_local global [12 x i8] c"Hello World\00", align 1
...
%call = call i32 (i8*, ...) #printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* #message, i64 0, i64 0))
I allocate and GEP the local replacement string as such:
%0 = alloca [12 x i8], align 1
%1 = getelementptr inbounds [12 x i8], [12 x i8]* %0, i64 0
But when i use setOperand() on %call to replace the argument with %1, i get the aforementioned error. I guess [12 x i8]* is different from i8* but i can't find a way to cast it that works.
I've tried casting with CreatePointerCast() which created this:
%2 = addrspacecast [12 x i8]* %1 to i8 addrspace(8)*
But got the same error (the type for %2 is i8 addrspace(8)* which is different from i8* i guess).
Also tries explicitly specifying the i8* type when creating the GEP:
Builder->CreateInBoundsGEP(llvm::Type::getInt8PtrTy(Ctx, 8), ...)
to no avail.

I think you're using the GEP instruction wrongly.
Remember the first index steps over the pointer of type [12 x i8]*,which is what you supplied, and the second index specifies helps compute the starting address of the first character of your string, which is probably what you missed.

Related

getelementptr llvm ir instruction arguments

I'm working on a toy compiler and I'm trying to generate llvm ir bytecode and the exe compiling it with clang.
I've a simple question: I searched on google some information about the getelementptr, but I can't understand why I've to pass two i32 or i64 consts as last two arguments.
For example:
In global scope:
#string = constant [4 x i8] c"abc\00"
In function scope:
;result now contains the value of #string
; instruction | type of result | pointer to #string | what are these two i32 0?
%result = getelementptr [4 x i8], [4 x i8]* #string, i32 0, i32 0

Maxima: replace function f(x) by its definition?

I cannot find anything about this, sorry.
If I have expressions with the symbolic function f(x) and now I want to replace in these expression f(x) by its explicit form how to do it?
For example:
I have
f(x):= x^2+sin(x)
and in the differentiation
diff (%e**sqrt(f(x)*a), x,2);
I want to replace now f(x) by the expression above?
Thanks
Karl
(%i1) i: integrate(f(x)*f(4*x), x, 0, 1) $
(%i2) f(x):= x^2+sin(x) $
(%i3) ev(i, f);
1
/
[ 2 2
(%o3) I (sin(x) + x ) (sin(4 x) + 16 x ) dx
]
/
0
-- Function: ev (<expr>, <arg_1>, ..., <arg_n>)
Evaluates the expression <expr> in the environment specified by the
arguments <arg_1>, ..., <arg_n>. The arguments are switches
(Boolean flags), assignments, equations, and functions. 'ev'
returns the result (another expression) of the evaluation.

How to convert a variable's value to an int in a model?

When using a model object, I call func_decl get_func_decl (unsigned i) to get the value assigned to a certain function (variable). The problem I am having is taking the output of that (which is a func_decl) and converting it to int.
For example, if the model is {x |-> 4, y |-> 12, z |-> 6}, I would like to get the actual int values of those 3 variables (4, 12 and 6).
Z3 provides the function Z3_get_numeral_int for this purpose:
Z3_bool Z3_API Z3_get_numeral_int(__in Z3_context c, __in Z3_ast v, __out int* i);
Note that the last parameter is a point to an integer that will be filled with the right value if the call succeeds (it will fail for non-numerals or numerals which are not representable as an int).
There are also other functions called Z3_get_numeral_* to obtain values of different types, e.g., uint64 etc.
This method works for constants (i.e., func_decls of arity 0). To get the entries of a (non-zero arity) function definition, the following function should be used:
Z3_func_entry Z3_API Z3_func_interp_get_entry(__in Z3_context c, __in Z3_func_interp f, unsigned i);

What does the symbol "⊇" mean?

In the attached picture there's a symbol I don't understand. To understand additive functional dependency I need to know what the symbol means. Please advice?
It's the symbol where it says: "Suppose that X ⊇ Y and that..."
⊇ = ?
Thanks!
This symbol is used in set theory to say that X is a superset of Y, ie. All elements of Y are contained within X. Note that X could be equal to Y in this meaning. Without the underlining on the symbol Y would be a strict subset of X meaning they cannot be equal
It means "X is a superset of Y". The subset symbol is the same but flipped horizontally.
A ⊇ B -> Superset: A has the same elements as B, or more
A ⊃ B -> Proper Superset: A has B's elements and more
for example:
A ⊇ B -> {1, 2, 3} ⊇ {1, 2, 3}
A ⊃ B -> {1, 2, 3, 4} ⊃ {1, 2, 3}

Why is this variable unused?

Why does compiling this code:
triples( [], _,_,_)->
[];
triples( Self, X, Y, none )->
[ Result || Result = { X, Y, _} <- Self ].
report:
./simple_graph.erl:63: Warning: variable 'X' is unused
./simple_graph.erl:63: Warning: variable 'Y' is unused
./simple_graph.erl:64: Warning: variable 'X' is unused
./simple_graph.erl:64: Warning: variable 'X' shadowed in generate
./simple_graph.erl:64: Warning: variable 'Y' is unused
./simple_graph.erl:64: Warning: variable 'Y' shadowed in generate
And return wrong result: full Self.
This is because variables occurring on the LHS of generators, X and Y here, are always new unbound variables local to the comprehension. This means that they are not the same variables as the X and Y in the head of triples and, therefore, there is no implicit equality test. This similar to funs where all variables occurring in the head of a fun are alse new variables local to the fun.
This is different from most of the rest of erlang, which is why the compiler not only warns that the X and Y in the head are not used but also that the X and Y in the comprehension shadow the other variables. They are also unused anywhere in the comprehension.
An easy way to get what you want is:
[ Result || Result = {X1,Y1,_} <- Self, X =:= X1, Y =:= Y1 ]

Resources