LLVM: How to traverse Module metadata to find a value? - llvm

I have this metadata tree in my LLVM module:
!meta.test = !{!0}
!0 = !{"str1", "str2", !1}
!1 = !{!2, !3, null}
!2 = !{"str3", i8 5}
I want to be able to get the value: i8 5.
I am trying it using M->getNamedMetadata("meta.test"), but I'm unable to traverse the metadata tree using the LLVM API to reach that value.
How should I do this?
Cheers.

For LLVM 3.6 onwards
getNamedMetadata returns NamedMetadata, you can use getOperand(unsigned) to get MDNode and can cast to your appropriate type as per your use.
so M->getNamedMetadata("meta.test")->getOperand(0) will get you metadataNode !0 MDNode.
you can use cast< ValueAsMetadata >(MDNode)->getvalue() to get Value i8 5
or you can use cast< MDString >(MDNode)->getString() to get Value str1.
so in short you can traverse metadata MDNodes using getOperand() call and cast it to your use as per hierarchy. see this for more info.

Related

Changing the name of a list on the fly using a counter

I have a set of list,
list_0=[a,b,a,b,b,c,f,h................]
list_1=[f,g,c,g,f,a,b,b,b,.............]
list_2=[...............................]
............
list_j=[...............................]
where j is (k-1), with some thousands of value stored in them. I want to count for how many times a specific value is in a specific list. And I can have only 8 different values (I mean, every single element of those list can only have one out of 8 specific values, let's say a,b,c,d,e,f,g,h; so I want to count for every list how many times there's the value a, how many times the value b, and so on).
This is not so complicated.
What is complicated, at least for me, is to change on the fly the name of the list.
I tried:
for i in range(k):
my_list='list_'+str(int(k))
a_sum=exec(my_list.count(a))
b_sum=exec(my_list.count(b))
...
and it doesn't work.
I've read some other answer to similar problem, but I' not able to translate it to fit my need :-(
Tkx.
What you want is to dynamically access a local variable by its name. That's doable, all you need is locals().
If you have variables with names "var0", "var1" and "var2", but you want to access their content without hardcoding it. You can do it as follows:
var0 = [1,2,3]
var1 = [4,5,6]
var2 = [7,8,9]
for i in range(3):
variable = locals()['var'+str(i)]
print(variable)
Output:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Although doable, it's not advised to do this, you could store those lists in a dict containing their names as string keys, so that later you could access them by simply using a string without needing to take care about variable scopes.
If your names differ just by a number then perhaps you could also use a list, and the number would be the index inside it.

How to set an attribute of a Dom element

I have created a td element with a call such as:
let td = Dom_html.createTd doc in
I would now like to set an attribute on this object. I have tried this:
td#setAttribute (Js.string "colspan") (Js.string "4")
But I get the error:
Error: This expression has type Dom_html.tableCellElement Js.t
It has no method setAttribute
Simple dash # is used to access method of OCaml object.
Js_of_ocaml has a special syntax (##) to deal with Javascript object.
see http://ocsigen.org/js_of_ocaml/2.4/manual/library
To set an attribute of a dom element:
td##setAttribute(Js.string "key", Js.string "val")
In you case you should rather use :
td##colSpan <- 4
The double dash ## will translate JavaScript field access.
The previous statement translates to td.colSpan = 4.
The type parameter 'a in 'a Js.t is a phantom type used by the type checker to check JavaScript field access. see http://ocsigen.org/js_of_ocaml/2.4/api/Dom_html.tableCellElement-c in your case.

Ember set subtraction with PromiseArrays

I have two PromiseArray objects, from within a controller, e.g. this.get('content.skills') and this.get('allSkills').
I'd like to do what is essentially set subtraction. For example:
[1, 2, 3] - [2, 3] // => [1]
Is there a straightforward to do this? There's an alias for doing intersections. My guess is that it will use rejectBy somehow, but I'm not quite sure how.
You can use http://emberjs.com/api/classes/Ember.MutableArray.html#method_removeObjects with the Promise's content as long as they are fulfilled.
P1.removeObjects(P2.toArray());

How to create named local variable in llvm?

In a struct like this:
struct point{
int x0;
int y0;
};
How can I pass the names x0 and y0 to llvm when I create the llvm::StructType with C++ API?
You can't; in LLVM IR, fields of structs do not have names.
What you can do depends on what you are trying to achieve:
If you want to enable debug info for these fields, this is not the correct approach anyway - instead, use a DIBuilder to define the struct type.
If you want to make the IR's textual representation (.ll files and dump() results) more readable, you can do something different - whenever a field is accessed, have the frontend use the field name for that field's value. For example:
%p.py0 = getelementptr %point* %p, i32 0, i32 1
%p.y0 = load i32* %p.py0
the getelementptr that accesses the 2nd field has a name that indicates it's a pointer to a field called y0, and the load has a name that indicates it's the actual field. Also notice how the name includes the name of the variable those were accessed from, for extra readability.

Get size of Regex.MatchIterator in Scala 2.7.7?

What is the most elegant way to get the size of a Regex.MatchIterator in Scala 2.7.7?
I tried the following:
ยค scala
Welcome to Scala version 2.7.7final (OpenJDK 64-Bit Server VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
scala> "a".r.findAllIn("a").size
<console>:5: error: value size is not a member of scala.util.matching.Regex.MatchIterator
"a".r.findAllIn("a").size
^
scala> "a".r.findAllIn("a").size()
<console>:5: error: value size is not a member of scala.util.matching.Regex.MatchIterator
"a".r.findAllIn("a").size()
^
You can convert the Iterator:
iter.toList.size
Be sure to save the converted iterator (if you want to access the data after calculating the size) because it can only iterated once.
Instead of converting to another collection you can also use foldLeft:
(0 /: iter) { case (sum, _) => sum+1 }