Extracting llvm::CallInst returned %tmp name - return

I have an llvm::CallInst *i, representing this call (taken from *.ll file):
%tmp3 = call i64 #__fdget(i32 %tmp) #5
How do I extract the returned value name (%tmp3 here)?
Thanks!

llvm::Value::getName contains instruction name.
llvm::StringRef name = i->getName();

Related

Freezed #Default list parameter creates unmodifiable list

I am trying out the example given in the Freezed page at https://pub.dev/packages/freezed.
This is the example I am testing replacing int with List<int> adding a [40] as default.
#Freezed(makeCollectionsUnmodifiable: false)
class Example with _$Example {
factory Example([#Default([40]) List<int> list]) = _Example;
factory Example.fromJson(Map<String, dynamic> json) => _$ExampleFromJson(json);
}
using the Example class:
final e = Example(); //default parameter [40]
//e.list.add(42); //error producing line
print('e $e');
final f = Example([40]); //parameter same as default value
f.list.add(42);
print('f $f');
final g = Example([41]); //another parameter
g.list.add(42);
print('g $g');
The output is:
e Example(list: [40])
f Example(list: [40, 42])
g Example(list: [41, 42])
If I remove the comment in the default parameter case, I get the following error:
Unhandled exception:
Unsupported operation: Cannot add to an unmodifiable list
#0 UnmodifiableListMixin.add (dart:_internal/list.dart:114:5)
#1 main (file:///D:/Dart/darttest/bin/darttest.dart:27:10)
#2 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#3 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
Why the list becomes unmodifiable?
Dart annotations are used by code generators that parse the source code. As the expressions inside annotations do not get executed, they must be constant expressions that can be computed during compilation.
Objects constructed with the const keyword must be immutable. They cannot change, as is written in the Dart Language Tour:
You can’t change the value of a const variable:
baz = [42]; // Error: Constant variables can't be assigned a value.
Although a final object cannot be modified, its fields can be changed. In comparison, a const object and its fields cannot be changed: they’re immutable.
var constantList = const [1, 2, 3];
// constantList[1] = 1; // This line will cause an error.
To provide a default list value in the annotation, it must be const and therefore unmodifiable.
A note on Freezed
On another note, modifying lists goes against the entire Freezed concept. Freezed is used to generate immutable data structures, that can be updated with the copyWith method. It's a mistake to modify any collections in Freezed classes.
A recent update does add mutability through the unfreezed annotation, which should be used for mutable classes.

Unable to print a function's parameter outside function even when returned

Contents is a table which is passed as a parameter for the function func. The issue is that I'm unable to print out any value from the table contents. For example here you can see I was trying to print the 2nd value in the table contents but I get this error 12: attempt to index global 'contents' (a nil value)
function func(contents)
print(contents[1])
return contents
end
func({"content1", "content2", "content3"})
print(contents[2])
If your function will always use the global variable, create the global variable and don't take it as an argument to the function:
contents = {"content1", "content2", "content3"}
function func()
print(contents[1])
print(#contents)
return contents
end
func()
print(contents[2])
If you do want to have a global variables, I'd suggest renaming your argument to avoid confusion. As this seems to just be a sample and your table seems to contain strings, I'll rename the argument 'strings' and show you can use the return value or the global variable (repl.it):
contents = {"content1", "content2", "content3"}
function func(strings)
print(strings[1])
print(#strings)
return strings
end
local result = func(contents)
print(contents[2])
print(result[2])
Using global variables is frowned upon, you can make it local to your module and use it anywhere in your module by putting 'local' in front:
local contents = {"content1", "content2", "content3"}
how can I make contents global
Function parameter contents is local to func and is shadowing any variable named contents in a bigger scope.
A global variable name is just referring to a field in table _G.
So in your function you could do
_G.contents = contents
But usually you avoid using global variables wherever possible.
As func returns contents simply do
print(func({"content1", "content2", "content3"})[2])
It is because you have the print(contents[2]) outside of the function. contents is local to the function only. According to the rest of the program contents does not exist thus it is throwing an error. There is no global variable contents and it is saying 12: which tells you the error is on line 12, which again, has the print(contents[2])

Can you run code before creating a type provider? (F#)

Say:
let x = // some operation
type t = SomeTypeProvider<x>
Is this valid?
No.
Since the types must be generated at compile-time, the parameter to the type provider needs to be a constant.
In other words, the code you marked // some operation can evaluate to a literal, but cannot be a value returned by a runnable function:
let arg = "foo"
type t = SomeTypeProvider<arg> // okay
let [<Literal>] arg = """{"name":"foo","value":42}"""
type t = SomeTypeProvider<arg> // okay
let arg = x.ToString()
type t = SomeTypeProvider<arg> // Boom! arg is not a Literal
It depends on your application, but one of the most common cases is the following:
You have a database-related Type Provider, and the connection string needs to be retrieved in runtime, from some sort of config file or something. So a developer mistakenly thinks they need a runnable code to retrieve the connection string first and then pass it to the Type Provider.
The correct approach is the following:
Keep two databases: one locally stored in a constant location (just for schema), and another one for the runtime purposes.
Pass the first one (a constant!) to your Type Provider. Don't worry about the hardcoded paths; it is only used for schema retrieval.
// Use a fixed sample file for schema generation only
type MyCSVData = CsvProvider<"dummy.csv">
// Load the actual data at runtime
let data = MyCSVData.Load(RetrieveFileNameFromConfig())

F# constructor with <"string", str>

In this article it shows how to use the SqlCommandProvider type. The sample code has this:
use cmd = new SqlCommandProvider<"
SELECT TOP(#topN) FirstName, LastName, SalesYTD
FROM Sales.vSalesPerson
WHERE CountryRegionName = #regionName AND SalesYTD > #salesMoreThan
ORDER BY SalesYTD
" , connectionString>(connectionString)
what does the <... ,...> before the type constructor name mean and why the the
first parameter have to be a string literal? It looks like a generic but it's taking variables not types. The constructor seems to be taking in a connection string already in the <> section.
The angle brackets are the configuration for a type.
In your example, you are defining a type and creating an instance at the same type. It's clearer when the steps are separated.
Define a type.
type SalesPersonQuery = SqlCommandProvider<query, connectionString>
But to actually have an instance of the type you have to create it:
let command = new SalesPersonQuery()
Now you can use the command.Execute() rather then SalesPersonQuery.Execute().
The reason there is a constructor is because later on (at run-time) you can change the connection string to a different then the one provided in the definition, so for instance:
let command = new SalesPersonQuery(differentConnectionString)
You can find that in the documentation in configuration section:
Connection string can be overridden at run-time via constructor optional parameter
First parameter can be a path to a SQL script or a SQL query. I suppose that's the reason it's a string: how else would you like to define a SQL query?
Again, from the documentation:
Command text (sql script) can be either literal or path to *.sql file

Which entity does a cross-reference refer?

If I have the following input
test = 3; //first
test = test + 3; //second
parsed with a grammar that looks like this (for example)
Declaration:
name=ID "=" DeclarationContent
;
DeclarationContent:
number=INT ("+" DeclarationContent)?
| reference=[Declaration] ("+" DeclarationContent)?
;
to which declaration does the reference refer (looking at the second declaration in my example)? Or in other words does the reference "test" (second Declaration) refer to the first Declaration ("test = 3;") or directly to itself ("test = test+ 3") and would therefor be a cycle in hierarchy.
Greeting Krzmbrzl
Xtext will always link a cross reference [Declaration] to the first elemenent which is identified by the given ID. In your example hello + 3, the cross reference behind hello will resolved to the declaration hello in line 1. But if you have more then one element with the same (qualified) name within the same scope (container of visibility) Xtext will throw an Exception. You should
implement your own ScopeProvider (Use the generated stub in your.dsl.scoping.YourDslScopeProvider) to resolve this problem or
Use the composed check to automatically validate names.
fragment = validation.JavaValidatorFragment auto-inject {
composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
}

Resources