how to get string literal from LLVM IR instruction - llvm-ir

I want to get string literal from LLVM IR.
C source code looked like:
char *test = "string";
LLVM IR looked like:
#.str = private unnamed_addr constant [9 x i8] c"string\00", align 1
#test = global i8* getelementptr inbounds ([9 x i8], [9 x i8]* #.str, i32 0, i32 0), align 8
I got some how in ArgValue variable. --> the second line of IR..
my code looked like this : and now i am stuck after getting Constant*
GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(ArgValue);
Value *Valop = gep->getPointerOperand();
Instruction *inst = dyn_cast<Instruction>(Valop);
Constant *cda = dyn_cast<Constant>(inst->getOperand(0));
now after the last statement, how do i get the constant "string" is something i stuck at.
Constant *cda is not null.. the third line is success
it does not work if i try to type cast to any other object.
please help...

Related

Converting a list of integers to a map of vertices containing the elements coordinates

This is what i have at the moment
(string -> int list)
let read filename = ....
this is working as intended, returning a list of integers from a textfile looking like this:
530070000
600195000
098000060
800600003
400803001
700020006
060000280
000419005
000080079
Yes you are correct, it is a sudoku board. This is what i have to work with:
type vertex = int * int (*Cells in the sudoku board*)
type gamma = int (*representing colors 1-9*)
(* [Vertex = Map.Make(Vertex)] *)
module Vertex = Map.Make(struct
type t = vertex
let compare = Stdlib.compare
end)
(* [Gamma = Set.Make(Gamma)] *)
module Gamma = Set.Make(struct
type t = gamma
let compare = Stdlib.compare
end)
The gamma set is for solving the sudoku board using graph coloring. I need help understanding how i can convert the list of integers to a suitable map for this kind of task. According to the structure i provided, so i can access each element in the map using it coordinates (x, y). Hope you understand, otherwise i will try to provide more info. I'm reaaally bad at OCaml but trying to learn. I'm sorry for body errors etc, first time posting here.
As far as I can understand your task, the text file contains a grid of digits with the initial disposition for sudoku. So you shouldn't interpret a line in the file as a single integer but rather as a list of integers. You can either change your read function so that it returns int list list instead of int list and then use List.fold_left over the list that will also count the position of an element in the list, but it is tedious. It is much easier to read the grid directly from the file, e.g.,
let read_matrix chan =
let rec loop i j grid =
match input_char chan with
| exception End_of_file -> grid
| '\n' -> loop (i+1) 0 grid
| '0'..'9' as c ->
loop i (j+1) ##
Vertex.add (i,j) (ascii_digit c) grid
| _ -> invalid_arg "invalid input" in
loop 0 0 Vertex.empty
where ascii_digit is defined as,
let ascii_digit c = Char.code c - Char.code '0'
The read_matrix function takes the channel as input so to read the grid from a file you can define,
let matrix_from_file file =
let chan = open_in file in
let r = read_matrix chan in
close_in chan;
r
Hint: you probably also don't want to include positions with 0 in your grid. It is easy to achieve, just add another case to the pattern in the loop function that will skip it, e.g.,
...
| '0' -> loop i (j+1) grid
...

How to do function override in OCaml?

I've read the entire chapter about modules on this book but there's something I didn't understand.
Suppose I have a signature and an implementation of that signature:
module type X = sig
val x : int
val y : int
end
module X1 : X = struct
let x = 5;
let y = 6;
end
Then suppose I have a functor that has this signature as paremeter:
module IncX (M: X) = struct
let x = M.x + 1
end
What if I want to instantiate IncX using the already defined X1 module but override the y function?
I want to do something like this:
module X1_Specialized : X = struct
//how use x from X1 module here?
let y = 10;
end
The analog in OOP languages would be to override a function.
What you ask for is usually called implementation inheritance. And it's not necessary to use a functor for this, just include:
module X1_Specialized : X = struct
include X1
let y = 10
end
include will include the entire contents of X1, as if you had written its definition in place of the include. This includes a definition of y, but the following definition of y will shadow it, and essentially replace it.
Also note that OCaml doesn't use semicolon as a statement terminator. These are syntax errors in your code.

Julia convert UInt32 to UInt8 vector

I have the following code and I need to covert several UInt32 variables to UInt8 vectors so then combine them into a single UInt8 vector.
The goal is to take the record I have decoded from a Pcap file and put it into a format that I can append to the end of an existing Pcap file.
The code below takes output from a previous function and returns a hex output of 4 UInt 32's and a vector of UInt8's for the payload.
function pcap_get_record(s::PcapOffline)
rec = PcapRec()
if (!eof(s.file))
rec.ts_sec = s.is_big ? read(s.file, UInt32) : ntoh(read(s.file, UInt32))
rec.ts_usec = s.is_big ? read(s.file, UInt32) : ntoh(read(s.file, UInt32))
rec.incl_len = s.is_big ? read(s.file, UInt32) : ntoh(read(s.file, UInt32))
rec.orig_len = s.is_big ? read(s.file, UInt32) : ntoh(read(s.file, UInt32))
rec.payload = read(s.file, rec.incl_len)
return rec
end
nothing
end
Thanks
Here you are
julia> reinterpret(UInt8, rand(UInt32, 1)) |> Vector
4-element Array{UInt8,1}:
0x4d
0x54
0x34
0xd3
remember to check the byte order.
Update: So I have solved this and I was overthinking what needed to be done.
I just wrote the UInt variable in their raw form and that did the trick.
write(pcap, rec.orig_len) #this is a UInt32
write(pcap, rec.payload) #this is a UInt8 vector
Original:
I was having a hard time making my previous comment readable.
Thanks for the response. I am not however able to get the reinterpret to work with my UInt32 variable.
a = reinterpret(UInt8, rec.ts_usec) |> Vector
ERROR: bitcast: argument size does not match size of target type
Stacktrace:
[1] reinterpret(::Type{UInt8}, ::UInt32) at .\essentials.jl:370
[2] top-level scope at none:0
typeof(rec.ts_usec)
UInt32
after messing around some more I was able to get this to work but this doesn't seem very efficient.
"Edit" I just found that this wont work since it cuts off any leading zeros in the UInt32. example rec.incl_len = 0x00000516 would come out as "516" instead of "00000516" which is needed.
julia> hex(n) = string(n, base = 16, pad = 2)
julia> a = hex2bytes(hex(rec.ts_sec))
4-element Array{UInt8,1}:
0x5b
0x60
0xa3
0xa1

Julia: fields of structs passed by value

In the following piece of Julia code, st.a and b are the same array, so when I delete an element from st.a, then this element is also deleted from b. Is it possible that a new array "*.a" is generated, every time a create an object * of Mystruct?
struct Mystruct
a::Array{Int64,1}
Mystruct(a::Array{Int64,1}) = new(a)
end
b = [1, 2, 3, 4]
st = Mystruct(b)
deleteat!(st.a,1)
I think that:
struct Mystruct
a::Array{Int64,1}
Mystruct(a::Array{Int64,1}) = new(copy(a))
end
will do the job you want.

Mutable Data in OCaml

I've created a mutable data structure in OCaml, however when I go to access it, it gives a weird error,
Here is my code
type vector = {a:float;b:float};;
type vec_store = {mutable seq:vector array;mutable size:int};;
let max_seq_length = ref 200;;
exception Out_of_bounds;;
exception Vec_store_full;;
let vec_mag {a=c;b=d} = sqrt( c**2.0 +. d**2.0);;
let make_vec_store() =
let vecarr = ref ((Array.create (!max_seq_length)) {a=0.0;b=0.0}) in
{seq= !vecarr;size=0};;
When I do this in ocaml top-level
let x = make _ vec _store;;
and then try to do x.size I get this error
Error: This expression has type unit -> vec_store
but an expression was expected of type vec_store
Whats seems to be the problem? I cant see why this would not work.
Thanks,
Faisal
make_vec_store is a function. When you say let x = make_vec_store, you are setting x to be that function, just like if you'd written let x = 1, that would make x the number 1. What you want is the result of calling that function. According to make_vec_store's definition, it takes () (also known as "unit") as an argument, so you would write let x = make_vec_store ().
try x = make_ vec_store()
As a follow up to the excellent answere provided. You can tell that your example line:
# let x = make_vec_store;;
val x : unit -> vec_store = <fun>
returns a function as the repl will tell you this. You can see from the output that x is of type <fun> that takes no parameters unit and returns a type vec_store.
Contrast this to the declaration
# let x = 1;;
val x : int = 1
which tells you that x is of type int and value 1.

Resources