summaryrefslogtreecommitdiff
path: root/src/freedreno/decode/script.c
blob: a882dd25af60d3c2795d941a6a953e88dbce7de7 (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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */

/*
 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *    Rob Clark <robclark@freedesktop.org>
 */

#define _GNU_SOURCE
#define LUA_COMPAT_APIINTCASTS

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <assert.h>

#include "script.h"
#include "cffdec.h"
#include "rnnutil.h"

static lua_State *L;

#if 0
#define DBG(fmt, ...) \
		do { printf(" ** %s:%d ** "fmt "\n", \
				__FUNCTION__, __LINE__, ##__VA_ARGS__); } while (0)
#else
#define DBG(fmt, ...) do {} while (0)
#endif

/* An rnn based decoder, which can either be decoding current register
 * values, or domain based decoding of a pm4 packet.
 *
 */
struct rnndec {
	struct rnn base;

	/* for pm4 packet decoding: */
	uint32_t sizedwords;
	uint32_t *dwords;
};

static inline struct rnndec *to_rnndec(struct rnn *rnn)
{
	return (struct rnndec *)rnn;
}

static uint32_t rnn_val(struct rnn *rnn, uint32_t regbase)
{
	struct rnndec *rnndec = to_rnndec(rnn);

	if (!rnndec->sizedwords) {
		return reg_val(regbase);
	} else if (regbase < rnndec->sizedwords) {
		return rnndec->dwords[regbase];
	} else {
		// XXX throw an error
		return -1;
	}
}

/* does not return */
static void error(const char *fmt)
{
	fprintf(stderr, fmt, lua_tostring(L, -1));
	exit(1);
}

/*
 * An enum type that can be used as string or number:
 */

struct rnndenum {
	const char *str;
	int val;
};

static int l_meta_rnn_enum_tostring(lua_State *L)
{
	struct rnndenum *e = lua_touserdata(L, 1);
	if (e->str) {
		lua_pushstring(L, e->str);
	} else {
		char buf[32];
		sprintf(buf, "%u", e->val);
		lua_pushstring(L, buf);
	}
	return 1;
}

/* so, this doesn't actually seem to be implemented yet, but hopefully
 * some day lua comes to it's senses
 */
static int l_meta_rnn_enum_tonumber(lua_State *L)
{
	struct rnndenum *e = lua_touserdata(L, 1);
	lua_pushinteger(L, e->val);
	return 1;
}

static const struct luaL_Reg l_meta_rnn_enum[] = {
	{"__tostring", l_meta_rnn_enum_tostring},
	{"__tonumber", l_meta_rnn_enum_tonumber},
	{NULL, NULL}  /* sentinel */
};

static void pushenum(struct lua_State *L, int val, struct rnnenum *info)
{
	struct rnndenum *e = lua_newuserdata(L, sizeof(*e));

	e->val = val;
	e->str = NULL;

	for (int i = 0; i < info->valsnum; i++) {
		if (info->vals[i]->valvalid && (info->vals[i]->value == val)) {
			e->str = info->vals[i]->name;
			break;
		}
	}

	luaL_newmetatable(L, "rnnmetaenum");
	luaL_setfuncs(L, l_meta_rnn_enum, 0);
	lua_pop(L, 1);

	luaL_setmetatable(L, "rnnmetaenum");
}

/* Expose rnn decode to script environment as "rnn" library:
 */

struct rnndoff {
	struct rnn *rnn;
	struct rnndelem *elem;
	uint64_t offset;
};

static void push_rnndoff(lua_State *L, struct rnn *rnn,
		struct rnndelem *elem, uint64_t offset)
{
	struct rnndoff *rnndoff = lua_newuserdata(L, sizeof(*rnndoff));
	rnndoff->rnn = rnn;
	rnndoff->elem = elem;
	rnndoff->offset = offset;
}

static int l_rnn_etype_array(lua_State *L, struct rnn *rnn,
		struct rnndelem *elem, uint64_t offset);
static int l_rnn_etype_reg(lua_State *L, struct rnn *rnn,
		struct rnndelem *elem, uint64_t offset);

static int pushdecval(struct lua_State *L, struct rnn *rnn,
		uint32_t regval, struct rnntypeinfo *info)
{
	union rnndecval val;
	switch (rnn_decodelem(rnn, info, regval, &val)) {
	case RNN_TTYPE_ENUM:
	case RNN_TTYPE_INLINE_ENUM:
		pushenum(L, val.i, info->eenum);
		return 1;
	case RNN_TTYPE_INT:
		lua_pushinteger(L, val.i);
		return 1;
	case RNN_TTYPE_UINT:
	case RNN_TTYPE_HEX:
		lua_pushunsigned(L, val.u);
		return 1;
	case RNN_TTYPE_FLOAT:
		lua_pushnumber(L, val.f);
		return 1;
	case RNN_TTYPE_BOOLEAN:
		lua_pushboolean(L, val.u);
		return 1;
	case RNN_TTYPE_INVALID:
	default:
		return 0;
	}

}

static int l_rnn_etype(lua_State *L, struct rnn *rnn,
		struct rnndelem *elem, uint64_t offset)
{
	int ret;
	uint32_t regval;
	DBG("elem=%p (%d), offset=%lu", elem, elem->type, offset);
	switch (elem->type) {
	case RNN_ETYPE_REG:
		/* if a register has no bitfields, just return
		 * the raw value:
		 */
		regval = rnn_val(rnn, offset);
		regval <<= elem->typeinfo.shr;
		ret = pushdecval(L, rnn, regval, &elem->typeinfo);
		if (ret)
			return ret;
		return l_rnn_etype_reg(L, rnn, elem, offset);
	case RNN_ETYPE_ARRAY:
		return l_rnn_etype_array(L, rnn, elem, offset);
	default:
		/* hmm.. */
		printf("unhandled type: %d\n", elem->type);
		return 0;
	}
}

/*
 * Struct Object:
 * To implement stuff like 'RB_MRT[n].CONTROL' we need a struct-object
 * to represent the current array index (ie. 'RB_MRT[n]')
 */

static int l_rnn_struct_meta_index(lua_State *L)
{
	struct rnndoff *rnndoff = lua_touserdata(L, 1);
	const char *name = lua_tostring(L, 2);
	struct rnndelem *elem = rnndoff->elem;
	int i;

	for (i = 0; i < elem->subelemsnum; i++) {
		struct rnndelem *subelem = elem->subelems[i];
		if (!strcmp(name, subelem->name)) {
			return l_rnn_etype(L, rnndoff->rnn, subelem,
					rnndoff->offset + subelem->offset);
		}
	}

	return 0;
}

static const struct luaL_Reg l_meta_rnn_struct[] = {
	{"__index", l_rnn_struct_meta_index},
	{NULL, NULL}  /* sentinel */
};

static int l_rnn_etype_struct(lua_State *L, struct rnn *rnn,
		struct rnndelem *elem, uint64_t offset)
{
	push_rnndoff(L, rnn, elem, offset);

	luaL_newmetatable(L, "rnnmetastruct");
	luaL_setfuncs(L, l_meta_rnn_struct, 0);
	lua_pop(L, 1);

	luaL_setmetatable(L, "rnnmetastruct");

	return 1;
}

/*
 * Array Object:
 */

static int l_rnn_array_meta_index(lua_State *L)
{
	struct rnndoff *rnndoff = lua_touserdata(L, 1);
	int idx = lua_tointeger(L, 2);
	struct rnndelem *elem = rnndoff->elem;
	uint64_t offset = rnndoff->offset + (elem->stride * idx);

	DBG("rnndoff=%p, idx=%d, numsubelems=%d",
			rnndoff, idx, rnndoff->elem->subelemsnum);

	/* if just a single sub-element, it is directly a register,
	 * otherwise we need to accumulate the array index while
	 * we wait for the register name within the array..
	 */
	if (elem->subelemsnum == 1) {
		return l_rnn_etype(L, rnndoff->rnn, elem->subelems[0], offset);
	} else {
		return l_rnn_etype_struct(L, rnndoff->rnn, elem, offset);
	}

	return 0;
}

static const struct luaL_Reg l_meta_rnn_array[] = {
	{"__index", l_rnn_array_meta_index},
	{NULL, NULL}  /* sentinel */
};

static int l_rnn_etype_array(lua_State *L, struct rnn *rnn,
		struct rnndelem *elem, uint64_t offset)
{
	push_rnndoff(L, rnn, elem, offset);

	luaL_newmetatable(L, "rnnmetaarray");
	luaL_setfuncs(L, l_meta_rnn_array, 0);
	lua_pop(L, 1);

	luaL_setmetatable(L, "rnnmetaarray");

	return 1;
}

/*
 * Register element:
 */

static int l_rnn_reg_meta_index(lua_State *L)
{
	struct rnndoff *rnndoff = lua_touserdata(L, 1);
	const char *name = lua_tostring(L, 2);
	struct rnndelem *elem = rnndoff->elem;
	struct rnntypeinfo *info = &elem->typeinfo;
	struct rnnbitfield **bitfields;
	int bitfieldsnum;
	int i;

	switch (info->type) {
	case RNN_TTYPE_BITSET:
		bitfields = info->ebitset->bitfields;
		bitfieldsnum = info->ebitset->bitfieldsnum;
		break;
	case RNN_TTYPE_INLINE_BITSET:
		bitfields = info->bitfields;
		bitfieldsnum = info->bitfieldsnum;
		break;
	default:
		printf("invalid register type: %d\n", info->type);
		return 0;
	}

	for (i = 0; i < bitfieldsnum; i++) {
		struct rnnbitfield *bf = bitfields[i];
		if (!strcmp(name, bf->name)) {
			uint32_t regval = rnn_val(rnndoff->rnn, rnndoff->offset);

			regval &= typeinfo_mask(&bf->typeinfo);
			regval >>= bf->typeinfo.low;
			regval <<= bf->typeinfo.shr;

			DBG("name=%s, info=%p, subelemsnum=%d, type=%d, regval=%x",
					name, info, rnndoff->elem->subelemsnum,
					bf->typeinfo.type, regval);

			return pushdecval(L, rnndoff->rnn, regval, &bf->typeinfo);
		}
	}

	printf("invalid member: %s\n", name);
	return 0;
}

static int l_rnn_reg_meta_tostring(lua_State *L)
{
	struct rnndoff *rnndoff = lua_touserdata(L, 1);
	uint32_t regval = rnn_val(rnndoff->rnn, rnndoff->offset);
	struct rnndecaddrinfo *info = rnn_reginfo(rnndoff->rnn, rnndoff->offset);
	char *decoded;
	if (info && info->typeinfo) {
		decoded = rnndec_decodeval(rnndoff->rnn->vc,
				info->typeinfo, regval);
	} else {
		asprintf(&decoded, "%08x", regval);
	}
	lua_pushstring(L, decoded);
	free(decoded);
	if (info) {
		free(info->name);
		free(info);
	}
	return 1;
}

static int l_rnn_reg_meta_tonumber(lua_State *L)
{
	struct rnndoff *rnndoff = lua_touserdata(L, 1);
	uint32_t regval = rnn_val(rnndoff->rnn, rnndoff->offset);

	regval <<= rnndoff->elem->typeinfo.shr;

	lua_pushnumber(L, regval);
	return 1;
}

static const struct luaL_Reg l_meta_rnn_reg[] = {
	{"__index", l_rnn_reg_meta_index},
	{"__tostring", l_rnn_reg_meta_tostring},
	{"__tonumber", l_rnn_reg_meta_tonumber},
	{NULL, NULL}  /* sentinel */
};

static int l_rnn_etype_reg(lua_State *L, struct rnn *rnn,
		struct rnndelem *elem, uint64_t offset)
{
	push_rnndoff(L, rnn, elem, offset);

	luaL_newmetatable(L, "rnnmetareg");
	luaL_setfuncs(L, l_meta_rnn_reg, 0);
	lua_pop(L, 1);

	luaL_setmetatable(L, "rnnmetareg");

	return 1;
}

/*
 *
 */

static int l_rnn_meta_index(lua_State *L)
{
	struct rnn *rnn = lua_touserdata(L, 1);
	const char *name = lua_tostring(L, 2);
	struct rnndelem *elem;

	elem = rnn_regelem(rnn, name);
	if (!elem)
		return 0;

	return l_rnn_etype(L, rnn, elem, elem->offset);
}

static int l_rnn_meta_gc(lua_State *L)
{
	// TODO
	//struct rnn *rnn = lua_touserdata(L, 1);
	//rnn_deinit(rnn);
	return 0;
}

static const struct luaL_Reg l_meta_rnn[] = {
	{"__index", l_rnn_meta_index},
	{"__gc", l_rnn_meta_gc},
	{NULL, NULL}  /* sentinel */
};

static int l_rnn_init(lua_State *L)
{
	const char *gpuname = lua_tostring(L, 1);
	struct rnndec *rnndec = lua_newuserdata(L, sizeof(*rnndec));
	_rnn_init(&rnndec->base, 0);
	rnn_load(&rnndec->base, gpuname);
	rnndec->sizedwords = 0;

	luaL_newmetatable(L, "rnnmeta");
	luaL_setfuncs(L, l_meta_rnn, 0);
	lua_pop(L, 1);

	luaL_setmetatable(L, "rnnmeta");

	return 1;
}

static int l_rnn_enumname(lua_State *L)
{
	struct rnn *rnn = lua_touserdata(L, 1);
	const char *name = lua_tostring(L, 2);
	uint32_t val = (uint32_t)lua_tonumber(L, 3);
	lua_pushstring(L, rnn_enumname(rnn, name, val));
	return 1;
}

static int l_rnn_regname(lua_State *L)
{
	struct rnn *rnn = lua_touserdata(L, 1);
	uint32_t regbase = (uint32_t)lua_tonumber(L, 2);
	lua_pushstring(L, rnn_regname(rnn, regbase, 1));
	return 1;
}

static int l_rnn_regval(lua_State *L)
{
	struct rnn *rnn = lua_touserdata(L, 1);
	uint32_t regbase = (uint32_t)lua_tonumber(L, 2);
	uint32_t regval = (uint32_t)lua_tonumber(L, 3);
	struct rnndecaddrinfo *info = rnn_reginfo(rnn, regbase);
	char *decoded;
	if (info && info->typeinfo) {
		decoded = rnndec_decodeval(rnn->vc, info->typeinfo, regval);
	} else {
		asprintf(&decoded, "%08x", regval);
	}
	lua_pushstring(L, decoded);
	free(decoded);
	if (info) {
		free(info->name);
		free(info);
	}
	return 1;
}

static const struct luaL_Reg l_rnn[] = {
	{"init", l_rnn_init},
	{"enumname", l_rnn_enumname},
	{"regname", l_rnn_regname},
	{"regval", l_rnn_regval},
	{NULL, NULL}  /* sentinel */
};



/* Expose the register state to script enviroment as a "regs" library:
 */

static int l_reg_written(lua_State *L)
{
	uint32_t regbase = (uint32_t)lua_tonumber(L, 1);
	lua_pushnumber(L, reg_written(regbase));
	return 1;
}

static int l_reg_lastval(lua_State *L)
{
	uint32_t regbase = (uint32_t)lua_tonumber(L, 1);
	lua_pushnumber(L, reg_lastval(regbase));
	return 1;
}

static int l_reg_val(lua_State *L)
{
	uint32_t regbase = (uint32_t)lua_tonumber(L, 1);
	lua_pushnumber(L, reg_val(regbase));
	return 1;
}

static const struct luaL_Reg l_regs[] = {
	{"written", l_reg_written},
	{"lastval", l_reg_lastval},
	{"val",     l_reg_val},
	{NULL, NULL}  /* sentinel */
};

/* Expose API to lookup snapshot buffers:
 */

uint64_t gpubaseaddr(uint64_t gpuaddr);
unsigned hostlen(uint64_t gpuaddr);

/* given address, return base-address of buffer: */
static int l_bo_base(lua_State *L)
{
	uint64_t addr = (uint64_t)lua_tonumber(L, 1);
	lua_pushnumber(L, gpubaseaddr(addr));
	return 1;
}

/* given address, return the remaining size of the buffer: */
static int l_bo_size(lua_State *L)
{
	uint64_t addr = (uint64_t)lua_tonumber(L, 1);
	lua_pushnumber(L, hostlen(addr));
	return 1;
}

static const struct luaL_Reg l_bos[] = {
	{"base", l_bo_base},
	{"size", l_bo_size},
	{NULL, NULL}  /* sentinel */
};

static void openlib(const char *lib, const luaL_Reg *reg)
{
  lua_newtable(L);
  luaL_setfuncs(L, reg, 0);
  lua_setglobal(L, lib);
}

/* called at start to load the script: */
int script_load(const char *file)
{
	int ret;

	assert(!L);

	L = luaL_newstate();
	luaL_openlibs(L);
	openlib("bos", l_bos);
	openlib("regs", l_regs);
	openlib("rnn", l_rnn);

	ret = luaL_loadfile(L, file);
	if (ret)
		error("%s\n");

	ret = lua_pcall(L, 0, LUA_MULTRET, 0);
	if (ret)
		error("%s\n");

	return 0;
}


/* called at start of each cmdstream file: */
void script_start_cmdstream(const char *name)
{
	if (!L)
		return;

	lua_getglobal(L, "start_cmdstream");

	/* if no handler just ignore it: */
	if (!lua_isfunction(L, -1)) {
		lua_pop(L, 1);
		return;
	}

	lua_pushstring(L, name);

	/* do the call (1 arguments, 0 result) */
	if (lua_pcall(L, 1, 0, 0) != 0)
		error("error running function `f': %s\n");
}

/* called at each DRAW_INDX, calls script drawidx fxn to process
 * the current state
 */
void script_draw(const char *primtype, uint32_t nindx)
{
	if (!L)
		return;

	lua_getglobal(L, "draw");

	/* if no handler just ignore it: */
	if (!lua_isfunction(L, -1)) {
		lua_pop(L, 1);
		return;
	}

	lua_pushstring(L, primtype);
	lua_pushnumber(L, nindx);

	/* do the call (2 arguments, 0 result) */
	if (lua_pcall(L, 2, 0, 0) != 0)
		error("error running function `f': %s\n");
}


static int l_rnn_meta_dom_index(lua_State *L)
{
	struct rnn *rnn = lua_touserdata(L, 1);
	uint32_t offset = (uint32_t)lua_tonumber(L, 2);
	struct rnndelem *elem;

	/* TODO might be nicer if the arg isn't a number, to search the domain
	 * for matching bitfields.. so that the script could do something like
	 * 'pkt.WIDTH' insteadl of 'pkt[1].WIDTH', ie. not have to remember the
	 * offset of the dword containing the bitfield..
	 */

	elem = rnn_regoff(rnn, offset);
	if (!elem)
		return 0;

	return l_rnn_etype(L, rnn, elem, elem->offset);
}

/*
 * A wrapper object for rnndomain based decoding of an array of dwords
 * (ie. for pm4 packet decoding).  Mostly re-uses the register-value
 * decoding for the individual dwords and bitfields.
 */

static int l_rnn_meta_dom_gc(lua_State *L)
{
	// TODO
	//struct rnn *rnn = lua_touserdata(L, 1);
	//rnn_deinit(rnn);
	return 0;
}

static const struct luaL_Reg l_meta_rnn_dom[] = {
	{"__index", l_rnn_meta_dom_index},
	{"__gc", l_rnn_meta_dom_gc},
	{NULL, NULL}  /* sentinel */
};

/* called to general pm4 packet decoding, such as texture/sampler state
 */
void script_packet(uint32_t *dwords, uint32_t sizedwords,
		struct rnn *rnn, struct rnndomain *dom)
{
	if (!L)
		return;

	lua_getglobal(L, dom->name);

	/* if no handler for the packet, just ignore it: */
	if (!lua_isfunction(L, -1)) {
		lua_pop(L, 1);
		return;
	}

	struct rnndec *rnndec = lua_newuserdata(L, sizeof(*rnndec));

	rnndec->base = *rnn;
	rnndec->base.dom[0] = dom;
	rnndec->base.dom[1] = NULL;
	rnndec->dwords = dwords;
	rnndec->sizedwords = sizedwords;

	luaL_newmetatable(L, "rnnmetadom");
	luaL_setfuncs(L, l_meta_rnn_dom, 0);
	lua_pop(L, 1);

	luaL_setmetatable(L, "rnnmetadom");

	lua_pushnumber(L, sizedwords);

	if (lua_pcall(L, 2, 0, 0) != 0)
		error("error running function `f': %s\n");
}

/* helper to call fxn that takes and returns void: */
static void simple_call(const char *name)
{
	if (!L)
		return;

	lua_getglobal(L, name);

	/* if no handler just ignore it: */
	if (!lua_isfunction(L, -1)) {
		lua_pop(L, 1);
		return;
	}

	/* do the call (0 arguments, 0 result) */
	if (lua_pcall(L, 0, 0, 0) != 0)
		error("error running function `f': %s\n");
}

/* called at end of each cmdstream file: */
void script_end_cmdstream(void)
{
	simple_call("end_cmdstream");
}

/* called at start of submit/issueibcmds: */
void script_start_submit(void)
{
	simple_call("start_submit");
}

/* called at end of submit/issueibcmds: */
void script_end_submit(void)
{
	simple_call("end_submit");
}

/* called after last cmdstream file: */
void script_finish(void)
{
	if (!L)
		return;

	simple_call("finish");

	lua_close(L);
	L = NULL;
}