File: fauhdli.c

package info (click to toggle)
fauhdlc 20180504-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,956 kB
  • sloc: cpp: 23,188; ansic: 6,077; yacc: 3,764; lex: 763; makefile: 605; sh: 494; xml: 403
file content (572 lines) | stat: -rw-r--r-- 12,602 bytes parent folder | download | duplicates (2)
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* $Id$
 * Interpreter library.
 *
 * Copyright (C) 2008-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#include "fauhdli_private.h"
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include "lookup_symbols.h"
#include "kernel.h"
#include "glue-log.h"
#include "glue-main.h"
#include "signals.h"
#include "trace.h"

#define ARRAY_SIZE(t)	(sizeof t / sizeof(t[0]))

/* definition of scanner */
extern FILE *yyin;
/* definition of parser */
extern int yyparse(struct fauhdli *instance, const char *filename);


static void
fauhdli_destroy_operand(
	struct operand *op, 
	const struct glue_vhdl_cb *callbacks
)
{
	if (op == NULL) {
		return;
	}

	switch (op->kind) {
	case OPERAND_TARGET:
		callbacks->free(op->bytype.target.name);
		break;

	case OPERAND_REFERENCE:
		callbacks->free(op->bytype.data.name);
		break;

	case OPERAND_IMMEDIATE:
	case OPERAND_INDIRECT:
	case OPERAND_REGISTER:
		/* nothing to do */
		break;
	}

	callbacks->free(op);
}

static void
fauhdli_destroy_annotations(
	struct slist *annotations, 
	const struct glue_vhdl_cb *callbacks
)
{
	struct slist_entry *i;

	if (annotations == NULL) {
		return;
	}

	for (i = annotations->first; i != NULL; i = i->next) {
		struct annotation_spec *a = (struct annotation_spec*)i->data;

		if (a->string_value != NULL) {
			callbacks->free(a->string_value);
		}
		callbacks->free(a->name);
		callbacks->free(a);
	}

	slist_destroy(annotations, callbacks->free);
}

static void
fauhdli_destroy_type_element(
	struct type_element *elem,
	const struct glue_vhdl_cb *callbacks
)
{
	if (elem == NULL) {
		return;
	}
	
	callbacks->free(elem->name);

	if (elem->initial_list != NULL) {
		struct slist_entry *i;

		for (i = elem->initial_list->first; i != NULL; i = i->next) {
			fauhdli_destroy_operand(
				(struct operand *)i->data,
				callbacks);
		}
		slist_destroy(elem->initial_list, callbacks->free);
	}
	fauhdli_destroy_annotations(elem->annotations, callbacks);
	callbacks->free(elem);
}


/** destroy one data definition.
 *  @param d data definition to destroy.
 */
static void
fauhdli_destroy_data_definition(
	struct data_definition *d,
	const struct glue_vhdl_cb *callbacks
)
{
	if (d == NULL) {
		return;
	}

	callbacks->free(d->name);
	fauhdli_destroy_type_element(d->type, callbacks);
	fauhdli_destroy_annotations(d->annotations, callbacks);

	callbacks->free(d);
}

/** destroy one type declaration.
 *  @param t type declaration to destroy.
 */
static void
fauhdli_destroy_type_declaration(
	struct type_declaration *t,
	const struct glue_vhdl_cb *callbacks
)
{
	struct slist_entry *i;

	if (t == NULL) {
		return;
	}

	assert(t->elements != NULL);

	for (i = t->elements->first; i != NULL; i = i->next) {
		fauhdli_destroy_type_element(
			(struct type_element*)i->data,
			callbacks);
	}
	slist_destroy(t->elements, callbacks->free);

	callbacks->free(t);
}


/** destroy a type list.
 *  @param seg type list to destroy.
 *  FIXME: cleanup wrt. fauhdli_destroy_data_seg.
 */
static void
fauhdli_destroy_type_seg(
	struct slist *seg,
	const struct glue_vhdl_cb *callbacks
)
{
	struct slist_entry *i;

	if (seg == NULL) {
		return;
	}

	for (i = seg->first; i != NULL; i = i->next) {
		fauhdli_destroy_type_declaration(
			(struct type_declaration *)i->data,
			callbacks);
	}

	slist_destroy(seg, callbacks->free);
}

/** destroy a transfer or stack segment.
 *  @param seg transfer or stack segment to destroy.
 */
static void
fauhdli_destroy_data_seg(
	struct slist *seg, 
	const struct glue_vhdl_cb *callbacks
)
{
	struct slist_entry *i;

	if (seg == NULL) {
		return;
	}
	
	for (i = seg->first; i != NULL; i = i->next) {
		fauhdli_destroy_data_definition(
			(struct data_definition*)i->data,
			callbacks);
	}

	slist_destroy(seg, callbacks->free);
}

static void
fauhdli_destroy_opcode(
	struct opcode *opcode, 
	const struct glue_vhdl_cb *callbacks
)
{
	if (opcode == NULL) {
		return;
	}

	fauhdli_destroy_operand(opcode->op1, callbacks);
	fauhdli_destroy_operand(opcode->op2, callbacks);
	fauhdli_destroy_operand(opcode->op3, callbacks);
	fauhdli_destroy_type_element(opcode->indexed_type, callbacks);
	fauhdli_destroy_annotations(opcode->annotations, callbacks);

	switch (opcode->kind) {
	case OPCODE_LABEL:
		callbacks->free(opcode->label);
		break;

	default:
		break;
	}

	callbacks->free(opcode);
}

/** destroy a text segment.
 *  @param seg text segment to destroy.
 */
static void
fauhdli_destroy_text_seg(
	struct slist *seg, 
	const struct glue_vhdl_cb *callbacks
)
{
	struct slist_entry *i;

	if (seg == NULL) {
		return;
	}

	for (i = seg->first; i != NULL; i = i->next) {
		fauhdli_destroy_opcode((struct opcode *)i->data, callbacks);
	}
	slist_destroy(seg, callbacks->free);
}

/** destroy the code_container hierarchy again recursively.
 *  @param instance fauhdli instance.
 */
static void
fauhdli_destroy_container(
	struct code_container *container, 
	const struct glue_vhdl_cb *callbacks
)
{
	struct slist_entry *i;
	assert(container != NULL);
	callbacks->free(container->name);

	fauhdli_destroy_type_seg(container->type_definitions, callbacks);
	fauhdli_destroy_data_seg(container->transfer_segment, callbacks);
	fauhdli_destroy_data_seg(container->stack_segment, callbacks);
	fauhdli_destroy_text_seg(container->text_segment, callbacks);

	if (container->sub_containers != NULL) {
		for (i = container->sub_containers->first; i != NULL; 
			i = i->next) {

			fauhdli_destroy_container(
				(struct code_container *)i->data,
				callbacks);
		}
		slist_destroy(container->sub_containers, callbacks->free);
	}

	callbacks->free(container);
}

static void
fauhdli_destroy_signals(
	struct slset *sigs, 
	const struct glue_vhdl_cb *callbacks
)
{
	struct slset_entry *i;
	for (i = sigs->first; i != NULL; i = i->next) {
		signal_destroy((struct signal *)i->data, callbacks);
	}
	slset_destroy(sigs, callbacks->free);
}

static bool
fauhdli_parse(struct fauhdli *instance, const char *file_name)
{
	int ret;
	assert(file_name != NULL);

	yyin = fopen(file_name, "r");
	if (yyin == NULL) {
		instance->callbacks.log(
			FAUHDLI_LOG_ERROR, "fauhdli", __func__,
			"Failed to open file \"%s\": %s\n",
			file_name, strerror(errno));
		return false;
	}

	ret = yyparse(instance, file_name);
	if (ret != 0) {
		instance->callbacks.log(
			FAUHDLI_LOG_ERROR, "fauhdli", __func__,
			"Failed to parse file \"%s\"\n", file_name);
		ret = fclose(yyin);
		assert(ret >= 0);
		return false;
	}

	ret = fclose(yyin);
	assert(ret >= 0);
	return true;
}

static void
fauhdli_setup_callbacks(
	struct fauhdli *instance, 
	const struct glue_vhdl_cb *callbacks
)
{
	if (callbacks == NULL) {
		memset(&instance->callbacks, 0, sizeof(instance->callbacks));
	} else {
		memcpy(&instance->callbacks, 
			callbacks, 
			sizeof(instance->callbacks));
	}

	if (   instance->callbacks.malloc == NULL 
	    || instance->callbacks.free == NULL) {
		instance->callbacks.malloc = malloc;
		instance->callbacks.free = free;
	}

	if (instance->callbacks.log == NULL) {
		instance->callbacks.log = 
			(void (*)(int, 
				const char *, 
				const char *, 
				const char *,
				...))fauhdli_log;
	}

	if (instance->callbacks.time_virt == NULL 
		|| instance->callbacks.time_call_at == NULL
		|| instance->callbacks.time_call_delete == NULL
		|| instance->callbacks.quit == NULL) {

		/* any scheduling callback set, but not all? -> error */
		if (instance->callbacks.time_virt != NULL
			|| instance->callbacks.time_call_at != NULL
			|| instance->callbacks.time_call_delete != NULL
			|| instance->callbacks.quit != NULL) {

			instance->callbacks.log(FAUHDLI_LOG_ERROR,
				"fauhdli",
				"setup",
				"Some, but not all callbacks for "
				"scheduling set. Overriding.\n");
		}

		instance->callbacks.time_virt = fauhdli_time_virt;
		instance->callbacks.time_call_at = fauhdli_time_call_at;
		instance->callbacks.time_call_delete = 
					fauhdli_time_call_delete;
		instance->callbacks.quit = fauhdli_simulation_quit;
	}
}

struct opcode *
fauhdli_alloc_opcode(
	enum opcode_kind kind, 
	const struct glue_vhdl_cb *callbacks
)
{
	/* FIXME calllbacks */
	struct opcode *ret = callbacks->malloc(sizeof(struct opcode));
	assert(ret != NULL);

	ret->kind = kind;
	ret->label = NULL;
	ret->op1 = NULL;
	ret->op2 = NULL;
	ret->op3 = NULL;
	ret->indexed_type = NULL;
	ret->annotations = NULL;

	return ret;
}

struct fauhdli *
fauhdli_create(
	const char *parse_file, 
	const char *trace_file, 
	bool debug,
	const struct glue_vhdl_cb *callbacks,
	void *_cpssp
)
{
	struct fauhdli *ret;
	bool r;

	if (callbacks->malloc != NULL) {
		ret = callbacks->malloc(sizeof(struct fauhdli));
	} else {
		ret = malloc(sizeof(struct fauhdli));
	}
	assert(ret != NULL);

	fauhdli_setup_callbacks(ret, callbacks);

	ret->container = NULL;
	ret->cleanup_ptrs = slset_create(NULL, ret->callbacks.malloc);
	ret->scheduler = vhdl_sched_create(&ret->callbacks);
	ret->signals = slset_create(NULL, ret->callbacks.malloc);
	ret->pending_stack_frames = slset_create(NULL, ret->callbacks.malloc);
	ret->processes = slset_create(NULL, ret->callbacks.malloc);
	ret->sim_time = 0;
	ret->debug = debug;
	ret->drivers = slset_create(NULL, ret->callbacks.malloc);
	ret->foreign_drivers = slset_create(NULL, ret->callbacks.malloc);

	ret->foreign_g_array_params.base.pointer = NULL;
	ret->foreign_g_array_params.left = 0;
	ret->foreign_g_array_params.left_set = false;
	ret->foreign_g_array_params.right = 0;
	ret->foreign_g_array_params.right_set = false;
	ret->foreign_g_array_params.direction = 0;
	ret->foreign_g_array_params.direction_set = false;
	ret->foreign_g_array_params.element_type = NULL;
	ret->foreign_g_array_params.formal_name = NULL;

	ret->foreign_mode = FOREIGN_MODE_COMPONENT;
	ret->wakeup_scheduled = false;

	ret->trace_list = NULL;

	assert(_cpssp != NULL);
	/* FIXME type should be opaque to us. */
	ret->glue_vhdl = _cpssp;


	if (trace_file != NULL) {
		ret->tracer = trace_create(trace_file, callbacks);
	} else {
		ret->tracer = NULL;
	}

	r = fauhdli_parse(ret, parse_file);
	if (! r) {
		assert(0);
		return ret;
	}

	return ret;
}

void
fauhdli_destroy(struct fauhdli *instance)
{
	assert(instance != NULL);
	assert(instance->cleanup_ptrs != NULL);

	if (instance->container != NULL) {
		fauhdli_destroy_container(instance->container,
					&instance->callbacks);
	}

	fauhdli_kernel_destroy(instance);
	slset_destroy(instance->pending_stack_frames, 
			instance->callbacks.free);
	slset_destroy(instance->drivers, instance->callbacks.free);
	slset_destroy(instance->foreign_drivers, instance->callbacks.free);
	slset_destroy_data(instance->cleanup_ptrs, instance->callbacks.free);
	fauhdli_destroy_signals(instance->signals, &instance->callbacks);
	vhdl_sched_destroy(instance->scheduler, &instance->callbacks);
	slset_destroy(instance->processes, instance->callbacks.free);
	if (instance->tracer != NULL) {
		trace_destroy(instance->tracer, &instance->callbacks);
	}
	instance->callbacks.free(instance);
}

void
fauhdli_init(
	struct fauhdli *instance, 
	const char *top_entity
)
{
	if (top_entity == NULL) {
		instance->callbacks.log(
			FAUHDLI_LOG_WARNING, "fauhdli", __func__,
			"Top entity not specified!\n");
	}

	fauhdli_resolve_symbols(instance);
	fauhdli_kernel_init(instance, top_entity);
}

const struct annotation_spec *
fauhdli_find_annotation(const char *name, const struct slist *annotations)
{
	struct slist_entry *i;

	if (annotations == NULL) {
		return NULL;
	}

	for (i = annotations->first; i != NULL; i = i->next) {
		const struct annotation_spec *spec = 
			(const struct annotation_spec *)i->data;
		if (strcmp(spec->name, name) == 0) {
			return spec;
		}
	}

	return NULL;
}

void
fauhdli_set_driver(
	struct fauhdli *instance,
	void *_drv,
	union fauhdli_value value
)
{
	struct driver *drv = (struct driver *)_drv;
	int64_t t;

	/* set driver value */
	assert(instance->callbacks.time_virt != NULL);
	t = (int64_t)instance->callbacks.time_virt();
	assert(0 <= t);
	driver_update(drv, value, t, &instance->callbacks);

	/* kernel already running? */
	if (instance->wakeup_scheduled) {
		return;
	}

	/* schedule a delta cycle */
	assert(instance->callbacks.time_call_delete != NULL);
	assert(instance->callbacks.time_call_at != NULL);

	instance->callbacks.time_call_delete(
			fauhdli_kernel_simulation_cycle, 
			instance);
	instance->callbacks.time_call_at(
			t,
			fauhdli_kernel_simulation_cycle,
			instance);

	instance->wakeup_scheduled = true;
}