-
Notifications
You must be signed in to change notification settings - Fork 149
/
codegen.py
1062 lines (899 loc) · 35.5 KB
/
codegen.py
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- python -*-
# Code generation support: emitting files, emitting functions, etc.
#BEGIN_LEGAL
#
#Copyright (c) 2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#END_LEGAL
import sys
import os
import re
import glob
import datetime
from genutil import *
add_mbuild_to_path()
try:
import mbuild
except:
sys.stderr.write("\nERROR(file: codegen.py): Could not find mbuild. Might try setting PYTHONPATH env var.\n\n")
sys.exit(1)
class ip_header_t(object):
"""Intellectual property headers"""
def __init__(self):
self.lines = None
def read_header(self,fn):
##msge("Attempting to open: " + fn)
fp = base_open_file(fn,"r")
self.lines = fp.readlines()
def emit_header(self, shell_type = False):
eol = '\n'
out = []
if shell_type:
out.append("#BEGIN_LEGAL" + eol)
for line in self.lines:
out.append("#" + line)
out.append("#END_LEGAL" + eol)
else:
out.append("/*BEGIN_LEGAL" + eol)
out.extend(self.lines)
out.append("END_LEGAL */" + eol)
return out
class file_emitter_t(object):
"""Attach IP headers, standard includes, and namespace decorations
to generated files. This replaces the file objects I was using for
emitting files."""
header_file_name_pattern = re.compile(r'.[hH]$')
# note: in the following the '-' must be last or it will (try to) act like a range!
header_guard_pattern = re.compile(r'[./-]')
def __init__(self,gendir, file_name, shell_file=False, namespace=None):
"""gendir is the output dir. If shell_file is True, we delimit
the header differently."""
self.file_name = file_name
self.gendir = gendir
self.namespace = namespace
# True for shell-like files, False for C++ files. Determines the comment syntax
self.shell_file = shell_file
self.lines = []
self.full_file_name = mbuild.join(self.gendir, self.file_name)
self.eol = '\n'
self.closed = False
self.header = False
if file_emitter_t.header_file_name_pattern.search(self.file_name):
self.header = True
self.headers = []
self.system_headers = []
self.misc_header = []
def add_header(self,h):
"""Add h to the list of headers"""
if type(h) == list:
self.headers.extend(h)
else:
self.headers.append(h)
def add_system_header(self,h):
"""Add h to the list of system headers"""
if type(h) == list:
self.system_headers.extend(h)
else:
self.system_headers.append(h)
def add_misc_header(self,h):
if type(h) == list:
self.misc_header.extend(h)
else:
self.misc_header.append(h)
def replace_headers(self,h):
"""Replace the existing headers with the header h"""
if type(h) == list:
self.headers = h
else:
self.headers = [h]
def start(self, full_header=True):
"""Call this this after creating the objectd"""
self.emit_header(full_header)
if not self.shell_file:
self.system_headers_emit()
self.user_headers_emit()
self.misc_headers_emit()
self.namespace_start()
def count_lines(self):
return len(self.lines)
def write(self,str):
"""Replaces the file pointer write() function call"""
self.lines.append(str)
def writelines(self,list_of_str):
"""Replaces the file pointer writelines() function call"""
self.lines.extend(list_of_str)
def add_code(self,str):
"""Add a line and newline"""
self.write(str+'\n')
def add_code_eol(self,str):
"""Add a line with semicolon, newline"""
self.add_code(str+';')
def close(self):
if not self.closed:
self.closed = True
if not self.shell_file:
self.namespace_end()
if self.header:
self.emit_header_guard_end()
self.emit_file()
del self.lines
else:
msge("FE: Closing an already-closed file: " + self.full_file_name)
def emit_file(self):
msge("FE:EMIT_FILE " + self.full_file_name)
fp = self.open_file(self.full_file_name,"w")
fp.writelines(self.lines)
fp.close()
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def open_file(self,fn,rw):
fp = base_open_file(fn,rw)
return fp
def emit_ip_header(self, ip_header_file_name):
iph = ip_header_t()
iph.read_header(ip_header_file_name)
s = iph.emit_header(self.shell_file)
return s
def emit_header(self, full_header=True):
if full_header:
self.dox('@file ' + self.file_name)
self.emit()
self.cmt('This file was automatically generated.')
self.cmt('Do not edit this file.')
self.emit()
if self.header:
self.emit_header_guard_start()
def emit_header_guard_start(self):
s = file_emitter_t.header_guard_pattern.sub('_',self.file_name)
defname = '%s' % s.upper()
self.emit_eol('#if !defined(' + defname + ')')
self.emit_eol('# define ' + defname )
def emit_header_guard_end(self):
self.emit_eol("#endif")
def dox(self,s):
if self.shell_file:
self.emit_eol('# '+ s)
else:
self.emit_eol('/// '+ s)
def cmt(self,s):
if self.shell_file:
self.emit_eol('# '+ s)
else:
self.emit_eol('// '+ s)
def emit(self,s='\n'):
self.lines.append(s)
def emit_eol(self,s=''):
self.emit(s + '\n')
def user_headers_emit(self):
for h in self.headers:
self.emit_eol('#include \"%s\"' % h )
def system_headers_emit(self):
for h in self.system_headers:
self.emit_eol('#include <%s>' % h )
def misc_headers_emit(self):
for h in self.misc_header:
self.emit_eol(h)
def namespace_start(self):
if self.namespace:
self.emit_eol( ''.join(['namespace ' , self.namespace , ' {']))
def namespace_end(self):
if self.namespace:
msge("FE:NAMESPACE " + self.full_file_name)
self.emit_eol( '} // namespace')
class xed_file_emitter_t(file_emitter_t):
"""Attach IP headers, standard includes, and namespace decorations
to generated files. This replaces the file objects I was using for
emitting files."""
def __init__(self, xeddir, gendir, file_name, shell_file=False,
namespace=None, is_private=True):
file_emitter_t.__init__( self,gendir, file_name, shell_file, namespace)
self.xeddir = xeddir
if is_private:
self.headers.append('xed-internal-header.h')
def start(self, full_header=True):
"""override the parent's start() function to apply the IP
header."""
self.emit_header(full_header)
ip_header_file_name = mbuild.join(self.xeddir,
'misc',
'legal-header.txt')
curr_year = datetime.date.today().strftime("%Y")
for line in self.emit_ip_header(ip_header_file_name):
if "<CURRENT_YEAR>" in line:
line = line.replace("<CURRENT_YEAR>", curr_year)
self.emit(line)
if not self.shell_file:
self.system_headers_emit()
self.user_headers_emit()
self.misc_headers_emit()
self.namespace_start()
inline_sring = "XED_INLINE"
class function_object_t(object):
inline_string = "XED_INLINE"
def __init__(self,name, return_type='xed_bool_t', static=False, inline=False,
doxgroup=None, force_no_inline=False, dll_export=False):
self.function_name = name
self.doxgroup=doxgroup
self.return_type = return_type
self.static=static
self.inline=inline
self.dll_export = dll_export
self.body = []
self.args = [] # list of pairs, (type+arg, meta type comment)
self.const_member = False
self.ref_return = False
self.force_no_inline = force_no_inline
def set_function_name(self,fname):
self.function_name = fname
def get_function_name(self):
return self.function_name
def lines(self):
return len(self.body)
def add_arg(self, arg, arg_type=None):
self.args.append((arg,arg_type))
def get_args(self):
return self.args
def get_arg_num(self):
return len(self.args)
def add_comment(self,s):
self.body.append(''.join(['/* ' , s , ' */']))
def set_const_member(self):
self.const_member = True
def set_ref_return(self):
self.ref_return = True
def add_code(self, line, comment=None):
if comment:
self.body.append(line + ' // {} '.format(comment) )
else:
self.body.append(line)
def add_code_eol(self, line, comment=None):
if comment:
self.body.append(line + '; // {} '.format(comment) )
else:
self.body.append(line + ';')
def add_lines(self, lines):
self.body.extend(lines)
def emit_header_internal(self, class_qualfier='', emit_group=False):
"""private function that emits the function name and args, but
no newline or semicolon"""
s = []
if emit_group and self.doxgroup:
s.append("/// @ingroup %s\n" % (self.doxgroup))
if self.static:
s.append("static ")
if self.inline:
s.append( function_object_t.inline_string + " ")
if self.force_no_inline:
s.append('XED_NOINLINE ')
if self.dll_export:
s.append('XED_DLL_EXPORT ')
s.append(self.return_type)
if self.ref_return:
s.append('&')
s.append(' ')
s.append(class_qualfier)
s.append(self.function_name)
s.append('(')
first_arg = True
for arg,arg_type in self.args:
if first_arg:
first_arg = False
else:
s.append(', ')
if arg_type:
s.append("{} /*{}*/".format(arg,arg_type))
else:
s.append(arg)
if first_arg: # no actual args so emit a "void"
s.append('void')
s.append(')')
if self.const_member:
s.append(' const')
return ''.join(s)
def emit_header(self):
'emit the header with the semicolon and newline'
s = [ self.emit_header_internal(emit_group=True), ";\n" ]
return ''.join(s)
def emit(self, class_qualfier=''):
'emit the function body'
eol = '\n'
s = [ self.emit_header_internal(class_qualfier) , eol ]
s.append('{')
s.append(eol)
for bline in self.body:
s.extend([ ' ' , bline , eol])
s.append('}')
s.append(eol)
return ''.join(s)
def emit_file_emitter(self, fe, class_qualfier=''):
'emit the function body'
fe.add_code(self.emit_header_internal(class_qualfier))
fe.add_code('{')
for bline in self.body:
fe.add_code(bline)
fe.add_code('}')
def emit_body(self):
'emit function body as string'
return '\n'.join([bline + ';' for bline in self.body])
############################################################
def dump_flist_2_header(h_file, functions, headers,
emit_headers=True,
emit_bodies=True):
''' emits the list of functions objects to a header file
@type: functions: list of function_object_t
@param functions: the function to emit
@type: h_file: xed_file_emitter_t
@param h_file: emmiting the function to this headr file
@type: headers: list of strings
@param headers: include headers to emit
'''
for header in headers:
h_file.add_header(header)
h_file.start()
if emit_headers:
for fo in functions:
decl = fo.emit_header()
h_file.add_code(decl)
if emit_bodies:
for fo in functions:
fo.emit_file_emitter(h_file)
h_file.close()
def emit_function_list(func_list,
fn_prefix,
xeddir,
gendir,
hgendir,
namespace=None,
other_headers=None,
max_lines_per_file=3000,
is_private_header=True,
extra_public_headers=None): # list
"""Emit a list of functions to a numbered sequence of
files. Breaking them up when the files get too big.
@type func_list: list of function_object_t objects
@param func_list: functions to emit
@type fn_prefix: string
@param fn_prefix: basis for the output file names.
@type xeddir: string
@param xeddir: location of the source directory so that we can find the legal header
@type gendir: string
@param gendir: directory where the output files go.
@type hgendir: string
@param hgendir: directory where the output hdr files go.
@type namespace: string
@param namespace: defaults to XED
@type other_headers: list
@param other_headers: extra headers to include
@type max_lines_per_file: int
@param max_lines_per_file: Approximate limit for file size, in lines.
"""
file_number = 0
fe = None
fn_header = "{}.h".format(fn_prefix)
companion_header = fn_header
if not is_private_header:
companion_header = mbuild.join('xed',fn_header)
fe_list = []
fe_header = xed_file_emitter_t(xeddir,hgendir,fn_header,shell_file=False,namespace=namespace, is_private=is_private_header)
if extra_public_headers:
fe_header.add_header(extra_public_headers)
fe_header.start()
fe_list.append(fe_header)
# remove any numbered files that we previously emitted. We won't
# necessarily overwrite them all each build and do not want
# stale files remaining from previous builds
for fn in glob.glob(mbuild.join(gendir, fn_prefix + '-[0-9]*.c')):
mbuild.remove_file(fn)
for func in func_list:
fe_header.write(func.emit_header())
if not fe or fe.count_lines() + func.lines() >= max_lines_per_file:
if fe:
fe.close()
fn = "%s-%d.c" % (fn_prefix, file_number)
fe = xed_file_emitter_t(xeddir,gendir, fn, shell_file=False, namespace=namespace)
fe.add_header(companion_header)
if other_headers:
for header in other_headers:
fe.add_header(header)
fe.start()
fe_list.append(fe)
file_number += 1
func.emit_file_emitter(fe)
fe.close()
fe_header.close()
return fe_list
############################################################
def function_call_sequence(fname, lst):
"""Return a function object (returning nothing) for a function
named fname that calls all the functions in lst.
@type fname: string
@param fname: function name
@type lst: list
@param lst: list of function names without parens
@rtype: function_object_t
@return: function that calls each function in lst
"""
fo = function_object_t(fname, "void")
for fn in lst:
fo.add_code_eol(fn + "()")
return fo
def function_call_sequence_conditional(fname, lst, subroutine_arg=''):
"""Return a function object (that returns xed_bool_t) for a function named fname that calls
all the functions in lst. Check each function call for an okay
return value and have this function return false if any of the
subroutines return false.
@type fname: string
@param fname: function name
@type lst: list
@param lst: list of function names without parens
@type subroutine_arg: string
@param subroutine_arg: optional parameter for the called functions
@rtype: function_object_t
@return: function that calls each function in lst
"""
fo = function_object_t(fname, "xed_bool_t")
fo.add_code_eol("xed_bool_t okay")
args = "(%s)" % subroutine_arg
for fn in lst:
fo.add_code_eol("okay = " + fn + args)
fo.add_code_eol("if (!okay) return 0")
fo.add_code_eol("return 1")
return fo
class class_generator_t(object):
"""Generate code for a c++ class (or union) declaration and
implementation.
If you want initialization or a printer, you can add your create
your own functions and add them with add_function().
"""
inline_string = "XED_INLINE"
inline_pattern = re.compile(inline_string)
def __init__(self,name, class_or_union='class', var_prefix = "_"):
self.name = name
self.var_type = [] # list of (variable,type,bit_width) tuples
self.functions = [] # member functions
self.class_or_union = class_or_union
self.var_prefix = var_prefix
def add_var(self, var, type, bit_width = None, accessors='set-get'):
"""Add a variable var of type. If accessors is set, generate
set/get functions for it. The potential values are the following
strings:
set
get
set-get
get-ref
set-get-array
none
"""
pvar = self.var_prefix + var
self.var_type.append((pvar,type,bit_width))
if accessors == 'set-get-array':
self.add_function(self.add_get_array_fn(var,pvar,type))
self.add_function(self.add_set_array_fn(var,pvar,type))
if accessors == 'set-get':
self.add_function(self.add_get_fn(var,pvar,type))
self.add_function(self.add_set_fn(var,pvar,type))
elif accessors == 'set':
self.add_function(self.add_set_fn(var,pvar,type))
elif accessors == 'get':
self.add_function(self.add_get_fn(var,pvar,type))
elif accessors == 'get-ref':
self.add_function(self.add_get_ref_fn(var,pvar,type))
elif accessors == 'none':
pass
else:
die("Unhandled accessor keyword: " + accessors)
def add_get_ref_fn(self,var,pvar,type):
'A get-accessor function for class variable pvar, returns a reference'
fname = 'get_' + var
fo = function_object_t(fname, class_generator_t.inline_string + ' ' + type)
fo.set_ref_return()
fo.add_code_eol( 'return %s' %( pvar ))
return fo
def add_get_fn(self,var,pvar,type):
'A get-accessor function for class variable pvar'
fname = 'get_' + var
fo = function_object_t(fname, class_generator_t.inline_string + ' ' + type)
fo.set_const_member()
fo.add_code_eol( 'return %s' % ( pvar ))
return fo
def add_set_fn(self, var,pvar,type):
'A set-accessor function for class variable pvar'
fname = 'set_' + var
fo = function_object_t(fname, class_generator_t.inline_string + ' void')
fo.add_arg(type + ' arg_' + var)
fo.add_code_eol( '%s=arg_%s' % (pvar,var))
return fo
def add_get_array_fn(self,var,pvar,type):
'A get-accessor function for class variable pvar'
fname = 'get_' + var
fo = function_object_t(fname, class_generator_t.inline_string + ' ' + type)
fo.add_arg("unsigned int idx") #FIXME: parameterize unsigned int
fo.set_const_member()
# FIXME: add bound checking for array index
fo.add_code_eol( 'return %s[idx]' % (pvar))
return fo
def add_set_array_fn(self, var,pvar,type):
'A set-accessor function for class variable pvar'
fname = 'set_' + var
fo = function_object_t(fname, class_generator_t.inline_string +' void')
fo.add_arg("unsigned int idx") #FIXME: parameterize unsigned int
fo.add_arg(type + ' arg_' + var)
# FIXME add bounds checking for array index
fo.add_code_eol( '%s[idx]=arg_%s' % (pvar, var))
return fo
def add_function(self, function):
self.functions.append(function)
def emit_decl(self):
'emit the class declaration'
eol = '\n'
pad = ' '
s = []
s.append(self.class_or_union + ' ' + self.name + eol)
s.append('{' + eol)
s.append(' public:' + eol)
for (var,type,bit_width) in self.var_type:
t = pad + type + ' ' + var
if bit_width:
t += ' : ' + str(bit_width)
s.append(t+ ';' + eol)
for fo in self.functions:
s.append( pad + fo.emit_header())
s.append('};' + eol)
# emit the inline functions in the header
for fo in self.functions:
if class_generator_t.inline_pattern.search(fo.return_type):
s.append(fo.emit(self.name + '::') )
return ''.join(s)
def emit_impl(self):
'emit the class implementation'
s = []
# only emit the noninline functions
for fo in self.functions:
if not class_generator_t.inline_pattern.search(fo.return_type):
s.append(fo.emit(self.name + '::') )
return ''.join(s)
############################################################################
class c_switch_generator_t(object):
def __init__(self, var_name, func_obj, pad=' '):
self.func_obj = func_obj
self.var_name = var_name
self.pad = pad
self._add('switch(%s) {' % (self.var_name))
def _add(self,s):
self.func_obj.add_code(self.pad + s)
def add(self,s, pad=' '):
self._add(pad + s)
def add_case(self,case_name,clines, do_break=True):
"""Add a case with a bunch of lines of code -- no semicolons
required"""
self._add('case %s:' % (case_name))
for line in clines:
self.add(line)
if do_break:
self.add('break;')
def add_default(self,clines, do_break=True):
"""Add a default case with a bunch of lines of code -- no
semicolons required"""
self._add('default:')
for line in clines:
self.add(line)
if do_break:
self.add('break;')
def finish(self):
self._add('}')
############################################################################
class c_class_generator_t(object):
"""Generate code for a C struct (or union) declaration and
implementation.
If you want initialization or a printer, you can add your create
your own functions and add them with add_function().
"""
type_ending_pattern = re.compile(r'_t$')
def remove_suffix(self,x):
return c_class_generator_t.type_ending_pattern.sub('',x)
def __init__(self,name, class_or_union='struct', var_prefix = "_"):
self.name = name
self.var_type = [] # list of (variable,type,bit_width) tuples
self.array_type = [] # list of (variable,type,limit) tuples
self.functions = [] # member functions
self.class_or_union = class_or_union
self.var_prefix = var_prefix
def add_array(self, var, type, limit):
"""Add an array variable var of type.
"""
pvar = self.var_prefix + var
self.array_type.append((pvar,type,limit))
self.add_function(self.add_get_array_fn(var,pvar,type))
self.add_function(self.add_set_array_fn(var,pvar,type))
def add_var(self, var, type, bit_width = None, accessors='set-get'):
"""Add a variable var of type. If accessors is set, generate
set/get functions for it. The potential values are the following
strings:
set
get
set-get
get-ref
set-get-array
none
"""
pvar = self.var_prefix + var
self.var_type.append((pvar,type,bit_width))
if accessors == 'set-get-array':
self.add_function(self.add_get_array_fn(var,pvar,type))
self.add_function(self.add_set_array_fn(var,pvar,type))
if accessors == 'set-get':
self.add_function(self.add_get_fn(var,pvar,type))
self.add_function(self.add_set_fn(var,pvar,type))
elif accessors == 'set':
self.add_function(self.add_set_fn(var,pvar,type))
elif accessors == 'get':
self.add_function(self.add_get_fn(var,pvar,type))
elif accessors == 'get-ref':
self.add_function(self.add_get_ref_fn(var,pvar,type))
elif accessors == 'none':
pass
else:
die("Unhandled accessor keyword: " + accessors)
def add_get_ref_fn(self,var,pvar,type):
"""A get-accessor function for class variable pvar, returns a POINTER"""
fname = self.remove_suffix(self.name) + '_get_' + var
fo = function_object_t(fname, type + "*")
fo.add_arg("%s* ppp" % self.name)
fo.add_code_eol( 'return &(ppp->%s)' %( pvar ))
return fo
def add_get_fn(self,var,pvar,type):
'A get-accessor function for class variable pvar'
fname = self.remove_suffix(self.name) + '_get_' + var
fo = function_object_t(fname, type)
fo.add_arg("%s* ppp" % self.name)
fo.add_code_eol( 'return ppp->%s' % ( pvar ))
return fo
def add_set_fn(self, var,pvar,type):
'A set-accessor function for class variable pvar'
fname = self.remove_suffix(self.name) + '_set_' + var
fo = function_object_t(fname, 'void')
fo.add_arg("%s* ppp" % self.name)
fo.add_arg(type + ' arg_' + var)
fo.add_code_eol( 'ppp->%s=arg_%s' % (pvar,var))
return fo
def add_get_array_fn(self,var,pvar,type):
'A get-accessor function for class variable pvar'
fname = self.remove_suffix(self.name) + '_get_' + var
fo = function_object_t(fname, type)
fo.add_arg("%s* ppp" % self.name)
fo.add_arg("unsigned int idx") #FIXME: parameterize unsigned int
# FIXME: add bound checking for array index
fo.add_code_eol( 'return ppp->%s[idx]' % (pvar))
return fo
def add_set_array_fn(self, var,pvar,type):
'A set-accessor function for class variable pvar'
fname = self.remove_suffix(self.name) + '_set_' + var
fo = function_object_t(fname, 'void')
fo.add_arg("%s* ppp" % self.name)
fo.add_arg("unsigned int idx") #FIXME: parameterize unsigned int
fo.add_arg(type + ' arg_' + var)
# FIXME add bounds checking for array index
fo.add_code_eol( 'ppp->%s[idx]=arg_%s' % (pvar, var))
return fo
def add_function(self, function):
self.functions.append(function)
def emit_decl(self):
'emit the class declaration'
eol = '\n'
pad = ' '
s = []
# I replace the suffix _t$ first then append a _s or _u. This way it
# adds a _s/_u even if no _t is present.
struct_name = re.sub(r'_t$', '',self.name )
if self.class_or_union == 'union':
struct_name += '_u'
else:
struct_name += '_s'
s.append('typedef %s %s {\n' % (self.class_or_union, struct_name))
for (var,type,limit) in self.array_type:
t = "%s %s %s[%s];\n" % ( pad, type, var, str(limit))
s.append(t)
for (var,type,bit_width) in self.var_type:
t = '%s %s %s' % ( pad, type, var)
if bit_width:
t += ' : ' + str(bit_width)
s.append(t+ ';' + eol)
s.append('} %s;\n' % self.name )
# accessor function prototypes
for fo in self.functions:
s.append( pad + fo.emit_header())
return ''.join(s)
def emit_impl(self):
"""emit the class implementation"""
s = []
# only emit the noninline functions
for fo in self.functions:
s.append(fo.emit() )
return ''.join(s)
############################################################################
class array_gen_t(object):
"""A simple C++ multidimensional array generator. The ranges are
typed. New values are added by specifying a list of indices, one
per dimension and a value"""
def __init__(self, name, type, target_op=None):
"""Set the name and storage type for the array"""
self.name = name
self.type = type
self.target_op = target_op
self.ranges = [] # list of tuples (range_type, minval, maxval+1, argname )
# including the max
# value for dimensioning the array
self.values = [] # list of tuples of tuples (dict(names->indices), value)
self.lookup_fn = None
self.init_fn = None
def add_dimension(self, range_type, range_min, range_max, argname):
"""For one dimension, add the type of the range index and the
min/max range values."""
self.ranges.append((range_type,range_min, range_max, argname))
def get_arg_names(self):
return [range_tuple[3] for range_tuple in self.ranges]
def get_target_opname(self):
"""Return the name of target operand of lookup function
(if it was supplied)"""
return self.target_op
def get_dimension_num(self):
"""Return the number of array dimensions.
In other words the number of the lookup function parameters."""
return len(self.ranges)
def get_values_space(self):
"""Return a list of all possible return values of a lookup function"""
return [val for (_idx_dict, val) in self.values]
def is_const_lookup_fun(self):
"""Return true if a lookup function always returns same value.
Const lookup function and the array is just a variable."""
return self.get_dimension_num() == 0
def add_value(self, indx_dict, value):
"""set the scalar value for the dictionary of indices"""
self.values.append((indx_dict, value))
def validate(self):
"""Make sure that all the index arrays have the expected
number of elements."""
expected_len = len(self.ranges)
for idict,value in self.values:
if len(list(idict.keys())) != expected_len:
return False
return True
def gen_lookup_function(self, fn_name, check_bounds=True, static=False,
inline=False, check_const=False):
"""Create a lookup function that will look up the value and
return the appropriate type. Typed args will be added for each
dimension
check_const argument is for checking if lookup function is of a form
return var; - constant function. Then we can optimize it to a form
return const; where const is a compile-time constant (a number).
"""
fo = function_object_t(fn_name, self.type, static=static, inline=inline)
#optimization for const functions
if check_const:
if self.is_const_lookup_fun() and len(self.values) == 1:
fo.add_code('/*Constant function*/')
value = self.values[0][1]
fo.add_code_eol('return %s' % str(value))
self.lookup_fn = fo
return
fo.add_code_eol(self.type + ' _v')
index_expression = ''
for i,range_tuple in enumerate(self.ranges):
range_type, range_min, range_max, argname = range_tuple
fo.add_arg(range_type + ' arg_' + argname)
index_expression += '[arg_%s]' % (argname)
lower_bound = str(range_min)
upper_bound = str(range_max)
if check_bounds:
# FIXME: if the range type is unsigned, and the lower
# bound is zero, then we need not check it. But it is
# hard to tell from here with an arbitrary type.
# ICL complains about this, warning/error #186.
# GCC complains about this, -Werror=type-limits
fo.add_code_eol('xed_assert(arg_'+ argname + '>=' + lower_bound +
' && arg_' + argname + '<' + upper_bound + ')')
fo.add_code_eol('_v=' + self.name + index_expression)
fo.add_code_eol('return _v')
self.lookup_fn = fo
def emit_lookup_function(self):
"""Emit the lookup function as a string"""
if self.lookup_fn == None:
die("Need to generate the lookup function first for " + self.name)
return self.lookup_fn.emit()
def emit_lookup_function_header(self):
"""Emit the lookup function header as a string"""
if self.lookup_fn == None:
die("Need to generate the lookup function first for " + self.name)
return self.lookup_fn.emit_header()
def emit_initialization_function_header(self):
if self.init_fn == None:
die("Need to generate the init function first for " + self.name)
return self.init_fn.emit_header()
def emit_declaration(self, extern=False, static=False):
"""Emit the array declaration as a string."""
s = []
if static:
s.append( 'static ')
if extern:
s.append( 'extern ')
s.append( self.type + ' ' + self.name)
for range_type, range_min, range_max, argname in self.ranges:
s.append('[%s]' % (str(range_max)))
s.append(';\n')
return ''.join(s)
def compute_missing_values(self,key):
present_values = {}
for indices_dict,value in self.values:
if key in indices_dict:
present_values[indices_dict[key]] = True
return list(present_values.keys())
def make_initialization_function(self, init_function_name,verbose=False):
fo = function_object_t(init_function_name,'void')
lines = self.emit_initialization(verbose=verbose)