<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://sdcc.sourceforge.net/mediawiki/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://sdcc.sourceforge.net/mediawiki/index.php?title=Builtins&amp;feed=atom&amp;action=history</id>
		<title>Builtins - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://sdcc.sourceforge.net/mediawiki/index.php?title=Builtins&amp;feed=atom&amp;action=history"/>
		<link rel="alternate" type="text/html" href="http://sdcc.sourceforge.net/mediawiki/index.php?title=Builtins&amp;action=history"/>
		<updated>2013-05-24T07:06:23Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.20.2</generator>

	<entry>
		<id>http://sdcc.sourceforge.net/mediawiki/index.php?title=Builtins&amp;diff=32&amp;oldid=prev</id>
		<title>Borutr: Created page with &quot;&lt;pre&gt; /* This document is meant for developers */  Implementation details of &quot;builtin&quot; functions in SDCC.  Built in functions are target/port specific and are defined in src/&lt;...&quot;</title>
		<link rel="alternate" type="text/html" href="http://sdcc.sourceforge.net/mediawiki/index.php?title=Builtins&amp;diff=32&amp;oldid=prev"/>
				<updated>2012-12-04T10:57:40Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;pre&amp;gt; - ‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;This document is meant for developers: &lt;/span&gt;  Implementation details of &amp;quot;builtin&amp;quot; functions in SDCC.  Built in functions are target/port specific and are defined in src/&amp;lt;...&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
/* This document is meant for developers */&lt;br /&gt;
&lt;br /&gt;
Implementation details of &amp;quot;builtin&amp;quot; functions in SDCC. &lt;br /&gt;
Built in functions are target/port specific and are defined in src/&amp;lt;arch&amp;gt;/main.c. &lt;br /&gt;
Here is a layout of the builtin structure (port.h).&lt;br /&gt;
&lt;br /&gt;
Front-End&lt;br /&gt;
---------&lt;br /&gt;
&lt;br /&gt;
#define MAX_BUILTIN_ARGS	16&lt;br /&gt;
/* definition of builtin functions */&lt;br /&gt;
typedef struct builtins&lt;br /&gt;
{&lt;br /&gt;
    char *name ;		/* name of builtin function */&lt;br /&gt;
    char *rtype;		/* return type as string : see typefromStr */&lt;br /&gt;
    int  nParms;		/* number of parms : max 8 */&lt;br /&gt;
    char *parm_types[MAX_BUILTIN_ARGS]; /* each parm type as string : see typeFromStr */&lt;br /&gt;
} builtins ;&lt;br /&gt;
&lt;br /&gt;
The port structure contains a pointer to a table of the builtin functions. Example of the&lt;br /&gt;
builtin function table.&lt;br /&gt;
&lt;br /&gt;
static builtins __ds390_builtins[] = {&lt;br /&gt;
    { &amp;quot;__builtin_memcpy_x2x&amp;quot;,&amp;quot;v&amp;quot;,3,{&amp;quot;cx*&amp;quot;,&amp;quot;cx*&amp;quot;,&amp;quot;i&amp;quot;}}, /* void __builtin_memcpy_x2x (xdata char *,xdata char *,int) */&lt;br /&gt;
    { &amp;quot;__builtin_memcpy_c2x&amp;quot;,&amp;quot;v&amp;quot;,3,{&amp;quot;cx*&amp;quot;,&amp;quot;cp*&amp;quot;,&amp;quot;i&amp;quot;}}, /* void __builtin_memcpy_c2x (xdata char *,code  char *,int) */&lt;br /&gt;
    { NULL , NULL,0, {NULL}} 			       /* mark end of table */&lt;br /&gt;
};    &lt;br /&gt;
&lt;br /&gt;
Here the function names are prefixed with &amp;quot;__builtin&amp;quot; this is not a requirement, standard C&lt;br /&gt;
library functions can also be defined in this table by a particular port to generate more&lt;br /&gt;
efficient code .&lt;br /&gt;
&lt;br /&gt;
Function &amp;quot;initBuiltIns&amp;quot; in SDCCsymt.c is invoked to initialize the symbol table with the&lt;br /&gt;
builtin functions. The return type &amp;amp; the parameter types are specified as encoded strings. &lt;br /&gt;
Function &amp;quot;typeFromStr&amp;quot; parses this encoded string and translates them into sym_link . &lt;br /&gt;
&lt;br /&gt;
/*-----------------------------------------------------------------*/&lt;br /&gt;
/* typeFromStr - create a typechain from an encoded string         */&lt;br /&gt;
/* basic types - 	'c' - char            			   */&lt;br /&gt;
/*   			's' - short				   */&lt;br /&gt;
/* 			'i' - int                                  */&lt;br /&gt;
/* 			'l' - long                                 */&lt;br /&gt;
/*                      'f' - float				   */&lt;br /&gt;
/*                      'v' - void				   */&lt;br /&gt;
/*                      '*' - pointer - default (GPOINTER)	   */&lt;br /&gt;
/* modifiers -          'u' - unsigned                             */&lt;br /&gt;
/* pointer modifiers -  'g' - generic                              */&lt;br /&gt;
/*                      'x' - xdata                                */&lt;br /&gt;
/*                      'p' - code                                 */&lt;br /&gt;
/*                      'd' - data                                 */                     &lt;br /&gt;
/*                      'F' - FUNCTION                             */                     &lt;br /&gt;
/* examples : &amp;quot;ig*&amp;quot; - generic int *				   */&lt;br /&gt;
/*            &amp;quot;cx*&amp;quot; - char xdata *                                 */&lt;br /&gt;
/*            &amp;quot;ui&amp;quot; -  unsigned int                                 */&lt;br /&gt;
/*-----------------------------------------------------------------*/&lt;br /&gt;
&lt;br /&gt;
So much for the front-end issues.&lt;br /&gt;
&lt;br /&gt;
iCode - implications.&lt;br /&gt;
---------------------&lt;br /&gt;
&lt;br /&gt;
Intermediate code (iCode) will ALWAYS generate a SEND for parameters of a builtin function with&lt;br /&gt;
a special flag (ic-&amp;gt;builtinSEND) set. This eliminates the need for special processing for different&lt;br /&gt;
memory models and --stack-auto option. All optimizations remain untouched. Example of intermediate code&lt;br /&gt;
generated for builtin&lt;br /&gt;
&lt;br /&gt;
void memcpy(xdata char *d,xdata char *s)&lt;br /&gt;
{&lt;br /&gt;
    __builtin_memcpy_x2x(d,s+10,10);&lt;br /&gt;
}&lt;br /&gt;
iTemp0 [k2 lr3:12 so:0]{ ia0 re1 rm0 nos0 ru0}{xdata char xdata * }{ sir@ _memcpy_d_1_1}[_memcpy_d_1_1] = recv &lt;br /&gt;
iTemp2 [k7 lr4:12 so:0]{ ia0 re0 rm0 nos0 ru0}{xdata char xdata * }[r5 r6 r7 ] &lt;br /&gt;
       := _memcpy_PARM_2 [k6 lr0:0 so:0]{ ia0 re0 rm0 nos0 ru0}{xdata char xdata * }&lt;br /&gt;
iTemp3 [k8 lr5:8 so:0]{ ia0 re0 rm0 nos0 ru0}{xdata char xdata * }[r0 r1 r2 ] = &lt;br /&gt;
       iTemp2 [k7 lr4:12 so:0]{ ia0 re0 rm0 nos0 ru0}{xdata char xdata * }[r5 r6 r7 ] + 0xa {literal unsigned char}&lt;br /&gt;
send iTemp0 [k2 lr3:12 so:0]{ ia0 re1 rm0 nos0 ru0}{xdata char xdata * }{ sir@ _memcpy_d_1_1}[_memcpy_d_1_1]&lt;br /&gt;
send iTemp3 [k8 lr5:8 so:0]{ ia0 re0 rm0 nos0 ru0}{xdata char xdata * }[r0 r1 r2 ]&lt;br /&gt;
send 0xa {literal int}&lt;br /&gt;
iTemp4 [k9 lr9:9 so:0]{ ia0 re0 rm0 nos0 ru1}{void} = &lt;br /&gt;
       call ___builtin_memcpy_x2x [k4 lr0:0 so:0]{ ia0 re0 rm0 nos0 ru0}{void function __builtin__}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Backend/Code generation for builtin functions.&lt;br /&gt;
----------------------------------------------&lt;br /&gt;
&lt;br /&gt;
When a SEND icode with the ic-&amp;gt;builtinSEND flag is detected by the code generator, it should go&lt;br /&gt;
into builtin function processing mode. E.g.&lt;br /&gt;
&lt;br /&gt;
	case SEND:&lt;br /&gt;
	  if (ic-&amp;gt;builtinSEND) genBuiltIn(ic);&lt;br /&gt;
	  else addSet (&amp;amp;_G.sendSet, ic);&lt;br /&gt;
	  break;&lt;br /&gt;
&lt;br /&gt;
SDCCicode.c contains a port/target independent routine (getBuiltinParms) which goes thru the &lt;br /&gt;
iCodes (starting from the first SEND (with builtinSEND set)) and returns the parameters and number &lt;br /&gt;
of parameters in an array, example. NOTE the function &amp;quot;getBuiltinParms&amp;quot; also marks the iCodes&lt;br /&gt;
as &amp;quot;generated&amp;quot; .&lt;br /&gt;
&lt;br /&gt;
/*-----------------------------------------------------------------*/&lt;br /&gt;
/* genBuiltIn - calls the appropriate function to  generating code */&lt;br /&gt;
/* for a built in function 					   */&lt;br /&gt;
/*-----------------------------------------------------------------*/&lt;br /&gt;
static void genBuiltIn (iCode *ic)&lt;br /&gt;
{&lt;br /&gt;
    operand *bi_parms[MAX_BUILTIN_ARGS];&lt;br /&gt;
    int nbi_parms;&lt;br /&gt;
    iCode *bi_iCode;&lt;br /&gt;
    symbol *bif;&lt;br /&gt;
&lt;br /&gt;
    /* get all the arguments for a built in function */&lt;br /&gt;
    bi_iCode = getBuiltinParms(ic,&amp;amp;nbi_parms,bi_parms);&lt;br /&gt;
&lt;br /&gt;
    /* which function is it */&lt;br /&gt;
    bif = OP_SYMBOL(IC_LEFT(bi_iCode));&lt;br /&gt;
    if (strcmp(bif-&amp;gt;name,&amp;quot;__builtin_memcpy_x2x&amp;quot;)==0) {&lt;br /&gt;
	genMemcpyX2X(bi_iCode,nbi_parms,bi_parms,0);&lt;br /&gt;
    } else if (strcmp(bif-&amp;gt;name,&amp;quot;__builtin_memcpy_c2x&amp;quot;)==0) {&lt;br /&gt;
	genMemcpyX2X(bi_iCode,nbi_parms,bi_parms,1);&lt;br /&gt;
    } else {&lt;br /&gt;
	werror(E_INTERNAL_ERROR,&amp;quot;unknown builtin function encountered\n&amp;quot;);&lt;br /&gt;
	return ;&lt;br /&gt;
    }&lt;br /&gt;
    return ;    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Borutr</name></author>	</entry>

	</feed>