summaryrefslogtreecommitdiffstats
path: root/pcilib/py.c
blob: a288043dd237a2d36370f3bd2d571534e5a9a2bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#ifdef BUILD_PYTHON_MODULES
#include <Python.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include "pci.h"
#include "debug.h"
#include "pcilib.h"
#include "py.h"
#include "error.h"

#ifdef BUILD_PYTHON_MODULES
typedef struct pcilib_script_s {
	const char* name;
    PyObject *module;	/**< PyModule object, contains script enviroment */	
	UT_hash_handle hh;
} pcilib_script_s;

struct pcilib_py_s {
    PyObject *main_module;
    PyObject *global_dict;
    PyObject *pcilib_pywrap;
    int py_initialized_inside; ///< Flag, shows that Py_Initialize has been called inside class
    struct pcilib_script_s *scripts;
};
#endif

int pcilib_init_py(pcilib_t *ctx) {
#ifdef BUILD_PYTHON_MODULES
    ctx->py = (pcilib_py_t*)malloc(sizeof(pcilib_py_t));
    if (!ctx->py) return PCILIB_ERROR_MEMORY;

    if(!Py_IsInitialized())
    {
        Py_Initialize();
        
        //Since python is being initializing from c programm, it needs
        //to initialize threads to works properly with c threads
        PyEval_InitThreads();
        PyEval_ReleaseLock();
        
        ctx->py->py_initialized_inside = 1;
	}
	else
		ctx->py->py_initialized_inside = 0;
		
    ctx->py->main_module = PyImport_AddModule("__parser__");
    if (!ctx->py->main_module)
        return PCILIB_ERROR_FAILED;

    ctx->py->global_dict = PyModule_GetDict(ctx->py->main_module);
    if (!ctx->py->global_dict)
        return PCILIB_ERROR_FAILED;



    PyObject* py_script_module = PyImport_ImportModule("pcipywrap");
	if(!py_script_module)
	{
		printf("Error in import python module: ");
		PyErr_Print();
		return PCILIB_ERROR_FAILED;
	}
	
	PyObject* mod_name = PyString_FromString("Pcipywrap");
	ctx->py->pcilib_pywrap = PyObject_CallMethodObjArgs(py_script_module,
																  mod_name,
																  PyCObject_FromVoidPtr(ctx, NULL),
																  NULL);
	Py_XDECREF(mod_name);
	
	if(!ctx->py->pcilib_pywrap)
	{
		printf("Error in import python module: ");
		PyErr_Print();
        return PCILIB_ERROR_FAILED;
	}
	
	ctx->py->scripts = NULL;
#endif
    return 0;
}

int pcilib_py_add_script_dir(pcilib_t *ctx)
{
#ifdef BUILD_PYTHON_MODULES
	//create path string, where the model scripts should be
	static int model_dir_added = 0;
	if(!model_dir_added)
	{
		char* model_dir = getenv("PCILIB_MODEL_DIR");
		char* model_path = malloc(strlen(model_dir) + strlen(ctx->model) + 2);
		if (!model_path) return PCILIB_ERROR_MEMORY;
		sprintf(model_path, "%s/%s", model_dir, ctx->model);
		//add path to python
		PyObject* path = PySys_GetObject("path");
		if(PyList_Append(path, PyString_FromString(model_path)) == -1)
		{
			pcilib_error("Cant set PCILIB_MODEL_DIR library path to python.");
			free(model_path);
			return PCILIB_ERROR_FAILED;
		}
		free(model_path);
		model_dir_added = 1;
	}
#endif
	return 0;
}

void pcilib_free_py(pcilib_t *ctx) {
#ifdef BUILD_PYTHON_MODULES
	int py_initialized_inside = 0;
	
    if (ctx->py) {		
		if(ctx->py->py_initialized_inside)
			py_initialized_inside = 1;
		
        // Dict and module references are borrowed
        free(ctx->py);
        ctx->py = NULL;
    }
    
    if(py_initialized_inside)
		Py_Finalize();
#endif
}

/*
static int pcilib_py_realloc_string(pcilib_t *ctx, size_t required, size_t *size, char **str) {
    char *ptr;
    size_t cur = *size;
    
    if ((required + 1) > cur) {
        while (cur < required) cur *= 2;
        ptr = (char*)realloc(*str, cur);
        if (!ptr) return PCILIB_ERROR_MEMORY;
        *size = cur;
        *str = ptr;
    }
    ]
    return 0;
}
*/
#ifdef BUILD_PYTHON_MODULES
static char *pcilib_py_parse_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *value) {
    int i;
    int err = 0;
    pcilib_value_t val = {0};
    pcilib_register_value_t regval;

    char save;
    char *reg, *cur;
    size_t offset = 0;

    size_t size;
    char *src;
    char *dst;

    size_t max_repl = 2 + 2 * sizeof(pcilib_value_t);                               // the text representation of largest integer

    if (value) {
        err = pcilib_copy_value(ctx, &val, value);
        if (err) return NULL;

        err = pcilib_convert_value_type(ctx, &val, PCILIB_TYPE_STRING);
        if (err) return NULL;

        if (strlen(val.sval) > max_repl)
            max_repl = strlen(val.sval);
    }

    size = ((max_repl + 1) / 3 + 1) * strlen(codestr);                          // minimum register length is 3 symbols ($a + delimiter space), it is replaces with (max_repl+1) symbols

    src = strdup(codestr);
    dst = (char*)malloc(size);                                                  // allocating maximum required space

    if ((!src)||(!dst)) {
        if (src) free(src);
        if (dst) free(dst);
        pcilib_error("Failed to allocate memory for string formulas");
        return NULL;
    }

    cur = src;
    reg = strchr(src, '$');
    while (reg) {
        strcpy(dst + offset, cur);
        offset += reg - cur;

            // find the end of the register name
        reg++;
        if (*reg == '{') {
            reg++;
            for (i = 0; (reg[i])&&(reg[i] != '}'); i++);
            if (!reg[i]) {
                pcilib_error("Python formula (%s) contains unterminated variable reference", codestr);
                err = PCILIB_ERROR_INVALID_DATA;
                break;
            }
        } else {
            for (i = 0; isalnum(reg[i])||(reg[i] == '_'); i++);
        }
        save = reg[i];
        reg[i] = 0;

            // determine replacement value
        if (!strcasecmp(reg, "value")) {
            if (!value) {
                pcilib_error("Python formula (%s) relies on the value of register, but it is not provided", codestr);
                err = PCILIB_ERROR_INVALID_REQUEST;
                break;
            }

            strcpy(dst + offset, val.sval);
        } else {
            if (*reg == '/') {
                pcilib_value_t val = {0};
                err = pcilib_get_property(ctx, reg, &val);
                if (err) break;
                err = pcilib_convert_value_type(ctx, &val, PCILIB_TYPE_STRING);
                if (err) break;
                sprintf(dst + offset, "%s", val.sval);
            } else {
                err = pcilib_read_register(ctx, NULL, reg, &regval);
                if (err) break;
                sprintf(dst + offset, "0x%lx", regval);
            }
        }

        offset += strlen(dst + offset);
        if (save == '}') i++;
        else reg[i] = save;

            // Advance to the next register if any
        cur = reg + i;
        reg = strchr(cur, '$');
    }

    strcpy(dst + offset, cur);

    free(src);

    if (err) {
        free(dst);
        return NULL;
    }

    return dst;
}
#endif

int pcilib_py_eval_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *value) {
#ifdef BUILD_PYTHON_MODULES
    PyGILState_STATE gstate;
    char *code;
    PyObject* obj;

    code = pcilib_py_parse_string(ctx, codestr, value);
    if (!code) {
        pcilib_error("Failed to parse registers in the code: %s", codestr);
        return PCILIB_ERROR_FAILED;
    }

    gstate = PyGILState_Ensure();
    obj = PyRun_String(code, Py_eval_input, ctx->py->global_dict, ctx->py->global_dict);
    PyGILState_Release(gstate);

    if (!obj) {
        pcilib_error("Failed to run the Python code: %s", code);
        return PCILIB_ERROR_FAILED;
    }

    pcilib_debug(VIEWS, "Evaluating a Python string \'%s\' to %lf=\'%s\'", codestr, PyFloat_AsDouble(obj), code);
    return pcilib_set_value_from_float(ctx, value, PyFloat_AsDouble(obj));
#else
	pcilib_error("Current build not support python.");
    return PCILIB_ERROR_NOTAVAILABLE;
#endif
}

pcilib_py_object* pcilib_get_value_as_pyobject(pcilib_t* ctx, pcilib_value_t *val, int *ret)
{
#ifdef BUILD_PYTHON_MODULES	
	int err;
	
	switch(val->type)
	{
		case PCILIB_TYPE_INVALID:
            pcilib_error("Invalid register output type (PCILIB_TYPE_INVALID)");
			if (ret) *ret = PCILIB_ERROR_NOTSUPPORTED;
			return NULL;
			
		case PCILIB_TYPE_STRING:
            pcilib_error("Invalid register output type (PCILIB_TYPE_STRING)");
            if (ret) *ret = PCILIB_ERROR_NOTSUPPORTED;
			return NULL;
		
		case PCILIB_TYPE_LONG:
		{
			long ret_val;
			ret_val = pcilib_get_value_as_int(ctx, val, &err);
			
			if(err)
			{
                if (ret) *ret = err;
				return NULL;
			}
			
			if (ret) *ret = 0;
			return (PyObject*)PyInt_FromLong((long) ret_val);
		}
		
		case PCILIB_TYPE_DOUBLE:
		{
			double ret_val;
			ret_val = pcilib_get_value_as_float(ctx, val, &err);
			
			if(err)
			{
                if (ret) *ret = err;
				return NULL;
			}
			
			if (ret) *ret = 0;
			return (PyObject*)PyFloat_FromDouble((double) ret_val);
		}
		
		default:
			if (ret) *ret = PCILIB_ERROR_NOTSUPPORTED;
            pcilib_error("Invalid register output type (unknown)");
			return NULL;
	}
#else
	pcilib_error("Current build not support python.");
    if (ret) *ret = PCILIB_ERROR_NOTAVAILABLE;
	return NULL;
#endif
}

int pcilib_set_value_from_pyobject(pcilib_t* ctx, pcilib_value_t *val, pcilib_py_object* pyObjVal)
{
#ifdef BUILD_PYTHON_MODULES
	PyObject* pyVal = pyObjVal;
	int err;
	
	PyGILState_STATE gstate = PyGILState_Ensure();
    if(PyInt_Check(pyVal))
    {
        err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyVal));
    }
    else
        if(PyFloat_Check(pyVal))
            err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(pyVal));
        else
            if(PyString_Check(pyVal))
                err = pcilib_set_value_from_static_string(ctx, val, PyString_AsString(pyVal));
                else
                {
					PyGILState_Release(gstate);
                    pcilib_error("Invalid input. Input type should be int, float or string.");
                    return PCILIB_ERROR_NOTSUPPORTED;
                }
    PyGILState_Release(gstate);
    if(err)
        return err;
        
    return 0;
#else
	pcilib_error("Current build not support python.");
    return PCILIB_ERROR_NOTAVAILABLE;
#endif
}

int pcilib_py_init_script(pcilib_t *ctx, const char* module_name)
{
#ifdef BUILD_PYTHON_MODULES
	//extract module name from script name
	char* py_module_name = strdup(module_name);
	py_module_name = strtok(py_module_name, ".");
	if(!py_module_name)
	{
		pcilib_error("Invalid script name specified in XML property (%s)."
					 " Seems like name doesnt contains extension", module_name);
		return PCILIB_ERROR_INVALID_DATA;
	}
	
	pcilib_script_s* module = NULL;
	HASH_FIND_STR( ctx->py->scripts, module_name, module);
	if(module)
	{
		pcilib_warning("Python module %s is already in hash. Skip init step", module_name);
		return 0;
	}
	
	//import python script
	PyObject* py_script_module = PyImport_ImportModule(py_module_name);
	if(!py_script_module)
	{
		printf("Error in import python module: ");
		PyErr_Print();
		free(py_module_name);
		return PCILIB_ERROR_INVALID_DATA;
	}
	free(py_module_name);
	
	//Success. Create struct and initialize values
	module = malloc(sizeof(pcilib_script_s));
	if (!module)
		return PCILIB_ERROR_MEMORY;
	module->module = py_script_module;
	module->name = module_name;
	HASH_ADD_STR( ctx->py->scripts, name, module);
#endif
	return 0;
}

int pcilib_py_get_transform_script_properties(pcilib_t *ctx, const char* module_name,
pcilib_access_mode_t *mode)
{
#ifdef BUILD_PYTHON_MODULES
	pcilib_script_s *module;
	
	HASH_FIND_STR(ctx->py->scripts, module_name, module);
	if(!module)
	{
		pcilib_error("Failed to find script module (%s) in hash", module_name);
		return PCILIB_ERROR_NOTFOUND;
	}
	
	PyObject* dict = PyModule_GetDict(module->module);
	//Setting correct mode
	mode[0] = 0;
	if(PyDict_Contains(dict, PyString_FromString("read_from_register")))
		mode[0] |= PCILIB_ACCESS_R;	
	if(PyDict_Contains(dict, PyString_FromString("write_to_register")))
		mode[0] |= PCILIB_ACCESS_W;	
	return 0;
#else
	mode[0] = PCILIB_ACCESS_RW;
	return 0;
#endif
}

int pcilib_py_free_script(pcilib_t *ctx, const char* module_name)
{
#ifdef BUILD_PYTHON_MODULES
	pcilib_script_s *module;
	HASH_FIND_STR(ctx->py->scripts, module_name, module);
	
	if(!module)
	{
		pcilib_warning("Cant find Python module %s in hash. Seems it has already deleted.", module_name);
		return 0;
	}
		
	if(module->module)
	{
		module->module = NULL;
	}
	
	HASH_DEL(ctx->py->scripts, module);
	free(module);
#endif
	return 0;
}

int pcilib_script_run_func(pcilib_t *ctx, const char* module_name,
                           const char* func_name,  pcilib_value_t *val)
{
#ifdef BUILD_PYTHON_MODULES
	int err;
	pcilib_script_s *module;
	HASH_FIND_STR(ctx->py->scripts, module_name, module);
	if(!module)
	{
		pcilib_error("Failed to find script module (%s) in hash", module_name);
		return PCILIB_ERROR_NOTFOUND;
	}
	
	PyGILState_STATE gstate = PyGILState_Ensure();
    PyObject *input = pcilib_get_value_as_pyobject(ctx, val, &err);
    if(err)
       return err;
       
	PyObject *py_func_name = PyUnicode_FromString(func_name);
   	PyObject *ret = PyObject_CallMethodObjArgs(module->module,
											   py_func_name,
											   ctx->py->pcilib_pywrap,
											   input,
											   NULL);

    Py_XDECREF(py_func_name);
	Py_XDECREF(input);
	PyGILState_Release(gstate);
	
	if (!ret) 
	{
	   printf("Python script error: ");
	   PyErr_Print();
	   return PCILIB_ERROR_FAILED;
	}
	
	if(ret != Py_None)
	{
        err = pcilib_set_value_from_pyobject(ctx, val, ret);
		Py_XDECREF(ret);
	
		if(err)
		{
			pcilib_error("Failed to convert python script return value to internal type: %i", err);
			return err;
		}
	}
    return 0;
#else
	pcilib_error("Current build not support python.");
    return PCILIB_ERROR_NOTAVAILABLE;
#endif
}