generate_new_opcodes.pl 25.7 KB
Newer Older
1
2
#!/usr/bin/perl -w

Christian Würdig's avatar
Christian Würdig committed
3
4
#
# This file is part of libFirm.
5
# Copyright (C) 2014 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
6
7
#

8
# This script generates the C code which creates the irop's and
9
# their corresponding node constructors for all operations in a given spec
10
11
12
13
14
15
16
17
18
19
20
# so they can be used as normal firm nodes.
# Creation: 2005/10/19

use strict;
use Data::Dumper;

my $specfile   = $ARGV[0];
my $target_dir = $ARGV[1];

our $arch;
our %nodes;
21
our $default_attr_type;
22
our $default_copy_attr;
23
our %init_attr;
24
our $custom_init_attr_func;
25
our %copy_attr;
26
our %reg_classes;
27
our %custom_irn_flags;
28
29
30
31
32
33
34

# include spec file

my $return;

no strict "subs";
unless ($return = do $specfile) {
35
36
37
	die "Fatal error: couldn't parse $specfile: $@" if $@;
	die "Fatal error: couldn't do $specfile: $!"    unless defined $return;
	die "Fatal error: couldn't run $specfile"       unless $return;
38
39
40
}
use strict "subs";

41
my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
Christian Würdig's avatar
Christian Würdig committed
42
my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
43

44
45
46
if(!defined($default_attr_type)) {
	$default_attr_type = "${arch}_attr_t";
}
Matthias Braun's avatar
Matthias Braun committed
47
if(! %init_attr) {
48
	%init_attr = (
49
		$default_attr_type => "be_info_init_irn(res, irn_flags_, in_reqs, n_res);",
50
51
	);
}
52

53
54
# create c code file from specs

55
56
57
58
59
60
61
62
my $obst_limit_func  = ""; #
my $obst_reg_reqs    = ""; #
my $obst_opvar       = ""; # buffer for the "ir_op *op_<arch>_<op-name> = NULL;" statements
my $obst_constructor = ""; # buffer for node constructor functions
my $obst_new_irop    = ""; # buffer for the new_ir_op calls
my $obst_free_irop   = ""; # buffer for free_ir_op calls
my $obst_enum_op     = ""; # buffer for creating the <arch>_opcode enum
my $obst_header      = ""; # buffer for function prototypes
63
my $obst_attrs_equal = ""; # buffer for the compare attribute functions
64
my $obst_proj        = ""; # buffer for the pn_ numbers
65
66
my $orig_op;
my $arity;
Christian Würdig's avatar
Christian Würdig committed
67
my $cmp_attr_func;
68
my $temp;
69
my $n_opcodes = 0;    # number of opcodes
70
my $ARITY_VARIABLE = -1;
71
72
73
74
75
76
my %requirements = ();
my %limit_bitsets = ();
my %reg2class = ();
my %regclass2len = ();

# build register->class hashes
77
foreach my $class_name (sort(keys(%reg_classes))) {
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
	my @class         = @{ $reg_classes{"$class_name"} };
	my $old_classname = $class_name;

	pop(@class);

	$class_name = $arch."_".$class_name;

	my $idx = 0;
	foreach (@class) {
		$reg2class{$_->{name}} = {
			"class" => $old_classname,
			"index" => $idx
		};
		$idx++;
	}

	$regclass2len{$old_classname} = $idx;
}

97

98
$obst_header .= "void ${arch}_create_opcodes(void);\n";
Matthias Braun's avatar
Matthias Braun committed
99
$obst_header .= "void ${arch}_free_opcodes(void);\n";
100

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
sub create_constructor {
	my $op   = shift;
	my $name = shift;
	my $n    = shift;
	my $on   = shift;
	my $known_mode;

	my $suffix = "";
	if ($name ne "") {
		$suffix = "_${name}";
	}

	# determine mode
	if (exists($n->{mode})) {
		$known_mode = $n->{mode};
	}

118
	my $in_reqs = $n->{"in_reqs"};
119
120
	# determine arity
	my $arity = 0;
121
122
123
124
125
126
127
	if ($in_reqs) {
		if ($in_reqs eq "...") {
			$arity   = $ARITY_VARIABLE;
			$in_reqs = undef;
		} else {
			$arity = scalar(@$in_reqs);
		}
128
129
130
131
	} elsif (exists($n->{"ins"})) {
		$arity = scalar(@{ $n->{"ins"} });
	}

132
	my $out_reqs = $n->{"out_reqs"};
133
134
	# determine out arity
	my $out_arity = 0;
135
136
137
138
139
140
141
	if ($out_reqs) {
		if ($out_reqs eq "...") {
			$out_arity = $ARITY_VARIABLE;
			$out_reqs  = undef;
		} else {
			$out_arity = scalar(@$out_reqs);
		}
142
143
144
	} elsif (exists($n->{"outs"})) {
		$out_arity = scalar(@{ $n->{"outs"} });
	}
145
146
147
	if ($out_arity != 0 && $out_arity != 1 && !defined($known_mode)) {
		$known_mode = "mode_T";
	}
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

	my $comment = $n->{"comment"};
	if(!exists($n->{"comment"})) {
		$comment = "construct ${orig_op} node";
	}
	$comment =
		"/**\n".
		" * ${comment}\n".
		" */\n";

	$obst_constructor .= $comment;

	# create constructor head
	my $complete_args = "";
	$temp             = "";

	$temp = "ir_node *new_bd_${arch}_${op}${suffix}(dbg_info *dbgi, ir_node *block";
165
166
167
168
169
170
171
172
	if ($arity == $ARITY_VARIABLE) {
		$complete_args = ", int arity, ir_node *in[]";
	} else {
		for (my $i = 0; $i < $arity; $i++) {
			my $opname = "op${i}";
			if (exists($n->{"ins"})) {
				my @ins = @{ $n->{"ins"} };
				$opname = $ins[$i];
173
			}
174
			$complete_args .= ", ir_node *${opname}";
175
		}
176
177
178
179
180
181
	}
	if ($out_arity == $ARITY_VARIABLE) {
		$complete_args .= ", int n_res";
	}
	if (!defined($known_mode)) {
		$complete_args .= ", ir_mode *mode";
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
	}

	# we have additional attribute arguements
	if (exists($n->{"attr"})) {
		$complete_args .= ", ".$n->{"attr"};
	}

	$temp .= "$complete_args)";
	$obst_constructor .= "${temp}\n{\n";

	$obst_header .= $comment;
	$obst_header .= "${temp};\n";

	# emit constructor code
	$temp = <<EOF;
197
	arch_irn_flags_t irn_flags_ = arch_irn_flags_none;
198
199
EOF

200
	if ($arity != $ARITY_VARIABLE) {
201
		$temp .= <<EOF;
202
	int      const   arity      = $arity;
203
204
205
EOF
		if($arity > 0) {
			$temp .= <<EOF;
206
	ir_node         *in[$arity];
207
208
209
EOF
		} else {
			$temp .= <<EOF;
210
	ir_node        **in         = NULL;
211
212
213
EOF
		}
	}
214
	if ($out_arity != $ARITY_VARIABLE) {
215
		$temp .= <<EOF;
216
	int      const   n_res      = ${out_arity};
217
218
219
220
221
222
223
224
225
EOF
	}

	undef my $in_req_var;
	undef my $out_req_var;

	my $set_out_reqs = "";

	# set up static variables for requirements and registers
226
	my $idx;
227

228
229
230
231
232
	if ($in_reqs) {
		my $idx = 0;
		for my $req (@$in_reqs) {
			generate_requirements($req, $n, "${arch}_${op}", $idx++, 1);
		}
233
	}
234
235
236
237
238
	if ($out_reqs) {
		my $idx = 0;
		for my $req (@$out_reqs) {
			generate_requirements($req, $n, "${arch}_${op}", $idx++, 0);
		}
239
	}
240

241
242
	if ($in_reqs) {
		if ($arity >= 0 && scalar(@$in_reqs) != $arity) {
243
244
			die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
		}
245

246
247
		$temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
		$temp .= "\t{\n";
248
249
250
		my $idx = 0;
		for my $req (@$in_reqs) {
			my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx++, 1);
251
			$temp .= "\t\t& ${reqstruct},\n";
252
		}
253
254
255
256
		$temp .= "\t};\n";
	} else {
		$temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
	}
257

258
259
	if ($out_reqs) {
		if ($out_arity >= 0 && scalar(@$out_reqs) != $out_arity) {
260
261
			die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
		}
262

263
264
		my $idx = 0;
		for my $req (@$out_reqs) {
265
266
267
			my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
			$set_out_reqs .= <<EOF;
info->out_infos[${idx}].req = &${reqstruct};
268
EOF
269
			++$idx;
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
		}
	}
	my $attr_type = $on->{attr_type};

	$temp .= "\n";

	if($arity > 0) {
		$temp .= "\t/* construct in array */\n";
		for (my $i = 0; $i < $arity; $i++) {
			my $opname = "op${i}";
			if (exists($n->{"ins"})) {
				my @ins = @{ $n->{"ins"} };
				$opname = $ins[$i];
			}

			$temp .= "\tin[${i}] = ${opname};\n";
		}
		$temp .= "\n";
	}

	# set flags
	if (exists($n->{"irn_flags"})) {
		$temp .= "\t/* flags */\n";
293
		my %known_irn_flags = (
294
295
296
297
298
			"none"             => "arch_irn_flag_none",
			"dont_spill"       => "arch_irn_flag_dont_spill",
			"rematerializable" => "arch_irn_flag_rematerializable",
			"modify_flags"     => "arch_irn_flag_modify_flags",
			"simple_jump"      => "arch_irn_flag_simple_jump",
299
			"schedule_first"   => "arch_irn_flag_schedule_first",
300
			"not_scheduled"    => "arch_irn_flag_not_scheduled",
301
		);
Matthias Braun's avatar
Matthias Braun committed
302
		if (%custom_irn_flags) {
303
304
			%known_irn_flags = (%known_irn_flags, %custom_irn_flags);
		}
305
306
307
		foreach my $flag (@{$n->{"irn_flags"}}) {
			if (not defined($known_irn_flags{$flag})) {
				print STDERR "WARNING: irn_flag '$flag' in opcode $op is unknown\n";
308
			} else {
309
				$temp .= "\tirn_flags_ |= " . $known_irn_flags{$flag} . ";\n";
310
311
312
313
314
315
			}
		}
		$temp .= "\n";
	}

	# lookup init function
316
	my $attr_init_code = "(void)in;(void)irn_flags_;(void)in_reqs;(void)n_res;";
317
318
319
320
321
	if ($attr_type ne "") {
		$attr_init_code = $init_attr{$attr_type};
		if(!defined($attr_init_code)) {
			die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
		}
322
323
324
325
326
327
328
329
330
331
332
	}
	my $custominit = "";
	if(defined($custom_init_attr_func)) {
		$custominit .= &$custom_init_attr_func($n, $on, "${arch}_${op}");
	}
	if(defined($n->{custominit})) {
		$custominit .= $n->{custominit};
	}

	$temp .= <<EOF;
	/* create node */
333
334
335
336
337
338
339
340
341
342
	ir_graph *irg  = get_irn_irg(block);
	ir_op    *op   = op_${arch}_${op};
EOF
	if (defined($known_mode)) {
		$temp .= <<EOF;
	ir_mode  *mode = ${known_mode};
EOF
	}
	$temp .= <<EOF;
	ir_node  *res  = new_ir_node(dbgi, irg, block, op, mode, arity, in);
343
344

	/* init node attributes */
345
346
347
348
349
EOF
	if ($attr_type ne "") {
		$temp .= <<EOF;
	${attr_type} *attr = (${attr_type}*)get_irn_generic_attr(res);
	(void) attr; /* avoid potential warning */
Matthias Braun's avatar
Matthias Braun committed
350
351
352
353
354
355
EOF
	}
	my $fixed = $on->{fixed};
	if (defined($fixed)) {
		$temp .= <<EOF;
	${fixed}
356
357
358
EOF
	}
	$temp .= <<EOF;
359
	${attr_init_code}
360
361
362
EOF
	if ($custominit ne "") {
		$temp .= <<EOF;
363
	${custominit}
364
365
366
367
EOF
	}
	$temp .= <<EOF;
	backend_info_t *info = be_get_info(res);
368
	(void) info; /* avoid potential warning */
369
${set_out_reqs}
370
371
372
373
374
375
376
377
378
EOF

	if (exists($n->{"init_attr"})) {
		$temp .= "\t".$n->{"init_attr"}."\n";
	}

	$temp .= <<EOF;
	/* optimize node */
	res = optimize_node(res);
Matthias Braun's avatar
Matthias Braun committed
379
	verify_new_node(irg, res);
380
381
382
383
384
385
386
387
388
389

	return res;
EOF

	$obst_constructor .= $temp;

	# close constructor function
	$obst_constructor .= "}\n\n";
}

390
391
392
393
394
my @node_attrs = (
	"arity",
	"attr",
	"comment",
	"custominit",
395
	"in_reqs",
396
397
398
399
	"init_attr",
	"ins",
	"irn_flags",
	"mode",
400
	"out_reqs",
401
402
403
	"outs",
);

404
$obst_enum_op .= "typedef enum ${arch}_opcodes {\n";
405
foreach my $op (sort(keys(%nodes))) {
406
	my %n        = %{ $nodes{"$op"} };
407
	my $known_mode;
408
409
	my $num_outs = 0;
	my @out_flags;
Christian Würdig's avatar
Christian Würdig committed
410

411
412
413
414
415
416
417
418
	if (my $template = $n{"template"}) {
		foreach my $key (keys(%$template)) {
			if (!exists $n{$key}) {
				$n{$key} = $template->{$key};
			}
		}
	}

419
420
	# determine arity
	$arity = 0;
421
422
423
424
425
426
	if (my $in_reqs = $n{"in_reqs"}) {
		if ($in_reqs eq "...") {
			$arity = $ARITY_VARIABLE;
		} else {
			$arity = scalar(@$in_reqs);
		}
427
428
429
430
431
	} elsif (exists($n{"ins"})) {
		$arity = scalar(@{ $n{"ins"} });
	}

	# determine out arity
432
433
434
435
436
437
438
	my $out_arity = 0;
	if (my $out_reqs = $n{"out_reqs"}) {
		if ($out_reqs eq "...") {
			$out_arity = $ARITY_VARIABLE;
		} else {
			$out_arity = scalar(@$out_reqs);
		}
439
440
441
	} elsif (exists($n{"outs"})) {
		$out_arity = scalar(@{ $n{"outs"} });
	}
442

Christian Würdig's avatar
Christian Würdig committed
443
444
	$orig_op = $op;
	$op      = $arch."_".$op;
445
	$temp    = "";
Christian Würdig's avatar
Christian Würdig committed
446

447
	# define proj numbers and in numbers
448
449
	if (exists($n{"outs"})) {
		undef my @outs;
450

451
		@outs = @{ $n{"outs"} };
452
		if($out_arity >= 0 && scalar(@outs) != $out_arity) {
453
			die "Fatal error: Op ${op} has different number of outs and out_arity\n";
454
455
		}

Christian Würdig's avatar
Christian Würdig committed
456
		$num_outs = $#outs + 1;
457

458
		if ($num_outs > 0) {
459
			$obst_proj .= "\ntypedef enum pn_$op {\n";
460
461
462
463
464
465
466
467

			for (my $idx = 0; $idx <= $#outs; $idx++) {
				# check, if we have additional flags annotated to out
				if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
					push(@out_flags, $1);
					$outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
				}
				$obst_proj .= "\tpn_${op}_".$outs[$idx]." = ${idx},\n";
468
469
			}

470
			$obst_proj .= "} pn_${op};\n";
471
		}
472
		# outs have names, it must be a mode_T node
473
474
475
		if (!defined($n{mode})) {
			$n{mode} = "mode_T";
		}
476
	}
477
478
479
480
	if (exists($n{"ins"})) {
		undef my @ins;

		@ins = @{ $n{"ins"} };
481
		if($arity >= 0 && scalar(@ins) != $arity) {
482
			die "Fatal error: Op ${op} has different number of ins and arity\n";
483
		}
484

485
		if ($#ins >= 0) {
486
			$obst_proj .= "\ntypedef enum n_$op {\n";
487
488
489
			for (my $idx = 0; $idx <= $#ins; $idx++) {
				$obst_proj .= "\tn_${op}_".$ins[$idx]." = ${idx},\n";
			}
490
			$obst_proj .= "} n_$op;\n";
491
		}
492
493
	}

494
	# Create opcode
495
	$obst_opvar     .= "ir_op *op_$op = NULL;\n";
Christian Würdig's avatar
Christian Würdig committed
496

497
498
	$obst_header .= <<EOF;
extern ir_op *op_${op};
499
500
501
502
503
504

static inline bool is_$op(ir_node const *const n)
{
	return get_irn_op(n) == op_$op;
}

505
EOF
Christian Würdig's avatar
Christian Würdig committed
506

507
	my $attr_type= $n{attr_type};
508
509
	if(!defined($attr_type)) {
		$attr_type = $default_attr_type;
510
		$n{attr_type} = $attr_type;
511
512
	}

513
514
515
516
517
518
	# determine hash function
	my $hash_func;
	if (exists($n{"hash_func"})) {
		$hash_func = $n{"hash_func"};
	}

519
	# determine compare function
520
521
522
	my $attrs_equal_func;
	if (exists($n{"attrs_equal"})) {
		$attrs_equal_func = $n{"attrs_equal"};
523
	} elsif ($attr_type eq "") {
524
		# do nothing
525
	} else {
526
527
528
		$attrs_equal_func = $attr_type;
		$attrs_equal_func =~ s/_t$//;
		$attrs_equal_func .= "s_equal";
Christian Würdig's avatar
Christian Würdig committed
529
530
	}

531
532
533
	my %constructors;
	if (exists($n{constructors})) {
		%constructors = %{ $n{constructors} };
534
	} else {
535
536
		# Create 1 default constructor
		my %constructor = ();
537
		foreach my $a (@node_attrs) {
538
539
			if (defined($n{$a})) {
				$constructor{$a} = $n{$a};
Christian Würdig's avatar
Christian Würdig committed
540
541
			}
		}
542
543
		%constructors = ( "" => \%constructor );
	}
544

545
	foreach my $constr (sort(keys(%constructors))) {
546
547
		my %cstr = %{ $constructors{$constr} };
		# Copy some values from outer node if they don't exists in the constr
548
		foreach my $a (@node_attrs) {
549
550
			if (!defined($cstr{$a}) && defined($n{$a})) {
				$cstr{$a} = $n{$a};
551
			}
Christian Würdig's avatar
Christian Würdig committed
552
		}
553
554
		create_constructor($orig_op, $constr, \%cstr, \%n);
	}
Christian Würdig's avatar
Christian Würdig committed
555
556

	# set default values for state and flags if not given
557
558
559
560
	$n{"state"}     = "floats" if (! exists($n{"state"}));
	$n{"op_flags"}  = ["none"] if (! exists($n{"op_flags"}));
	$n{"dump_func"} = "${arch}_dump_node" if (!exists($n{"dump_func"}));
	my $dump_func = $n{"dump_func"};
Christian Würdig's avatar
Christian Würdig committed
561

562
	my %known_flags = map { $_ => 1 } (
Matthias Braun's avatar
Matthias Braun committed
563
564
565
		"none", "commutative", "cfopcode", "unknown_jump", "fragile", "forking",
		"constlike", "keep", "start_block", "uses_memory", "dump_noblock",
		"cse_neutral"
566
	);
Matthias Braun's avatar
Matthias Braun committed
567
	my $is_fragile = 0;
568
569
570
571
	foreach my $flag (@{$n{"op_flags"}}) {
		if (not defined($known_flags{$flag})) {
			print STDERR "WARNING: Flag '$flag' in opcode $op is unknown\n";
		}
Matthias Braun's avatar
Matthias Braun committed
572
573
574
		if ($flag eq "fragile") {
			$is_fragile = 1;
		}
575
576
577
578
	}
	my @mapped = map { "irop_flag_$_" } @{$n{"op_flags"}};
	my $op_flags = join('|', @mapped);

579
580
581
582
583
	my $attr_size = "0";
	if ($attr_type ne "") {
		$attr_size = "sizeof(${attr_type})"
	}

584
	$n_opcodes++;
Matthias Braun's avatar
Matthias Braun committed
585
	$temp  = "\top = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", $op_flags";
586
	$temp .= ", ".translate_arity($arity).", -1, ${attr_size});\n";
587
	$obst_new_irop .= $temp;
Matthias Braun's avatar
Matthias Braun committed
588
	$obst_new_irop .= "\tset_op_dump(op, ${dump_func});\n";
589
590
	if (defined($attrs_equal_func)) {
		$obst_new_irop .= "\tset_op_attrs_equal(op, ${attrs_equal_func});\n";
591
592
593
	}
	my $copy_attr_func = $copy_attr{$attr_type};
	if (!defined($copy_attr_func)) {
594
595
		# don't set a copy_attr function if the node has no additional attributes.
		if ($attr_type ne "") {
596
597
598
599
			$copy_attr_func = $default_copy_attr;
		}
	}
	if (defined($copy_attr_func)) {
Matthias Braun's avatar
Matthias Braun committed
600
		$obst_new_irop .= "\tset_op_copy_attr(op, ${copy_attr_func});\n";
601
602
	}
	if (defined($hash_func)) {
Matthias Braun's avatar
Matthias Braun committed
603
		$obst_new_irop .= "\tset_op_hash(op, ${hash_func});\n";
604
605
	}

Matthias Braun's avatar
Matthias Braun committed
606
	if ($is_fragile) {
607
608
		$obst_new_irop .= "\tir_op_set_memory_index(op, n_${op}_mem);\n";
		$obst_new_irop .= "\tir_op_set_fragile_indices(op, pn_${op}_X_regular, pn_${op}_X_except);\n";
Matthias Braun's avatar
Matthias Braun committed
609
	}
610
	$obst_new_irop .= "\tset_op_tag(op, $arch\_op_tag);\n";
Matthias Braun's avatar
Matthias Braun committed
611
	if(defined($n{op_attr_init})) {
612
		$obst_new_irop .= "\t".$n{op_attr_init}."\n";
613
	}
614
	$obst_new_irop .= "\top_${op} = op;\n";
615

616
	$obst_free_irop .= "\tfree_ir_op(op_$op); op_$op = NULL;\n";
Matthias Braun's avatar
Matthias Braun committed
617

618
	$obst_enum_op .= "\tiro_$op,\n";
Christian Würdig's avatar
Christian Würdig committed
619

620
	$obst_header .= "\n";
621
}
622
623
$obst_enum_op .= "\tiro_$arch\_last\n";
$obst_enum_op .= "} $arch\_opcodes;\n\n";
624
625
626

# emit the code

627
open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
628

629
print OUT<<EOF;
630
631
#include "gen_$arch\_regalloc_if.h"
#include "fourcc.h"
632
#include "irgopt.h"
633

634
$obst_attrs_equal
635
$obst_opvar
636

637
static int $arch\_opcode_start = -1;
638

639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
EOF

# build the FOURCC arguments from $arch

my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');

if (length($arch) >= 1) {
	$a = uc(substr($arch, 0, 1));
}

if (length($arch) >= 2) {
	$b = uc(substr($arch, 1, 1));
}

if (length($arch) >= 3) {
	$c = uc(substr($arch, 2, 1));
}

if (length($arch) >= 4) {
	$d = uc(substr($arch, 3, 1));
}

661
print OUT <<END;
662

663
/** A tag for the $arch opcodes. */
664
#define $arch\_op_tag FOURCC('$a', '$b', '$c', '$d')
Michael Beck's avatar
Michael Beck committed
665
666

/** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
667
668
int is_$arch\_op(const ir_op *op)
{
669
	return get_op_tag(op) == $arch\_op_tag;
Michael Beck's avatar
Michael Beck committed
670
671
}

672
/** Return 1 if the given node is a $arch machine node, 0 otherwise */
673
674
int is_$arch\_irn(const ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
675
	return is_$arch\_op(get_irn_op(node));
676
677
}

678
679
680
681
int get_$arch\_irn_opcode(const ir_node *node)
{
	assert(is_$arch\_irn(node));
	return get_irn_opcode(node) - $arch\_opcode_start;
682
683
}

684
685
686
#ifdef BIT
#undef BIT
#endif
687
#define BIT(x)  (1 << (x))
688

689
690
$obst_limit_func
$obst_reg_reqs
691
692
$obst_constructor

693
/**
694
 * Creates the $arch specific Firm machine operations
695
696
 * needed for the assembler irgs.
 */
697
void $arch\_create_opcodes(void)
Matthias Braun's avatar
Matthias Braun committed
698
{
699
700
	ir_op *op;
	int    cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
701

702
	$arch\_opcode_start = cur_opcode;
703
$obst_new_irop
Matthias Braun's avatar
Matthias Braun committed
704
705
706
707
}

void $arch\_free_opcodes(void)
{
708
$obst_free_irop
Matthias Braun's avatar
Matthias Braun committed
709
}
710
END
711
712
713

close(OUT);

714
open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
715

716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
my $creation_time = localtime(time());
my $tmp = uc($arch);

print OUT<<EOF;
/**
 * \@file
 * \@brief Function prototypes for the new opcode functions.
 * \@note  DO NOT EDIT THIS FILE, your changes will be lost.
 *        Edit $specfile instead.
 *        created by: $0 $specfile $target_dir
 * \@date  $creation_time
 */
#ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
#define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H

731
$obst_enum_op
732
int is_${arch}_irn(const ir_node *node);
733
int is_${arch}_op(const ir_op *op);
734
735
736
737
738

int get_${arch}_irn_opcode(const ir_node *node);
${obst_header}
${obst_proj}

739
740
#endif
EOF
741
742

close(OUT);
743
744
745
746
747

###
# Translates numeric arity into string constant.
###
sub translate_arity {
Christian Würdig's avatar
Christian Würdig committed
748
749
750
	my $arity = shift;

	if ($arity =~ /^\d+$/) {
751
		return "oparity_any";
752
753
754
	} elsif ($arity == $ARITY_VARIABLE) {
		return "oparity_variable";
	} else {
755
		die "Fatal error: Unknown arity $arity";
Christian Würdig's avatar
Christian Würdig committed
756
	}
757
}
758

759
sub mangle_requirements {
760
761
	my $reqs  = shift;
	my $class = shift;
762
	my $flags = shift;
763
764
765
766
767
768
769
770

	my @alternatives = split(/ /, $reqs);
	for(my $idx = 0; $idx < scalar(@alternatives); $idx++) {
		$alternatives[$idx] =~ s/!/not_/g;
	}

	@alternatives = sort @alternatives;

771
	my $name = $class."_".join('_', @alternatives);
772
773
774
775
	if (defined($flags)) {
		$flags =~ s/\|/_/g;
		$name .= "_$flags";
	}
776
777
778
779
780
781
782
783
784
785
786
787
788
789

	return $name;
}

###
# Determines whether $name is a specified register class or not.
# @return 1 if name is register class, 0 otherwise
###
sub is_reg_class {
    my $name = shift;
    return 1 if exists($reg_classes{"$name"});
    return 0;
}

790
791
792
793
794
795
sub is_ambiguous {
	my $name = shift;
	return 1 if exists($reg_classes{"$name"}) && exists($reg2class{"$name"});
	return 0;
}

796
797
798
799
800
801
802
803
804
805
806
807
###
# Returns the register class for a given register.
# @return class or undef
###
sub get_reg_class {
    my $reg = shift;
    $reg = substr($reg, 1) if ($reg =~ /!.*/);
    return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
    return undef;
}

###
808
# Returns the index of a given register within its register class.
809
810
811
812
813
814
815
816
817
818
819
820
821
# @return index or undef
###
sub get_reg_index {
    my $reg = shift;
    return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
    return undef;
}

###
# Remember the register class for each index in the given requirements.
# We need this information for requirements like "in_sX" or "out_dX"
# @return array of classes corresponding to the requirement for each index
###
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
sub get_in_req_class {
	my $n   = shift;
	my $idx = shift;

	my $reqs = $n->{"in_reqs"};
	if ($reqs && $reqs ne "..." && $idx < scalar(@$reqs)) {
		my ($req,) = split(/:/, @$reqs[$idx]);
		if ($req eq "none") {
			return "none";
		} elsif (is_reg_class($req)) {
			return $req;
		} else {
			foreach my $reg (split(/ /, $req)) {
				my $class = get_reg_class($reg);
				if ($class) {
					return $class;
				}
			}
		}
	}
842

843
	return undef;
844
845
846
847
848
849
850
851
852
853
854
855
856
}

###
# Generates the function for a given $op and a given IN-index
# which returns a subset of possible register from a register class
# @return classname from which the subset is derived or undef and
#         pos which corresponds to in/out reference position or undef
###
sub build_subset_class_func {
	my $neg           = undef;
	my $class         = undef;
	my $has_limit     = 0;
	my $limit_name;
857
858
	my $same_pos      = 0;
	my $different_pos = 0;
859
860
861
862
863
864
865
866
867
868
869
870
871
	my $temp;
	my @obst_init;
	my @obst_limits;
	my @obst_ignore;
	my @limit_array;
	my $limit_reqs;   #used for name mangling

	# build function header
	my $node  = shift;
	my $op    = shift;
	my $idx   = shift;
	my $is_in = shift;
	my @regs  = split(/ /, shift);
872
	my $flags = shift;
873
874
875

	# set/unset registers
CHECK_REQS: foreach (@regs) {
876
		if (!$is_in && /(!)?in_r(\d+)/) {
877
878
879
880
881
882
883
884
			my $bit_pos = 1 << ($2 - 1);
			if ($different_pos & $bit_pos) {
				if ($1) {
					print STDERR "duplicate !in constraint\n";
				} else {
					print STDERR "conflicting !in and in constraints\n";
				}
				return (undef, undef, undef, undef);
885
886
			}

887
888
889
			if ($same_pos & $bit_pos) {
				if ($1) {
					print STDERR "conflicting !in and in constraints\n";
890
				} else {
891
					print STDERR "duplicate in constraint\n";
892
				}
893
894
895
896
897
898
899
				return (undef, undef, undef, undef);
			}

			if ($1) {
				$different_pos |= $bit_pos;
			} else {
				$same_pos      |= $bit_pos;
900
901
			}

902
903
904
905
906
			my $idx = $2;
			$class = get_in_req_class($node, $idx - 1);
			if (!$class) {
				die("Fatal error: Could not get in_reqs register class for '$op' index $idx ... exiting.\n");
			}
907
908
909
910
911
912
913
914
915
			next CHECK_REQS;
		}

		# check for negate
		if (substr($_, 0, 1) eq "!") {
			if (defined($neg) && $neg == 0) {
				# we have seen a positiv constraint as first one but this one is negative
				# this doesn't make sense
				print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
916
				return (undef, undef, undef, undef);
917
918
919
920
921
922
923
924
925
926
927
928
929
			}

			if (!defined($neg)) {
				$has_limit = 1;
			}

			$_   = substr($_, 1); # skip '!'
			$neg = 1;
		} else {
			if (defined($neg) && $neg == 1) {
				# we have seen a negative constraint as first one but this one is positive
				# this doesn't make sense
				print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
930
				return (undef, undef, undef, undef);
931
932
933
934
935
936
937
938
939
940
			}

			$has_limit = 1;
			$neg = 0;
		}

		# check if register belongs to one of the given classes
		$temp = get_reg_class($_);
		if (!defined($temp)) {
			print STDERR "Unknown register '$_'!\n";
941
			return (undef, undef, undef, undef);
942
943
944
945
946
947
948
949
		}

		# set class
		if (!defined($class)) {
			$class = $temp;
		} elsif ($class ne $temp) {
			# all registers must belong to the same class
			print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
950
			return (undef, undef, undef, undef);
951
952
953
954
955
956
957
958
959
960
961
		}

		# calculate position inside the initializer bitfield (only 32 bits per
		# element)
		my $regidx = get_reg_index($_);
		my $arrayp = $regidx / 32;
		push(@{$limit_array[$arrayp]}, $_);
		$limit_reqs .= "$_ ";
	}

	if ($has_limit == 1) {
962
		$limit_name = "${arch}_limit_".mangle_requirements($limit_reqs, $class);
963
964

		if(defined($limit_bitsets{$limit_name})) {
965
			$limit_name = $limit_bitsets{$limit_name};
966
			return ($class, $limit_name, $same_pos, $different_pos);
967
968
969
970
		}

		$limit_bitsets{$limit_name} = $limit_name;

971
		$obst_limit_func .= "static const unsigned $limit_name\[] = { ";
972
973
		my $first = 1;
		my $limitbitsetlen = $regclass2len{$class};
974
		my $limitarraylen = ($limitbitsetlen+31) / 32;
975
976
977
978
979
980
		for(my $i = 0; $i < $limitarraylen; $i++) {

			my $limitarraypart = $limit_array[$i];
			if($first) {
				$first = 0;
			} else {
981
				$obst_limit_func .= ", ";
982
983
984
985
986
987
988
989
990
991
992
			}
			my $temp;
			if($neg) {
				$temp = "0xFFFFFFFF";
			}
			foreach my $reg (@{$limitarraypart}) {
				if($neg) {
					$temp .= " & ~";
				} elsif(defined($temp)) {
					$temp .= " | ";
				}
993
994
995
996
				my $firstreg = uc($reg_classes{$class}[0]->{"name"});
				my $classuc = uc($class);
				my $reguc = uc($reg);
				$temp .= "BIT(REG_${classuc}_${reguc})";
997
			}
998
			$obst_limit_func .= $temp || "0";
999
		}
1000
		$obst_limit_func .= " };\n";
1001
1002
	}

1003
	return ($class, $limit_name, $same_pos, $different_pos);
1004
1005
1006
1007
1008
1009
}

###
# Generate register requirements structure
###
sub generate_requirements {
1010
	my ($reqs, $flags) = split(/:/, shift);
1011
1012
1013
1014
	my $node  = shift;
	my $op    = shift;
	my $idx   = shift;
	my $is_in = shift;
1015
	my $width = 1;
1016
	my $result;
1017

1018
1019
1020
1021
1022
1023
1024
	my @req_type_mask;
	if (defined($flags)) {
		foreach my $f (split(/|/, $flags)) {
			if ($f eq "I") {
				push(@req_type_mask, "arch_register_req_type_ignore");
			} elsif ($f eq "S") {
				push(@req_type_mask, "arch_register_req_type_produces_sp");
1025
1026
1027
1028
			} elsif ($f eq "a") {
				push(@req_type_mask, "arch_register_req_type_aligned");
			} elsif ($f eq "2" or $f eq "4" or $f eq "8") {
				$width = int($f);
1029
1030
1031
1032
			}
		}
	}

1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
	my $is_cls = 0;
	my $is_reg = 0;
	if ($reqs =~ /reg-(.*)/) {
		$reqs = $1;
		$is_reg = 1;
	} elsif ($reqs =~ /cls-(.*)/) {
		$reqs = $1;
		$is_cls = 1;
	}
	if (is_ambiguous($reqs) && !$is_reg && !$is_cls) {
		die("Fatal error: $reqs is ambiguous (try reg-$reqs or cls-$reqs) at node $op")
	}

1046
	my $class;
1047
	if ($reqs eq "none") {
1048
		return "arch_no_requirement";
1049
	} elsif (is_reg_class($reqs) && !$is_reg) {
1050
		my $reqtype = join(" | ", @req_type_mask) || "arch_register_req_type_none";
1051
1052
1053
		$class  = $reqs;
		$result = <<EOF;
{
1054
1055
1056
1057
1058
1059
	.cls             = &${arch}_reg_classes[CLASS_${arch}_${class}],
	.limited         = NULL,
	.type            = ${reqtype},
	.other_same      = 0,
	.other_different = 0,
	.width           = $width,
1060
1061
1062
1063
1064
};

EOF

	} else {
1065
		my ($regclass, $limit_bitset, $same_pos, $different_pos)
1066
			= build_subset_class_func($node, $op, $idx, $is_in, $reqs, $flags);
1067

1068
		if (!defined($regclass)) {
1069
1070
1071
1072
1073
1074
			die("Fatal error: Could not build subset for requirements '$reqs' of '$op' pos $idx ... exiting.\n");
		}

		if (defined($limit_bitset) && $limit_bitset ne "NULL") {
			push(@req_type_mask, "arch_register_req_type_limited");
		}
1075
		if ($same_pos != 0) {
1076
1077
			push(@req_type_mask, "arch_register_req_type_should_be_same");
		}
1078
		if ($different_pos != 0) {
1079
			push(@req_type_mask, "arch_register_req_type_must_be_different");
1080
		}
1081
		my $reqtype = join(" | ", @req_type_mask);
1082
1083
1084
1085
1086

 		if(!defined($limit_bitset)) {
			$limit_bitset = "NULL";
		}

1087
1088
1089
		$class  = $regclass;
		$result = <<EOF;
{
1090
1091
1092
1093
1094
1095
	.cls             = &${arch}_reg_classes[CLASS_${arch}_${class}],
	.limited         = ${limit_bitset},
	.type            = ${reqtype},
	.other_same      = ${same_pos},
	.other_different = ${different_pos},
	.width           = $width,
1096
1097
1098
1099
1100
};

EOF
	}

1101
	my $name = "${arch}_requirements_".mangle_requirements($reqs, $class, $flags);
1102
1103
1104
1105
	if(defined($requirements{$name})) {
		return $name;
	}
	$requirements{$name} = $name;
1106
	$obst_reg_reqs .= "static const arch_register_req_t ${name} = ${result}\n";
1107

1108
1109
	return $name;
}