Check-in [d6a2d38950]
Overview
Comment:Wrapping some Tcl-specific changes to TCC into TclTCC
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:d6a2d389505dfc7384c84f3e368e0d669683d830
User & Date: rkeene on 2014-05-01 23:50:52
Other Links: manifest | tags
Context
2014-05-02
00:54
Added start of initial patches to TCC needed for integration with Tcl check-in: 08662daa7c user: rkeene tags: trunk
2014-05-01
23:50
Wrapping some Tcl-specific changes to TCC into TclTCC check-in: d6a2d38950 user: rkeene tags: trunk
23:35
Corrected call to tcc configure check-in: 013ffc32d5 user: rkeene tags: trunk
Changes

Modified tcltcc.c from [07a867c26b] to [fae14f0ce9].

1
2
3
4

5
6
7
8
9
10
11
..
16
17
18
19
20
21
22
23





24
25
26
27
28
29
30
31




32

33
34
35

36
37

38

39
40
41
42

43
44
45
46
47
48
49
50
51
52
53
54
55
56


57
58
59
60
61
62
63
...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
...
211
212
213
214
215
216
217

218

219
220
221
222

223
224
225
226

227
228
229
230
231
232
233
234

235
236



237

238
239
240
241
242
243
244
245
246

247
248
249

250
251

252
253
254
255
256
/*
 *  TclTCC - Tcl binding to Tiny C Compiler
 * 
 *  Copyright (c) 2007 Mark Janssen

 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
................................................................................
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <tcl.h>
#include "tcc.h"






static void TccErrorFunc(Tcl_Interp * interp, char * msg) {
    Tcl_AppendResult(interp, msg, "\n", NULL);
}


static void TccCCommandDeleteProc (ClientData cdata) {
    TCCState * s ;
    s = (TCCState *)cdata;




    Tcl_DecrRefCount(s->tcc_lib_path);

    /* We can delete the compiler if the output was not to memory */
    if (s->output_type != TCC_OUTPUT_MEMORY) {
        tcc_delete(s);

    }
}



static int TccHandleCmd ( ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj * CONST objv[]){
    unsigned long val;
    int index;
    int res;

    TCCState * s = (TCCState *)cdata ;
    Tcl_Obj * sym_addr;

    static CONST char *options[] = {
        "add_include_path", "add_file",  "add_library", 
        "add_library_path", "add_symbol", "command", "compile",
        "define", "get_symbol", "output_file", "undefine",    (char *) NULL
    };
    enum options {
        TCLTCC_ADD_INCLUDE, TCLTCC_ADD_FILE, TCLTCC_ADD_LIBRARY, 
        TCLTCC_ADD_LIBRARY_PATH, TCLTCC_ADD_SYMBOL, TCLTCC_COMMAND, TCLTCC_COMPILE,
        TCLTCC_DEFINE, TCLTCC_GET_SYMBOL, TCLTCC_OUTPUT_FILE, TCLTCC_UNDEFINE
    };




    if (objc < 2) {
        Tcl_WrongNumArgs(interp, 1, objv, "subcommand arg ?arg ...?");
        return TCL_ERROR;
    }

    if (Tcl_GetIndexFromObj(interp, objv[1], options, "option", 0,
................................................................................
            tcc_add_symbol(s,Tcl_GetString(objv[2]),val); 
            return TCL_OK; 
        case TCLTCC_COMMAND:
            if (objc != 4) {
                Tcl_WrongNumArgs(interp, 2, objv, "tclname cname");
                return TCL_ERROR;
            }
            if (!s->relocated) {     
                if(tcc_relocate(s)!=0) {
                    Tcl_AppendResult(interp, "relocating failed", NULL);
                    return TCL_ERROR;
                } else {
                    s->relocated=1;
                }
            }
            if (tcc_get_symbol(s,&val,Tcl_GetString(objv[3]))!=0) {
		    Tcl_AppendResult(interp, "symbol '", Tcl_GetString(objv[3]),"' not found", NULL);
		    return TCL_ERROR;
	    }

            /*printf("symbol: %x\n",val); */
            Tcl_CreateObjCommand(interp,Tcl_GetString(objv[2]),(void *)val,NULL,NULL);
            return TCL_OK;
        case TCLTCC_COMPILE:
            if(s->relocated == 1) {
                Tcl_AppendResult(interp, "code already relocated, cannot compile more",NULL);
                return TCL_ERROR;
            }
            if (objc != 3) {
                Tcl_WrongNumArgs(interp, 2, objv, "ccode");
                return TCL_ERROR;
            } else {
................................................................................
            tcc_define_symbol(s,Tcl_GetString(objv[2]),Tcl_GetString(objv[3]));
            return TCL_OK;
        case TCLTCC_GET_SYMBOL:
            if (objc != 3) {
                Tcl_WrongNumArgs(interp, 2, objv, "symbol");
                return TCL_ERROR;
            }
            if (!s->relocated) {     
                if(tcc_relocate(s)!=0) {
                    Tcl_AppendResult(interp, "relocating failed", NULL);
                    return TCL_ERROR;
                } else {
                    s->relocated=1;
                }
            }
            if(tcc_get_symbol(s,&val,Tcl_GetString(objv[2]))!=0) {
                Tcl_AppendResult(interp, "symbol '", Tcl_GetString(objv[2]),"' not found", NULL);
                return TCL_ERROR;
            }
            sym_addr = Tcl_NewLongObj(val);
................................................................................
            Tcl_SetObjResult(interp, sym_addr);
            return TCL_OK; 
        case TCLTCC_OUTPUT_FILE:
            if (objc != 3) {
                Tcl_WrongNumArgs(interp, 2, objv, "filename");
                return TCL_ERROR;
            }
            if (s->relocated) {     
                Tcl_AppendResult(interp, "code already relocated, cannot output to file", NULL);
                return TCL_ERROR;
            }
            if (s->output_type == TCC_OUTPUT_MEMORY) {     
                Tcl_AppendResult(interp, "output_type memory not valid for output to file", NULL);
                return TCL_ERROR;
            }
................................................................................
        default:
            Tcl_Panic("internal error during option lookup");
    }
    return TCL_OK;
} 

static int TccCreateCmd( ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj * CONST objv[]){

    TCCState * s;

    static CONST char *types[] = {
        "memory", "exe", "dll", "obj", "preprocess",    (char *) NULL
    };
    int index;

    if (objc < 3 || objc > 4) {
        Tcl_WrongNumArgs(interp, 1, objv, "tcc_libary_path ?output_type? handle");
        return TCL_ERROR;
    }

    if (objc == 3) {
        index = TCC_OUTPUT_MEMORY;
    } else {
        if (Tcl_GetIndexFromObj(interp, objv[2], types, "type", 0,
                    &index) != TCL_OK) {
            return TCL_ERROR;
        }
    }

    s = tcc_new(objv[1]);
    tcc_set_error_func(s, interp, (void *)&TccErrorFunc);



    s->relocated = 0;

    /*printf("type: %d\n", index); */
    tcc_set_output_type(s,index);
    Tcl_CreateObjCommand(interp,Tcl_GetString(objv[objc-1]),TccHandleCmd,s,TccCCommandDeleteProc);

    return TCL_OK;
}

DLL_EXPORT int Tcc_Init(Tcl_Interp *interp)
{

    if (Tcl_InitStubs(interp, "8.4" , 0) == 0L) {
        return TCL_ERROR;
    }

    Tcl_CreateObjCommand(interp,PACKAGE_NAME,TccCreateCmd,NULL,NULL);
    Tcl_PkgProvide(interp,PACKAGE_NAME,PACKAGE_VERSION);

    return TCL_OK;
}







>







 








>
>
>
>
>

|


<

|
|
>
>
>
>
|
>
|
|
|
>
|
|
>
|
>

|
|
|
>
|
|
<
|
|
|
|
|
|
|
|
|
|

>
>







 







|




|











|







 







|




|







 







|







 







>
|
>
|
|
|
<
>
|
|
|
|
>
|
|
|
|
<
|
|
|
>
|
|
>
>
>
|
>
|
|
|

|


|
<
>
|
|
|
>
|
|
>
|

<
<
<
1
2
3
4
5
6
7
8
9
10
11
12
..
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
...
226
227
228
229
230
231
232
233
234
235
236
237
238

239
240
241
242
243
244
245
246
247
248

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

268
269
270
271
272
273
274
275
276
277



/*
 *  TclTCC - Tcl binding to Tiny C Compiler
 * 
 *  Copyright (c) 2007 Mark Janssen
 *  Copyright (c) 2014 Roy Keene
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
................................................................................
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <tcl.h>
#include "tcc.h"

struct TclTCCState {
	TCCState *s;
	int relocated;
};

static void TccErrorFunc(Tcl_Interp * interp, char * msg) {
	Tcl_AppendResult(interp, msg, "\n", NULL);
}


static void TccCCommandDeleteProc (ClientData cdata) {
	struct TclTCCState *ts;
	TCCState *s ;

	ts = (struct TclTCCState *) cdata;
	s = ts->s;

	Tcl_DecrRefCount(s->tcc_lib_path);

	/* We can delete the compiler if the output was not to memory */
	if (s->output_type != TCC_OUTPUT_MEMORY) {
		tcc_delete(s);
		ts->s = NULL;
	}

	free(ts);
}

static int TccHandleCmd ( ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj * CONST objv[]){
	unsigned long val;
	int index;
	int res;
	struct TclTCCState *ts;
	TCCState *s;
	Tcl_Obj *sym_addr;

	static CONST char *options[] = {
		"add_include_path", "add_file",  "add_library", 
		"add_library_path", "add_symbol", "command", "compile",
		"define", "get_symbol", "output_file", "undefine",    (char *) NULL
	};
	enum options {
		TCLTCC_ADD_INCLUDE, TCLTCC_ADD_FILE, TCLTCC_ADD_LIBRARY, 
		TCLTCC_ADD_LIBRARY_PATH, TCLTCC_ADD_SYMBOL, TCLTCC_COMMAND, TCLTCC_COMPILE,
		TCLTCC_DEFINE, TCLTCC_GET_SYMBOL, TCLTCC_OUTPUT_FILE, TCLTCC_UNDEFINE
	};

	ts = (struct TclTCCState *) cdata;
	s = ts->s;

    if (objc < 2) {
        Tcl_WrongNumArgs(interp, 1, objv, "subcommand arg ?arg ...?");
        return TCL_ERROR;
    }

    if (Tcl_GetIndexFromObj(interp, objv[1], options, "option", 0,
................................................................................
            tcc_add_symbol(s,Tcl_GetString(objv[2]),val); 
            return TCL_OK; 
        case TCLTCC_COMMAND:
            if (objc != 4) {
                Tcl_WrongNumArgs(interp, 2, objv, "tclname cname");
                return TCL_ERROR;
            }
            if (!ts->relocated) {     
                if(tcc_relocate(s)!=0) {
                    Tcl_AppendResult(interp, "relocating failed", NULL);
                    return TCL_ERROR;
                } else {
                    ts->relocated=1;
                }
            }
            if (tcc_get_symbol(s,&val,Tcl_GetString(objv[3]))!=0) {
		    Tcl_AppendResult(interp, "symbol '", Tcl_GetString(objv[3]),"' not found", NULL);
		    return TCL_ERROR;
	    }

            /*printf("symbol: %x\n",val); */
            Tcl_CreateObjCommand(interp,Tcl_GetString(objv[2]),(void *)val,NULL,NULL);
            return TCL_OK;
        case TCLTCC_COMPILE:
            if(ts->relocated == 1) {
                Tcl_AppendResult(interp, "code already relocated, cannot compile more",NULL);
                return TCL_ERROR;
            }
            if (objc != 3) {
                Tcl_WrongNumArgs(interp, 2, objv, "ccode");
                return TCL_ERROR;
            } else {
................................................................................
            tcc_define_symbol(s,Tcl_GetString(objv[2]),Tcl_GetString(objv[3]));
            return TCL_OK;
        case TCLTCC_GET_SYMBOL:
            if (objc != 3) {
                Tcl_WrongNumArgs(interp, 2, objv, "symbol");
                return TCL_ERROR;
            }
            if (!ts->relocated) {     
                if(tcc_relocate(s)!=0) {
                    Tcl_AppendResult(interp, "relocating failed", NULL);
                    return TCL_ERROR;
                } else {
                    ts->relocated=1;
                }
            }
            if(tcc_get_symbol(s,&val,Tcl_GetString(objv[2]))!=0) {
                Tcl_AppendResult(interp, "symbol '", Tcl_GetString(objv[2]),"' not found", NULL);
                return TCL_ERROR;
            }
            sym_addr = Tcl_NewLongObj(val);
................................................................................
            Tcl_SetObjResult(interp, sym_addr);
            return TCL_OK; 
        case TCLTCC_OUTPUT_FILE:
            if (objc != 3) {
                Tcl_WrongNumArgs(interp, 2, objv, "filename");
                return TCL_ERROR;
            }
            if (ts->relocated) {     
                Tcl_AppendResult(interp, "code already relocated, cannot output to file", NULL);
                return TCL_ERROR;
            }
            if (s->output_type == TCC_OUTPUT_MEMORY) {     
                Tcl_AppendResult(interp, "output_type memory not valid for output to file", NULL);
                return TCL_ERROR;
            }
................................................................................
        default:
            Tcl_Panic("internal error during option lookup");
    }
    return TCL_OK;
} 

static int TccCreateCmd( ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj * CONST objv[]){
	struct TclTCCState *ts
	TCCState *s;
    	int index;
	static CONST char *types[] = {
		"memory", "exe", "dll", "obj", "preprocess",    (char *) NULL
	};


	if (objc < 3 || objc > 4) {
		Tcl_WrongNumArgs(interp, 1, objv, "tcc_libary_path ?output_type? handle");
		return TCL_ERROR;
	}

	if (objc == 3) {
		index = TCC_OUTPUT_MEMORY;
	} else {
		if (Tcl_GetIndexFromObj(interp, objv[2], types, "type", 0, &index) != TCL_OK) {

			return TCL_ERROR;
		}
	}

	s = tcc_new(objv[1]);
	tcc_set_error_func(s, interp, (void *)&TccErrorFunc);

	ts = malloc(sizeof(*ts));
	ts->s = s;
    	ts->relocated = 0;

	/*printf("type: %d\n", index); */
	tcc_set_output_type(s,index);
	Tcl_CreateObjCommand(interp,Tcl_GetString(objv[objc-1]),TccHandleCmd,s,TccCCommandDeleteProc);

	return TCL_OK;
}

DLL_EXPORT int Tcc_Init(Tcl_Interp *interp) {

#ifdef TCL_USE_STUBS
	if (Tcl_InitStubs(interp, "8.4" , 0) == 0L) {
		return TCL_ERROR;
	}
#endif
	Tcl_CreateObjCommand(interp,PACKAGE_NAME,TccCreateCmd,NULL,NULL);
	Tcl_PkgProvide(interp,PACKAGE_NAME,PACKAGE_VERSION);

	return TCL_OK;
}