Create llvm metadata node with constantInt - metadata

While the language reference mention a lot about the LLVM Metadata,
and I see some posts on SO - How to add a Metadata String to an LLVM module with the C++ API?
I also see some code in the llvm source-
http://llvm.org/doxygen/DIBuilder_8cpp_source.html
However, they dont seem to mention how to create a MDNode containing a ConstantInt of a particular width.
Following is the relevant code (doesn't work ) -
std::vector<Metadata*> Elts =
{
ConstantInt::get(TheContext,APInt(returnType->getIntegerBitWidth(),decimal_val))
};
MDNode* Node = MDNode::get(TheContext, Elts);
callInst->setMetadata(LLVMContext::MD_range,Node);
Can anyone explain how this can be done ?
Thanks!

I have written this small post about how to insert metadata in LLVM IR. You can refer that. Basically you need to use ConstantAsMetadata to achieve this.

So apparently I checked the llvm class hierarchy and checked the sub-classes
under MetaData. I found a few classes and one of them was - ConstantAsMetadata
The change in the above code that works for me is -
std::vector<Metadata*> Elts = {
ConstantAsMetadata::get(ConstantInt::get(TheContext,APInt(returnType,0)) ),
ConstantAsMetadata::get(ConstantInt::get(TheContext,APInt(returnType,decimal_val)) )
};
Note- You should ( if you want correctness ) specify a pair of numbers for each range that you are trying to create.
If you are using the verifier pass provided by llvm , the above would not work/make sense if you just have a single ConstantInt inside the initializer.
This is because of an assert inside the Verifier pass provided by the llvm.

Related

What are similar class for below objects in Harmony OS?

Please help me for similar classes for 1. AppBarLayout, 2.Interpolator(with setInterpolator method), 3. BitmapFactory.decodeResource(res, id), 4. SpannableString, 5. ReplacementSpan in HarmonyOS.
For Interpolator/setInterpolator try using Animator/AnimatorProperty setCurveType(curveType)
By default it is a linear interpolator but you can set other types as well. Please check -
https://developer.harmonyos.com/en/docs/documentation/doc-references/animatorproperty-0000001054439987#ZH-CN_TOPIC_0000001054439987__setCurveType-int-
For BitmapFactory.decodeResource(res, id) : you can use ImageSource and createPixelmap. Something like-
In addition to the BitMapFactory.decodeResource and ImageSource.createPixelmap method classes, No other class can find a similar type in Harmony OS by now.
For details about image decoding, see the following website development guide:
https://developer.harmonyos.com/en/docs/documentation/doc-guides/media-image-decoding-0000000000031770?ha_source=hms1

Kaitai Struct Parameter Type

I am trying to pass a parameter to ksy file. The parameter is of type another ksy file. The reason is that i need to access all the fields from the ksy file passed as parameter.
Is that possible?
If yes, would you please provide me with syntax code snippet so I can mimic it.
If no, what would be another solution?
Thank You.
Affiliate disclaimer: I'm a Kaitai Struct maintainer (see my GitHub profile).
First, I recommend always using the development version of the Kaitai Struct Web IDE (https://ide.kaitai.io/devel/), not the stable one. The stable IDE deployed at https://ide.kaitai.io/ has KS compiler of version 0.8, which is indeed the latest stable version, but already 2 years old at the moment. But the project is under active development, new bug fixes and improvements are coming every week, so the stable Web IDE is pretty much outdated. And thanks to the recent infrastructure enhancement, the devel Web IDE now gets rebuilt every time the compiler is updated, so you can use even the most recent features.
However, you won't be able to simulate the particular situation you describe in the Web IDE, because it can't currently handle top-level parameteric types (there is no hook where you can pass your own values as arguments). But it should work in a local environment. You can compile the commontype.ksy and pty.ksy specs in the Web IDE to the target language you want to use (the manual shows how to do it). The code putting it together could look like this (Java):
Commontype ct = new Commontype(new ByteBufferKaitaiStream(new byte[] { 80, 75 }));
Pty r = new Pty(
new ByteBufferKaitaiStream(new byte[] { 80 }), // IO stream
ct // commonword
);
Note that the actual parameter order of the Pty constructor may be different, e.g. in Python come the custom params (commonword) first and then the IO object. Check the generated code in your particular language.

autools, libtool and -Wl,-whole-archive

I have a problem very similar to this question. I need to tell the linker to add the static lib, despite the fact that to reference to its symbols seems to be made.
The reason for that is that the library contains an function in .init or using ( __attribute__ ((constructor))) to perform its initialisation, which in turn, is going to call a register() function from the main program to register its functionality (read: pass other function pointers).
So despite the fact that no symbol from the library seems to be used in the main program, the main program will call functions from the library as soon as the latter are registered via the library init function.
But I am using libtool...
So taking the example given in the previously mentionned question, I'd need to write something like:
bin_PROGRAMS = pktanon
pktanon_SOURCES = main.cpp
pktanon_DEPENDENCIES = $(lib_LIBRARIES)
pktanon_LDADD = libpktanon.la $(LDADD)
Note the "la" extension instead of "a" for the lib.
Now, how shall I pass the --whole-archive option to the linker? The answer suggested in the question assumes the path to the archive (.a file) is known... It does not feel right to hard code a path like .libs/libptanon.a in the Makefile.am...
And the linker does not like meeting a .la file in its whole-archive otpion if trying:
pktanon_LDFLAGS = -Wl,--whole-archive,libpktanon.la,--no-whole-archive
Any suggestions?
The use of --whole-archive is not portable, so why not create a Libtool convenience library instead? In your Automake Makefile.am, add noinst_ to the library, then instead of creating a (possibly non-pic) static archive, it will take all the object files in the library and add it to anything that links to it. It is portable, and should work for this case, as it doesn't seem like you want a library here at all.

What types can there be for "UDLText"s other than comments?

I am now using the Caché DB jar to query for class definitions (thanks #daimor; it was an eye opener) and in order to write the source files I need to also account for "UDL texts".
Now, all I have been able to understand of them so far is that they are comments found in the source you actually write in Studio. But then UDLTextDefinition defines .getCategory(); the only value I have seen this method return so far is "comment".
Is there any other possible value?
If you open source code for that class (%Dictionary.UDLTextDefinition), you will find CategoryIsValid method, and will see, that only two values are correct now, and it is comment and error. I'm sure you understand category comment. So, error used when developer managed to save source code with syntax errors in class definition, and that text will be in UDLText and with category error.

Eclipse C++ std::vector methods invalid parameters errors

I am working on an Android application that uses OpenCV 2.4.9 and NDKr9 as dependecies. I also use Eclipse 4.4 Luna as the IDE, with CDT plugin 8.4 installed.
Whenever I'm trying to use the methods std::vector.at(int), or the "[]" method, i get weird errors. For instance, consider the code:
#include <vector>
.........................
struct CustomStruct {
int level;
Point firstPoint, secondPoint, middlePoint;
};
.........................
int maxElemNr = 10;
std::vector<CustomStruct > customStructVector(maxElemNr);
.........................
for(int i=0;i<customStructVector.size();i++){
if(customStructVector.at(i).level == 0){
}
}
At customStructVector.at(i) Eclipse tells me the following:
Invalid arguments ' Candidates are: ResultWithEvidence & at(?) const
ResultWithEvidence & at(?)'
If I want to use the "[]" operator, instead of the "at(index)" method, i get the following:
resultWithEvidenceVector[i].level tells me that the field level is not found.
I am by no means an expert C/C++ coder, and I am rather new at working with the NDK. Coming from a Java background, I was expecting to get an object of type CustomStruct when calling either customStructVector.at(i) or customStructVector[i], such that I could then simply access the field level on my object, to read its value.
Also, declaring my vector as
int maxElemNr = 10;
std::vector<CustomStruct> customStructVector;
customStructVector.reserve(maxElemNr);
I get:
Invalid arguments ' Candidates are: void reserve(?) '
I have searched for answers, and came with the theory that eclipse might not use the c++11 version of the std library?
I've read about the vector class from here. Also, this issue closely resembles the question asked here.
Will provide more info about my environment and such, if needed. Would like to solve this, as it's quite a blocker for my project as of now..
What am I doing wrong? I had no problems compiling and running the code previously to using the std::vector class.
LE: apparently the workaround &(resultWithEvidenceVector.data()+i)->level is recognized by the editor, and the code compiles. Would still like to use the std::vector as it is supposed to be used though.
I had the same problem. It works in Visual Studio compiler but when i try to access vector element i get exactly the same error.
However it seems that if you don't use 'at' then it works for example customStructVector.at(i) use customStructVector(i),
It is strange. I didn't test it in detail. Please note that if you have vector you have to cast the result in order to access the type members.
Regards