If a variable is passed as a constant does it get passed by reference? - pass-by-reference

If I pass a variable as a constant does it automatically get passed by reference?
procedure foo(const x : integer)
I can already pass a variable by reference like this:
procedure foo(var y : integer);
Ideally I'd want something like the code below:
procedure foo(const var z : integer)

const does not guarantee that the value is actually passed by reference.
Free Pascal supports
procedure foo(constref z : integer);
for this purpose: z is always passed by reference in this case. I'am not aware of something similiar in other pascal compiler.

Related

Converting arrays from signed to integer in VHDL?

I have declared an array
type datastream is array(0 to 10) of signed (5 downto 0);
For simulation, I want to display the array as integer-numbers
So I created
type datastream_int is array(0 to 10) of integer;
and
signal DIN_ARRAY: datastream;
signal DIN_ARRAY_int: datastream_int;
...
DIN_ARRAY_real <= datastream_int(DIN_ARRAY);
But it fails. How to convert it? Dont want to use a for loop
The numeric_std package, that I assume you are using, provides a to_integer function to convert from a single signed value to a single integer object. For an array, you're going to have to use a for loop. Something like this:
for i in DIN_ARRAY'range loop
DIN_ARRAY_int <= to_integer(DIN_ARRAY(i));
end loop;
You could also provide a conversion function (it will also contain a for loop)
function datastream_to_datastream_int( d : datastream ) return datastream_int is
variable r : datastream_int;
begin
for i in d'range loop
r(i) := to_integer(d(i));
end loop;
return r;
end function;
....
--no loop required
DIN_ARRAY_int <= datastream_to_datastream_int(DIN_ARRAY);
So, there will be a for loop somewhere.
Your code fails because you have attempted a type conversion, which is only allowed between similar types - ie. array or record types where the element types match between the two types.
PS. VHDL 2008 provides an integer_vector type in the std.standard library (which is included by default) which may help by allowing you to do this:
signal DIN_ARRAY_int: integer_vector(DIN_ARRAY'range);
If you did decide to keep datastream_int as per your original, you could type convert it to an integer_vector, because the types are similar:
my_iv <= integer_vector(DIN_ARRAY_int);

How does an integer store a literal (eg var x = 0)

when i write var x = 0
I know that x is an object that has properties and methods (created from Int Structure).
Where and how does x store the 0 ?
Is the 0 stored as a property of x ?
If yes, what would be the type of that property ?
If not, where is it stored ?
x is not an object, strictly speaking. "Object" is a name we give to instances of classes, not structs. x is an instance of the Int struct.
The Int structure wraps a Builtin integer type, and defines a bunch of methods you can call on it. That builtin integer literal type isn't accessible from Swift (nor is there a reason for it to be). Like all structures, instances of Int are stored on the runtime stack. They're not objects on the heap like Integer in Java, for example.
You can see the implementation details of (U)Int(8/16/32/64) here. This file uses the Generate Your Boilerplate (GYB) preprocessor created by the Swift team to generate .swift files from .swift.gyb template files.
On line 221, you can see the property _value of type Builtin.${BuiltinName}. The GYB preprocessor expands this out so that Int has a _value of type Builtin.Int, Int64 has Built.Int64, etc.

How to execute procedure from List with parameters in Progress 4gl?

I have some list like this
DEFINE VARIABLE procedurelist AS CHARACTER EXTENT 5
INITIAL [ "1.p", "2.p", "3.p", "4.p", "5.p"].
but this all procedures with input-output parameters and i want to execute this procedure, How can i do this? I have no idea how to do this.
The base of your solution is the RUN VALUE statement.
The manual states.
VALUE( extern-expression ) An expression that returns the name of the (local or remote) external procedure you want to run....
This basically means that you can input a string with the value of a program (or procedure) into your RUN statement.
If all input-output parameters are exactly the same you can do like this:
DEFINE VARIABLE procedurelist AS CHARACTER EXTENT 5 INITIAL [ "1.p", "2.p", "3.p", "4.p", "5.p"].
DEFINE VARIABLE iExtent AS INTEGER NO-UNDO.
DEFINE VARIABLE cVariable AS CHARACTER NO-UNDO.
DO iExtent = 1 TO EXTENT(procedurelist):
RUN VALUE(procedurelist[iExtent]) (INPUT-OUTPUT cVariable).
END.
If the parameters are different it gets trickier (but not impossible). The CREATE CALL and the Call Object can help you there. In this case you would need some kind of way to keep track of the different parameters as well.
Here's a basic example taken directly from the online help:
DEFINE VARIABLE hCall AS HANDLE NO-UNDO.
CREATE CALL hCall.
/* Invoke hello.p non-persistently */
hCall:CALL-NAME = "hello.p".
/* Sets CALL-TYPE to the default */
hCall:CALL-TYPE = PROCEDURE-CALL-TYPE.
hCall:NUM-PARAMETERS = 1.
hCall:SET-PARAMETER(1, "CHARACTER", "INPUT", "HELLO WORLD").
hCall:INVOKE.
/* Clean up */
DELETE OBJECT hCall.

Illegal type conversion VHDL

I was trying to return type std_logic_vector by type conversion in vhdl.
Here is my code:
function mul(num1,num2 : in std_logic_vector(7 DOWNTO 0)) return std_logic_vector is
variable v_TEST_VARIABLE1 : integer;
variable v_TEST_VARIABLE2 : integer;
variable n_times: integer:=1;
variable product: integer:=0;
begin
for n_times in 1 to v_TEST_VARIABLE2 loop
product:=product + v_TEST_VARIABLE1;
end loop;
return std_logic_vector(product);
end mul;
It gives "Illegal type conversion from std.standard.integer to ieee.std_logic_1164.std_logic_vector (numeric to array)." on compilation.
How do I return std_logic_vector in such a code?
See Russell's post first. If you use the VHDL-2008, numeric_std_unsigned package, then you can use just one conversion:
use ieee.numeric_std_unsigned.all ;
...
return to_std_logic_vector(product, length) ; -- long form
-- alternate short form
return to_slv(product, length) ;
Usage warning: for synthesis, I consider VHDL-2008 items to be on the bleeding edge of support. Hence, while I use VHDL-2008 frequently in my testbenches, I try to limit the usage of it in my RTL code to what can't be done using other methods. However, if you ever want to use code like this, it is it is important to try it out in your synthesis tool and submit a bug report against it if it does not work - that is the only way change happens.
You must first convert it to an unsigned. I hope you're using numeric_std? If so, your code looks like this. Note that to use to_unsigned you must know the length of your output std_logic_vector. So this either needs to be an input to your function or you can bound the return vector:
return std_logic_vector(to_unsigned(product, length));
More information about how to convert std_logic_vector to integer.

How to pass mulitiple data types in parameters to function?

F.e. I want to implement inc function:
FUNCTION inc RETURNS INT (INPUT-OUTPUT i AS INT, AddExpression AS INT):
i = i + AddExpression.
END FUNCTION.
to use it like this:
inc(tt-data.qty,1).
I didn't found how to override my function for DEC data type or how to combine both in one. If it possible I also want my function to deal with CHAR - kind of ADD-ENTRY. Maybe this basic functions are already implemented by someone? Something like this STLib on OEHive.
Plain old user-defined functions can only have a single signature. Your function definition is a bit "off". You are using an input-output parameter (which isn't "wrong" but it is odd) and you aren't returning a value -- which is wrong. It should look like this:
function inc returns integer ( input-output i as integer, addExpression as integer ):
i = i + addExpression.
return i.
end.
Procedures have somewhat more relaxed data-type rules and will do some type conversions automatically (such as an implied decimal to integer conversion). This would, for example, support passing a decimal that gets automatically rounded:
procedure z:
define input-output parameter i as integer no-undo.
define input parameter x as integer.
i = i + x.
return.
end.
You can overload method signatures if you create your function as a method of a class.
Something along these lines (untested):
class X:
method public integer inc( input-output i as integer, input addExpression as integer ):
i = i + addExpression.
return i.
end.
method public integer inc( input-output i as integer, input addExpression as character ):
i = i + integer( addExpression ).
return i.
end.
end.