Regex in SV or UVM - system-verilog

What functions do I need to call to use Regular Expressions in Systemverilog/UVM?
Note: I'm not asking how to use regular expressions, just method names.

First, if you want to use regular expression, you'll need to make sure you're using a UVM library compiled together with its DPI code (i.e. the UVM_NO_DPI define isn't set).
The methods you want to use are located in dpi/uvm_regex.svh. The main function is uvm_re_match(...), which takes as an argument a regular expression and the string to match against. This is basically a wrapper around the regexec(...) C function found in the regex.h library. It will return 0 on a match.
Another function you might want to use is uvm_glob_to_re(...) which can convert from a glob expression (the kind you get in a Linux shell) to a true regular expression.

Related

Why do string macros in Julia use ...?

I was looking at the source for the r_str macro in Julia, which parses r"text" into Regex("text"). The second argument is flags..., which passes flags into the regex, like i for case insensitive, and so on.
I was playing with this myself and got:
julia> macro a_str(p, flags...)
print(flags)
p
end
julia> a"abc"iii
("iii",)"abc"
So it seems that the iii is all passed in as the first flag. In that case, why is there the ... on the flags. Is it possible to pass in more than one element of flags to the macro?
When this question was originally asked, a macro expander – i.e. the function defined with the macro keyword, which is called to transform the expressions passed to a macro into a single output expression – was not a generic function, but rather an anonymous function, which were a different kind of function in Julia 0.4 and earlier. At that point, the only way to write an anonymous function signature which could work for either one or two arguments was to use a trailing varargs argument, which is why this pattern was used to define string macros. In Julia 0.5 all functions have become generic functions, including anonymous functions and macro expanders. Thus, you can now write a macro a variety of ways, including the old way of using a varargs argument after the string argument:
# old style
macro rm_str(raw, rest...)
remove = isempty(rest) ? "aeiouy" : rest[1]
replace(raw, collect(remove), "")
end
# new style with two methods
macro rm_str(raw)
replace(raw, ['a','e','i','o','u','y'], "")
end
macro rm_str(raw, remove)
replace(raw, collect(remove), "")
end
# new style with default second argument
macro rm_str(raw, remove="aeiouy")
replace(raw, collect(remove), "")
end
These all result in the same non-standard string literal behavior:
julia> rm"foo bar baz"
"f br bz"
julia> rm"foo bar baz"abc
"foo r z"
The string literal produces the string with the flagged letters stripped from it, defaulting to stripping out all the ASCII vowels ("aeiouy"). The new approach of using a second argument with a default is the easiest and clearest in this case, as it will be in many cases, but now you can use whichever approach is best for the circumstances.
With an explicit call like
#a_str("abc", "iii", "jjj")
you can pass multiple flags. But I'm not aware of a way to make this work with a"abc"ijk syntax.
I don't believe it is possible, and the documentation doesn't provide an example where that would be used. In addition, the mostly-fully-compliant JuliaParser.jl doesn't support multiple flags either. Perhaps open an PR on Julia changing that?

what is the difference between 'define as' to 'define as computed' in specman?

The difference between the two is not so clear from the Cadence documentation.
Could someone please elaborate on the difference between the two?
A define as macro is just a plain old macro that you probably know from other programming languages. It just means that at some select locations in the macro code you can substitute your own code.
A define as computed macro allows you to construct your output code programmatically, by using control flow statements (if, for, etc.). It acts kind of like a function that returns a string, with the return value being the code that will be inserted in its place by the pre-processor.
With both define as and define as computed macros you define a new syntactic construct of a given syntactic category (for example, <statement> or <action>), and you implement the replacement code that replaces a construct matching the macro match expression (or pattern).
In both cases the macro match expression can have syntactic arguments that are used inside the replacement code and are substituted with the actual code strings used in the matched code.
The difference is that with a define as macro the replacement code is just written in the macro body.
With a define as computed macro you write a procedural code that computes the desired replacement code text and returns it as a string. It's effectively a method that returns string, you can even use the result keyword to assign the resulting string, just like in any e method.
A define as computed macro is useful when the replacement code is not fixed, and can be different depending on the exact macro argument values or even semantic context (for example, in some cases a reflection query can be used to decide on the exact replacement code).
(But it's important to remember that even define as computed macros are executed during compilation and not at run time, so they cannot query actual run time values of fields or variables to decide on the resulting replacement code).
Here are some important differences between the two macro kinds.
A define as macro is more readable and usually easier to write. You just write down the code that you want to be created.
Define as computed macros are stronger. Everything that can be implemented with define as, can also be implemented with define as computed, but not vice versa. When the replacement code is not fixed, define as is not sufficient.
A define as macro can be used immediately after its definition. If the construct it introduces is used in the statement just following the macro, it will already be matched. A define as computed macro can only be used in the next file, and is not usable in the same file in which the macro is defined.

Is "my" a function in Perl?

I know that my is used to declare a variable local to a block or file. I have always assumed that my is a keyword in Perl. But I was just told that it's actually a function. One of the proofs is that perldoc puts my under the “Functions” section, see http://perldoc.perl.org/functions/my.html.
How does a function do the job of declaring local variables?
my is not a function, it's just clumped together with functions (in perl documentation) because it works like a function.
If you look at perldoc perlfunc, it is saith,
Here are Perl's functions (including things that look like functions, like some keywords and named operators) arranged by category...
then a bit below that
Keywords related to scoping
caller, import, local, my, our, package, state, use
Specifically, note that the word “keyword” was used there instead of “function”
So that implies that you would find some non-functions (e.g. keywords) under Perl functions A-Z
Another way of saying this: if something is listed under “Functions” in perldoc, it is not necessarily a function – it can be a keyword or named operator which acts like a function.
Yes, by Perl's (very unique) definition, my is a function. The opening paragraph of perlfunc defines "function":
The functions in this section can serve as terms in an expression. They fall into two major categories: list operators and named unary operators.
my is a named operator. But it's special in two ways:
In addition to behaving like a function (that allocates a new variable and returns that variable), it has a compile-time effect.
my ... is a unary operator, but it can accept multiple arguments when parens are used.
If on the other hand you were ask if my was a function by C's definition, then no. my is not a C function. Neither is print, open, chr, etc. Everything in perlfunc is an operator; none of them are functions.
Finally, print, open and chr are far closer to a person's conception of a function than my. To be more precise, few people would consider my to be a function. It's more of a technicality than anything meaningful that it matches perfunc's definition of function.
See also:
What are perl built-in operators/functions?
Why does this [my] variable keep its value

Common Lisp Unbound Variable

Is it possible to use an uninitialized variable as a function argument? For an assignment, I have to use the CLOS to write a semantic network system, and my professor included a test function to test our output, and one of them specifies:
(print (def-concept Human)),
which implies passing the argument Human to the function def-concept. When running this test function, I cannot get away from the error (in Allegro CL):
Error: Attempt to take the value of the unbound variableHUMAN'.`
As this is the first function in the test, there is no initializing of any variables before this. Is there any way to get around passing an uninitialized variable as an argument of a function?
Thanks in advance.
It is not possible to use an unitialized value as a function argument in a regular Common Lisp function call. Common Lisp uses eager evaluation: argument expressions are reduced to their values before the function call takes place.
I suspect maybe you don't exactly understand the structure of the homework assignment.
If def-concept is a function which expects a value, and human is not defined, you simply cannot test that function.
Perhaps you are expected to define the variable human and then load the file containing (print (def-concept human)).
Just because there is nothing prior to that form in the same file doesn't mean that no prior evaluation is possible. Other files can be loaded before that file, or forms evaluated in the listener.

Can the C preprocessor perform simple string manipulation?

This is C macro weirdness question.
Is it possible to write a macro that takes string constant X ("...") as argument and evaluates to sting Y of same length such that each character of Y is [constant] arithmetic expression of corresponding character of X.
This is not possible, right ?
No, the C preprocessor considers string literals to be a single token and therefore it cannot perform any such manipulation.
What you are asking for should be done in actual C code. If you are worried about runtime performance and wish to delegate this fixed task at compile time, modern optimising compilers should successfully deal with code like this - they can unroll any loops and pre-compute any fixed expressions, while taking code size and CPU cache use patterns into account, which the preprocessor has no idea about.
On the other hand, you may want your code to include such a modified string literal, but do not want or need the original - e.g. you want to have obfuscated text that your program will decode and you do not want to have the original strings in your executable. In that case, you can use some build-system scripting to do that by, for example, using another C program to produce the modified strings and defining them as macros in the C compiler command line for your actual program.
As already said by others, the preprocessor sees entire strings as tokens. There is only one exception the _Pragma operator, that takes a string as argument and tokenizes its contents to pass it to a #pragma directive.
So unless your targeting a _Pragma the only way to do things in the preprocessing phases is to have them written as token sequences, manipulate them and to stringify them at the end.