I'm trying to convert an if condition of:
unless defined? SomeConstant
# do some stuff
end
Into part of a native C extension. Does anybody know how to do the defined? predicate check in the C API?
EDIT | I guess I could invoke:
rb_funcall(rb_cObject, rb_intern("const_defined?"), 1, rb_intern("SomeConstant"))
Though this is obviously slightly different, semantically.
If you trace through the 1.9.3 source, you'll find that defined? is implemented in insns.def:
DEFINE_INSN
defined
(rb_num_t op_type, VALUE obj, VALUE needstr)
/* ... */
switch (type) {
/* ... */
case DEFINED_CONST:
klass = v;
if (vm_get_ev_const(th, GET_ISEQ(), klass, SYM2ID(obj), 1)) {
expr_type = "constant";
}
break;
So when you defined? SomeConstant, you trickle through that big switch and end up calling vm_get_ev_const. The function is defined in vm_insnhelper.c:
static inline VALUE
vm_get_ev_const(rb_thread_t *th, const rb_iseq_t *iseq,
VALUE orig_klass, ID id, int is_defined)
That function happens to be static so you can't get at it. Looks like vm_get_ev_const is defined in terms of rb_const_defined and rb_const_defined_from and both of those should be available in your C so you could try those; but you'd have to find the right klass for those.
Or you could go with your idea and just use Object.const_defined?. One problem with that is that it won't do The Right Thing with things like A::B, you'd have to say Object.const_defined? :A && A.const_defined? :B for that as Object.const_defined? :'A::B' will just throw an exception in your face. A general solution here would require iteration and class lookups. However, if the classes that you're looking at are all in the top level namespace, then a simple Object.const_defined? should do the trick.
Related
I need to code an automata, and I bumped into this old need of a computed goto (ala fortran4 :) )
I need to code this in a portable ansi-C.
I want to stay away from the "don't do that", away from longjmp/setjmp, away from embedded ASM(), away from non ansi-C extensions.
Does anyone know how to do this?
Like I said in a comment, despite your plea to not use anything other than goto, standard C has nothing to offer.
Design your state appropriately, and pass a pointer to it to the handler functions for them to modify. That way the handler can setup the next function to call. Something like this:
struct state;
typedef void state_func(struct state*);
#define NULL_ACTION_ADDRESS (state_func*)0
struct state {
state_func *action;
int value1;
int value2;
};
#define INIT_STATE { initial_action, -1, -1}
state_func initial_action;
state_func handle_a;
state_func handle_b;
int main(void) {
struct state s = INIT_STATE;
while(s.action != NULL_ACTION_ADDRESS) {
(*s.action)(&s);
}
return 0;
}
void initial_action(struct state* ps) {
ps->action = &handle_a;
}
void handle_a(struct state* ps) {
ps->action = &handle_b;
}
void handle_b(struct state* ps) {
ps->action = NULL_ACTION_ADDRESS;
}
I think I got it, I reviewed all the various threads on this topics and I started to agree that that there where no ansi C solutions, yet I found an way to do this that fit my needs. All solution I saw on stackoverflow where based on the idea to 'get' the addr of a label, then stuff it into a table, then index this table and goto, this is both with gcc/clang non ansi extension or the asm extension.
I gave it another try tonite and got this.
In an include file named cgoto.h I have this
#ifndef CGOTO_dcl
#define CGOTO_dcl(N) int CGOTO_##N
#define CGOTO_LE(l) l,
#define CGOTO_LG(l) case l:goto l;
#define CGOTO_def(N) \
if(0){typedef enum {N(CGOTO_LE)} N; CGOTO_##N: switch(CGOTO_##N)\
{N(CGOTO_LG) default:CGOTO_##N=0;goto CGOTO_##N;}}
#define CGOTO(N,i) CGOTO_##N=i; goto CGOTO_##N;
#endif
The usage is like this
#include <stdio.h>
#include "cgoto.h"
int f(int x)
{ //...
CGOTO_dcl(gtb);
//...
# define gtb(L) L(l0) L(l1) L(l2)
CGOTO_def(gtb);
//...
CGOTO(gtb,x);
l0: printf("error\n");
return(0);
//...
l1:return(11);
l2:return(22);
l3:return(33);
}
int main()
{ printf("f(0)=%d f(1)=%d f(2)=%d,f(3)=%d\n",f(0),f(1),f(2),f(3));
}
In this implementation, the cost of jumping is 2 jumps and a switch() that is sequential, then optimisable. So this is reasonably performing compared to function call, a little less performing than &&label solution at the cost of portability.
With this implementation, labels code (semantic actions) are not confined into a switch() so we can implement jump table with shared semantic actions.
The index is assigned to a local goto_table_index, making the function using this re-entrant (multi threadable), though the optimiser can remove altogether this temp assignment.
The 1st Label in a jump table is 'special' (on this implementation) in the sense that it catch index out of bound, the first label is the 'error' label. If your code is bullet proof, i.e there is no way you can get an out of bound index, then the 1st label has not particular semantic.
CGOTO_dcl(gtb);
Declare the jump table 'gtb' own index as an auto integer so reentrant.
# define gtb(L) L(l0) L(l1) L(l2)
CGOTO_def(gtb);
Define a jump table named gtb, labels can be entered/removed with L(label) so it is pretty convenient, and this is symbolic by nature, i.e the labels are name with a meaning. With #define as a switch() case, labels addition/suppression often mean #define renumbering that is a problem.
The #define can be separated from the CGOTO_def() but it make more sense to keep them together. The CGOTO_def() though got to be placed after the function local declaration as it contain a switch() that is code.
A uniq jump table can be used in multiple place in the function.
CGOTO(gtb,x);
...
CGOTO(gtb,y);
A label may be entered in multiple jump table
# define gtb1(L) L(l0) L(l1) L(l2)
CGOTO_def(gtb1);
# define gtb2(L) L(l0) L(l4) L(l5)
CGOTO_def(gtb2);
So all in all, this may looks ugly, yet, the jump table definition though 2 line the #define and the CGOTO_def() is manageable and practical, semi performant, and portable.
We are back to FTN4 :)
Cheers,
Phi
Please help to understand one issue that I am facing when trying to customize the “select_sequence” of the sequence_library.
So I need to customize the “select_sequence” and make my sequences to run in exact order, which is written in external file.
For that reason I am stepping on the container of all registered sequence types: sequences[$]. And doing dynamic casting. If casting matches with my needed sequence I return number from the select_sequence function i.e. making the library to run the sequence.
This is the part from my code:
class cfgSeqncLib extends uvm_sequence_library #(seqItem_cfg);
`uvm_object_utils(cfgSeqncLib)
`uvm_sequence_library_utils(cfgSeqncLib)
rstSeqnc resetSequence;
function int unsigned select_sequence(int unsigned max);
static int unsigned counter;
select_sequence = counter;
counter++;
if (counter > max)
counter = 0;
foreach (sequences[i])
begin
if ($cast(resetSequence, sequences[i]) )
$display("%t: <><><>><>< ResetSeq: Casting IS Successfull", $time);
else
$display("%t: >>>>>>>>ResetSeq: Casting is NOT Successfull", $time);
end // foreach
endfunction
endclass
And I have added the reset sequence in the library using : `uvm_add_to_seq_lib(rstSeqnc, cfgSeqncLib) command.
The thing is that the $case never return 1 i.e. during simulation it always print
\ >>>>>>>>ResetSeq: Casting is NOT Successfull
Even though I can see that reset sequence is being run by the sequence_lib, but never casting returns true.
Can you please explain why?
If using the UVM code, I change the select_sequence function to following, than casting works:
function int unsigned select_sequence(int unsigned max);
static int unsigned counter;
uvm_object_wrapper wrap;
uvm_object obj;
select_sequence = counter;
counter++;
if (counter > max)
counter = 0;
foreach (sequences[i])
begin
wrap = sequences[i];
obj = factory.create_object_by_type(wrap,get_full_name(),
$sformatf("%s:%0d",wrap.get_type_name(),sequences_executed+1));
if ($cast(resetSequence, obj) )
$display("%t: !!!!!!! ResetSeq: Casting IS Successfull", $time);
else
$display("%t: >>>>>>>>ResetSeq: Casting is NOT Successfull", $time);
end // foreach
endfunction
During Simulaiton I am getting:
!!!!!!! ResetSeq: Casting IS Successfull
I cannot understand why casting in the the first version of select_sequence does not work but in second case it works. Can someone explain this to me please.
using the macro uvm_add_to_seq_lib to add the sequence into the library [`uvm_add_to_seq_lib(rstSeqnc, cfgSeqncLib) ] gives us the feeling that its adding an instance of the sequence into an array present in the sequence library and this instance can be used later in the sequence library.
But that is not the case and like the commonly used style within the uvm, the macro is adding a object of the type uvm_object_registry. This object is created (statically) once per class within the uvm_object_utils macros [ uvm_object_registry#(T,"S") type_id; ].
This mechanism is used within the factory classes/mechanism to create and use a lightweight wrapper(uvm_object_registry) of the object. This ensures that uvm framework is not creating the complete class till its actually needed. The instance of the class is only created at the time of calling the type_id::create function ( or other factory create functions).
So $cast(resetSequence, sequences[i]) in case 1 is really trying to cast the object_wrapper for the type resetSequence [uvm_object_registry#(resetSequence,"resetSequence") ] into resetSequence which fails .
resetSequence != uvm_object_registry#(resetSequence,"resetSequence")
Examining the macro itself we can see its not adding an instance of TYPE ( sequence) instead is calling get_type function which returns an object wrapper.
`define uvm_add_to_seq_lib(TYPE,LIBTYPE) \
static bit add_``TYPE``_to_seq_lib_``LIBTYPE =\
LIBTYPE::m_add_typewide_sequence(TYPE::get_type());
Also examining the code ( case 2 ) below we can see that its using the factory mechanism to create the instance of the class. This ensures that any type override in the environment will work and the sequence library will then use the new sequence over the registered one in the sequence library. In example 1 such a case is not possible as its attempting to directly use the instance of the class [ even if the code was fixed ] . So case 2 has advantages.
wrap = sequences[i];
obj = factory.create_object_by_type(wrap,get_full_name(),
$sformatf("%s:%0d",wrap.get_type_name(),sequences_executed+1));
if ($cast(resetSequence, obj) )
I was wondering if it is possible to create something like an array of macros.
I've implemented the following code which works:
struct led_cmds_
{
ioport_pin_t *commands[LED_COUNT] ;
};
struct led_cmds_ the_led_cmd_ ;
void populate() {
the_led_cmd_.commands[0] = SPECIFICPIN(0);
}
and in main:
int main(void)
{
//.....
populate();
LED_On(the_led_cmd_.commands[0]);
}
SPECIFICPIN(x) is macro defined as:
#define SPECIFICPIN(X) (LED##X##_PIN)
What I was hoping for is a way to is a way to do something like this:
#define ioport_pin_t* ARR_LED[LED_COUNT] \
for (int j = 0; j < LED_COUNT; j++) ARR_LED[j] = SPECIFICPIN(j);
and then only need to call the following when I want to use the specific pin
LED_On(ARR_LED[some_number])
when I try to do that I get an ARR_LED undeclared (first use in this function) error.
When I try to call SPECIFICPIN(x) where x is an int iterator in a for loop for example, I get an error saying something like 'LEDx_PIN' undeclared...
You need to work on your terminology. An array of macros is not possible. Macros are no data type, but rather pure text replacement before your program is actually compiled.
I guess " populate an array using macros " is what you want to do. But it is not possible to do that in a compile-time loop - What you seem to want to achieve with your ioport_pin_t macro attempt. Macros do not have the capability to expand to more instances of text elements than you have initially given. There is no such feature as looping at compile time through macro expansions and do repetitive expansion of macros.
Your for loop loops at run-time, while the macro is being expanded at compile-time. Once you have made yourself aware what is done by the preprocessor what is done by the compiler, and what is done at run-time by the finished program, you will see that will not work.
Something like
#define P(X) {(LED##X##_PIN)}
ioport_pin_t *commands[LED_COUNT] = {
P(0), P(1), P(2),......}
#undefine P
Would be the closest thing possible to what you seem to want. Note the main use of the pre-processor is not to save you typing effort - You would be better off using copy & paste in your editor, achieve the same thing and have clearer code.
An array as tofro's answer is the way to go. However in cases that couldn't be solved simply with an array then there's another way with switch
#define SPECIFICPIN(X) (LED##X##_PIN)
void setpin(int pin, int value)
{
switch (pin)
{
case 1:
SPECIFICPIN(1) = value;
doSomething(); // if needed
break;
case x: ...
default: ...
}
}
I'm writing an app that calls ruby code from c. I am having a little difficulty and wondered if anyone could point me in the rite direction.
I currently have in my C.
#include ruby.h
main()
{
ruby_init();
rb_require("myRubyFile");
rb_funcall(rb_module_new(), rb_intern("RubyFunction"), 0, NULL);
}
My ruby file is in the same directory as my c file and is called myRubyFile.rb and contains a definition of the function RubyFunction().
This is a cut down of what I actually want to do, just making it more readable for others. I just require some feedback as to whether this is the correct method to call ruby code from my c file.
Regards
Short answer:
extern VALUE rb_vm_top_self(void); /* Assumes 1.9. Under 1.8, use the global
* VALUE ruby_top_self
*/
...
rb_funcall(rb_vm_top_self(), /* irb> RubyFunction() */
rb_intern("RubyFunction"), /* irb> self.RubyFunction() # same thing */
0,
NULL);
Longer answer:
The first argument to rb_funcall is the receiver of the method call.
Assuming you defined RubyFunction() outside of any explicit class or module context, then it is added to the eigenclass of the implicit, main object at the "top level" of every ruby vm.
In ruby, this object is accessible as the top-level self:
$ cat myRubyFile.rb
# file: myRubyFile.rb
def foo
puts "foo"
end
$ irb
irb> require "myRubyFile"
=> true
irb> foo
foo
=> nil
irb> self.foo() # same thing, more explicit
foo
=> nil
irb> self
=> main
In C under 1.9 it is accessible as indicated above.
I try to use the following approach:
Basic struct to share data
typedef struct ruby_shared_data {
VALUE obj;
ID method_id;
int nargs;
VALUE args[4];
} ruby_shared_data;
Create a function for call ruby objects on some part of your code
static VALUE ruby_callback(VALUE ptr) {
ruby_shared_data *data = (ruby_shared_data*)ptr;
return rb_funcall2(data->obj,data->method_id,data->nargs,data->args);
}
On some part of your code...
ruby_shared_data rbdata;
rbdata.obj = obj;
rbdata.method_id = rb_intern("mycallback");
rbdata.nargs = 1;
rbdata.args[0] = rb_str_new2("im a parameter");
int error = 0;
VALUE result = rb_protect(ruby_callback,(VALUE)&rbdata,&error);
if (error)
throw "Ruby exception on callback";
Is always a good idea to wrap rb_funcall with rb_protect.
Another interesting thing is to know the parameters of the callback, one approach is the following
ruby_shared_data rbdata;
rbdata.obj = callback;
rbdata.method_id = rb_intern("arity");
rbdata.nargs = 0;
int error = 0;
VALUE result = rb_protect(ruby_callback,(VALUE)&rbdata,&error);
if (error)
throw "Ruby exception on callback";
narguments = NUM2INT(result);
I don't like to call ruby from inside C unless you have complex C project which you don't want to re-build in ruby.
There are two ways to interact between C and ruby. You can extend ruby with code written in C. See SWIG.
Or you can embed ruby, see here, here and here.
BTW, what do you mention is "embed" ruby, not "extend" ruby.
I want to build a lexer in C and I am following the dragon book, I can understand the state transitions but how to implement them?
Is there a better book?
The fact that I have to parse a string through a number of states so that I can tell whether the string is acceptable or not!
You can implement simple state transitions with a single state variable, for example if you want to cycle through the states start->part1->part2->end then you can use an enum to keep track of the current state and use a switch statement for the code you want to run in each state.
enum state { start=1, part1, part2, end} mystate;
// ...
mystate = start;
do {
switch (mystate) {
case start:
// ...
case part1:
// ...
case part2:
// ...
if (part2_end_condition) mystate = end; // state++ will also work
// Note you could also set the state back to part1 on some condition here
// which creates a loop
break;
}
} while (mystate != end);
For more complex state transitions that depend on several variables, you should use tables/arrays like this:
var1 var2 var_end next_state
0 0 0 state1
0 1 0 state2
1 0 0 state3
1 1 0 state4
-1 -1 1 state_end // -1 represents "doesn't matter" here
G'day,
Assuming you mean The Dragon book on compiler design, I'd recommend having a look around this page on compiler tools.
The page itself is quite small but has links through to various excellent resources on lexical analysers.
HTH
cheers,
There's more than one way to do it. Every regular expression corresponds directly to a simple structured program. For example, an expression for numbers could be this:
// regular expression
digit* [.digit*]
and the corresponding C code would be:
// corresponding code
while(DIGIT(*pc)) pc++;
if (*pc=='.'){
pc++;
while(DIGIT(*pc)) pc++;
}
The transition-table way of building lexers is, in my opinion, needlessly complicated, and obviously runs slower.
If you're looking for a more modern treatment than the dragon book(s) : Andrew W. Appel and Maia Ginsburg, Modern Compiler Implementation in C, Cambridge University Press, 2008.
Chapter 2 is focused on Lexical Analysis : Lexical tokens, Regular expressions, Finite automata; Nondeterministic Finite Automata; Lexical analyzer generators
Look at the Table of Contents
The program flex (a clone of lex) will create a lexer for you.
Given an input file with the lexer rules, it will produce a C file with an implementation of a lexer for those rules.
You can thus check the output of flex for how to write a lexer in C. That is, if you don't just want to use flex's lexer...