1 /* This document is meant for developers */
3 Implementation details of "builtin" functions in SDCC.
4 Built in functions are target/port specific and are defined in src/<arch>/main.c.
5 Here is a layout of the builtin structure (port.h).
10 #define MAX_BUILTIN_ARGS 16
11 /* definition of builtin functions */
12 typedef struct builtins
14 char *name ; /* name of builtin function */
15 char *rtype; /* return type as string : see typefromStr */
16 int nParms; /* number of parms : max 8 */
17 char *parm_types[MAX_BUILTIN_ARGS]; /* each parm type as string : see typeFromStr */
20 The port structure contains a pointer to a table of the builtin functions. Example of the
21 builtin function table.
23 static builtins __ds390_builtins[] = {
24 { "__builtin_memcpy_x2x","v",3,{"cx*","cx*","i"}}, /* void __builtin_memcpy_x2x (xdata char *,xdata char *,int) */
25 { "__builtin_memcpy_c2x","v",3,{"cx*","cp*","i"}}, /* void __builtin_memcpy_c2x (xdata char *,code char *,int) */
26 { NULL , NULL,0, {NULL}} /* mark end of table */
29 Here the function names are prefixed with "__builtin" this is not a requirement, standard C
30 library functions can also be defined in this table by a particular port to generate more
33 Function "initBuiltIns" in SDCCsymt.c is invoked to initialize the symbol table with the
34 builtin functions. The return type & the parameter types are specified as encoded strings.
35 Function "typeFromStr" parses this encoded string and translates them into sym_link .
37 /*-----------------------------------------------------------------*/
38 /* typeFromStr - create a typechain from an encoded string */
39 /* basic types - 'c' - char */
45 /* '*' - pointer - default (GPOINTER) */
46 /* modifiers - 'u' - unsigned */
47 /* pointer modifiers - 'g' - generic */
51 /* examples : "ig*" - generic int * */
52 /* "cx*" - char xdata * */
53 /* "ui" - unsigned int */
54 /*-----------------------------------------------------------------*/
56 So much for the front-end issues.
61 Intermediate code (iCode) will ALWAYS generate a SEND for parameters of a builtin function with
62 a special flag (ic->builtinSEND) set. This eliminates the need for special processing for different
63 memory models and --stack-auto option. All optimizations remain untouched. Example of intermediate code
66 void memcpy(xdata char *d,xdata char *s)
68 __builtin_memcpy_x2x(d,s+10,10);
70 iTemp0 [k2 lr3:12 so:0]{ ia0 re1 rm0 nos0 ru0}{xdata char xdata * }{ sir@ _memcpy_d_1_1}[_memcpy_d_1_1] = recv
71 iTemp2 [k7 lr4:12 so:0]{ ia0 re0 rm0 nos0 ru0}{xdata char xdata * }[r5 r6 r7 ]
72 := _memcpy_PARM_2 [k6 lr0:0 so:0]{ ia0 re0 rm0 nos0 ru0}{xdata char xdata * }
73 iTemp3 [k8 lr5:8 so:0]{ ia0 re0 rm0 nos0 ru0}{xdata char xdata * }[r0 r1 r2 ] =
74 iTemp2 [k7 lr4:12 so:0]{ ia0 re0 rm0 nos0 ru0}{xdata char xdata * }[r5 r6 r7 ] + 0xa {literal unsigned char}
75 send iTemp0 [k2 lr3:12 so:0]{ ia0 re1 rm0 nos0 ru0}{xdata char xdata * }{ sir@ _memcpy_d_1_1}[_memcpy_d_1_1]
76 send iTemp3 [k8 lr5:8 so:0]{ ia0 re0 rm0 nos0 ru0}{xdata char xdata * }[r0 r1 r2 ]
77 send 0xa {literal int}
78 iTemp4 [k9 lr9:9 so:0]{ ia0 re0 rm0 nos0 ru1}{void} =
79 call ___builtin_memcpy_x2x [k4 lr0:0 so:0]{ ia0 re0 rm0 nos0 ru0}{void function __builtin__}
82 Backend/Code generation for builtin functions.
83 ----------------------------------------------
85 When a SEND icode with the ic->builtinSEND flag is detected by the code generator, it should go
86 into builtin function processing mode. E.g.
89 if (ic->builtinSEND) genBuiltIn(ic);
90 else addSet (&_G.sendSet, ic);
93 SDCCicode.c contains a port/target independent routine (getBuiltinParms) which goes thru the
94 iCodes (starting from the first SEND (with builtinSEND set)) and returns the parameters and number
95 of parameters in an array, example. NOTE the function "getBuiltinParms" also marks the iCodes
98 /*-----------------------------------------------------------------*/
99 /* genBuiltIn - calls the appropriate function to generating code */
100 /* for a built in function */
101 /*-----------------------------------------------------------------*/
102 static void genBuiltIn (iCode *ic)
104 operand *bi_parms[MAX_BUILTIN_ARGS];
109 /* get all the arguments for a built in function */
110 bi_iCode = getBuiltinParms(ic,&nbi_parms,bi_parms);
112 /* which function is it */
113 bif = OP_SYMBOL(IC_LEFT(bi_iCode));
114 if (strcmp(bif->name,"__builtin_memcpy_x2x")==0) {
115 genMemcpyX2X(bi_iCode,nbi_parms,bi_parms,0);
116 } else if (strcmp(bif->name,"__builtin_memcpy_c2x")==0) {
117 genMemcpyX2X(bi_iCode,nbi_parms,bi_parms,1);
119 werror(E_INTERNAL_ERROR,"unknown builtin function encountered\n");