1. Adding a new builtin function

I started grepping through the source code today in an attempt to add a new builtin function. For example, I want to add a simple function that takes 1 input (atom) and returns 1 output (atom): atom foo(atom a). I found some tips/instructions in the source files, but I'm missing some key steps. In reswors.h it says:

  remember to update reswords.e, opnames.e, 
   opnames.h, optable[], localjumptab[] 
   in be_execute.c, be_runtime.c. redef.h, 
   emit.e, keylist.e, compile.e, be_syncolor.c 

I've taken a stab at editing these files with a new reserved word 'foo' and using the builtin 'cos' function as an example. I gave my function a new opcode and added entries to the files listed above. After rebuilding euphoria, I get the following when trying to use 'foo' in code:

Fatal run-time error: 
BAD IL OPCODE: i is 4, word is 1590060 (max=12), len is 1076948992 

The error occurs in the function code_set_pointers in be_execute.c. Any suggestions?

Thanks,
Ira

new topic     » topic index » view message » categorize

2. Re: Adding a new builtin function


A lot of Eu code is in the form of includes. Why don't you do that?

useless

new topic     » goto parent     » topic index » view message » categorize

3. Re: Adding a new builtin function

eukat said...


A lot of Eu code is in the form of includes. Why don't you do that?

I'm guessing that this is an attempt to add support for some low-level ARM hardware functionality that would be difficult to do in another way, based on some of the OPs other recent posts...

new topic     » goto parent     » topic index » view message » categorize

4. Re: Adding a new builtin function

jimcbrown said...

I'm guessing that this is an attempt to add support for some low-level ARM hardware functionality that would be difficult to do in another way, based on some of the OPs other recent posts...

Exactly! Include files or shared libraries are certainly an option, but I'm also curious about Eu's internals so I wanted to give this a try. Either way, it's a good learning experience.

Thanks,
Ira

new topic     » goto parent     » topic index » view message » categorize

5. Re: Adding a new builtin function

Jerome said...
jimcbrown said...

I'm guessing that this is an attempt to add support for some low-level ARM hardware functionality that would be difficult to do in another way, based on some of the OPs other recent posts...

Exactly! Include files or shared libraries are certainly an option, but I'm also curious about Eu's internals so I wanted to give this a try. Either way, it's a good learning experience.

Thanks,
Ira


I am not sure mod'ing the Eu internals for Pi support is a great idea. If there's voting, i vote for a DLL or include for each micro-mobo available, because the interface for a Pi will be different than an Arduino, or an Oak, or Rabbit 2000, or PICAXE, or a NextBestThing.

useless

new topic     » goto parent     » topic index » view message » categorize

6. Re: Adding a new builtin function

useless_ said...

I am not sure mod'ing the Eu internals for Pi support is a great idea. If there's voting, i vote for a DLL or include for each micro-mobo available, because the interface for a Pi will be different than an Arduino, or an Oak, or Rabbit 2000, or PICAXE, or a NextBestThing.

Agreed. While adding internal routines is certainly useful for many things, for this task an external library would probably be more helpful and a lot more portable, i.e. it could be excluded when not necessary. Now, adding a library would be much easier if Euphoria could better integrate with its own compiled libraries, i.e. importing the symbol table instead of manually wrapping all the routines.

-Greg

new topic     » goto parent     » topic index » view message » categorize

7. Re: Adding a new builtin function

ghaberek said...
useless_ said...

I am not sure mod'ing the Eu internals for Pi support is a great idea. If there's voting, i vote for a DLL or include for each micro-mobo available, because the interface for a Pi will be different than an Arduino, or an Oak, or Rabbit 2000, or PICAXE, or a NextBestThing.

Agreed. While adding internal routines is certainly useful for many things, for this task an external library would probably be more helpful and a lot more portable, i.e. it could be excluded when not necessary. Now, adding a library would be much easier if Euphoria could better integrate with its own compiled libraries, i.e. importing the symbol table instead of manually wrapping all the routines.

-Greg

I'm not advocating that the Eu internals need to be modified for Pi support, using external libraries/includes for GPIO and SPI is good with me. I am still curious about the internals of Eu though, which brings me back to my original question. Is there any documentation on adding builtin routines or would one of the devs mind sharing notes/tips?

Thanks,
Ira

new topic     » goto parent     » topic index » view message » categorize

8. Re: Adding a new builtin function

Jerome said...

I started grepping through the source code today in an attempt to add a new builtin function. For example, I want to add a simple function that takes 1 input (atom) and returns 1 output (atom): atom foo(atom a). I found some tips/instructions in the source files, but I'm missing some key steps. In reswors.h it says:

  remember to update reswords.e, opnames.e, 
   opnames.h, optable[], localjumptab[] 
   in be_execute.c, be_runtime.c. redef.h, 
   emit.e, keylist.e, compile.e, be_syncolor.c 

I think we need to update the hints. You'll also definitely need to update shift.e.

Jerome said...
Fatal run-time error: 
BAD IL OPCODE: i is 4, word is 1590060 (max=12), len is 1076948992 

The error occurs in the function code_set_pointers in be_execute.c. Any suggestions?

This error suggests that when the previous opcode was dealt with, i was incremented by an incorrect amount. I'm not sure what "max" is supposed to be (I suspect MAX_OPCODE), but it's being reported with the value for len. The statement is clearly wrong:

 
if (word > MAX_OPCODE || word < 1) { 
	RTFatal("BAD IL OPCODE: i is %d, word is %d (max=%d), len is %d", 
			i, word, len); 
} 

When we parse euphoria code, everything is represented as indices into the symbol table, which is just a sequence in the front end. When your code is actually executed by the back end, however, pointers are much more efficient. This function converts the indices into pointers (the symbol table is converted from a sequence to the corresponding C struct in backend.e).

We use the macro, SET_OPERAND() to translate from an index into the symbol table to a pointer to the correct symtab entry struct, and SET_JUMP() to translate from an index pointing to a particular point in the IL code to where that instruction is in memory.

Different opcodes have different numbers and types of arguments, of course, though many share these characteristics with each other, as you'll notice the groupings of the various opcodes. When you create a new built-in, you have to put yours in the appropriate place or write a customized version if you need to.

There are a lot of places that need to be updated when you add a built-in. I recommend updating dis.e so that you can take a look at the generated IL code to make sure the front end is working correctly. Assuming you're adding foo() to be similar to cos(), you would just need to add the following to dis.e:

procedure opFOO() 
	unary() 
end procedure 

We should really document this whole process better. Maybe a wiki page.

Matt

new topic     » goto parent     » topic index » view message » categorize

9. Re: Adding a new builtin function

mattlewis said...

Different opcodes have different numbers and types of arguments, of course, though many share these characteristics with each other, as you'll notice the groupings of the various opcodes. When you create a new built-in, you have to put yours in the appropriate place or write a customized version if you need to.

There are a lot of places that need to be updated when you add a built-in. I recommend updating dis.e so that you can take a look at the generated IL code to make sure the front end is working correctly. Assuming you're adding foo() to be similar to cos(), you would just need to add the following to dis.e:

procedure opFOO() 
	unary() 
end procedure 

We should really document this whole process better. Maybe a wiki page.

Thanks for the detailed explanation! I was able to get my function foo working with your help. Also, I would personally like to see a wiki page about some of the inner workings of Euphoria. It would definately be beneficial for me and others!

Thanks,
Ira

new topic     » goto parent     » topic index » view message » categorize

10. Re: Adding a new builtin function


In case you missed it, Eu can load machine code into memory and execute it. There's various libs in the archives that do this.

useless

new topic     » goto parent     » topic index » view message » categorize

11. Re: Adding a new builtin function

eukat said...

In case you missed it, Eu can load machine code into memory and execute it. There's various libs in the archives that do this.

Using a library is easier. Things like the ARM version of the NX bit (http://www.slideshare.net/scovetta/android-attacks) make this trickier to do nowadays.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu