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

Christian Würdig's avatar
Christian Würdig committed
3
#
Michael Beck's avatar
Michael Beck committed
4
# Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#
# This file is part of libFirm.
#
# This file may be distributed and/or modified under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation and appearing in the file LICENSE.GPL included in the
# packaging of this file.
#
# Licensees holding valid libFirm Professional Edition licenses may use
# this file in accordance with the libFirm Commercial License.
# Agreement provided with the Software.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE.
#

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# This script generates the C code which creates the irop's and
# their coresponding node constructors for all operations in a given spec
# 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];
my $state      = 1;
my $cur_op     = "";
my $line_nr    = 0;

our $arch;
37
our $additional_opcodes;
38
our %nodes;
39
our %operands;
40
our %cpu;
41
our $default_op_attr_type;
42
our $default_attr_type;
43
our $default_cmp_attr;
44
our $default_copy_attr;
45
our %init_attr;
46
our $custom_init_attr_func;
47
our %compare_attr;
48
our %copy_attr;
49
our %reg_classes;
50
our %custom_irn_flags;
51
52
53
54
55
56
57

# include spec file

my $return;

no strict "subs";
unless ($return = do $specfile) {
58
59
60
	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;
61
62
63
}
use strict "subs";

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

67
68
69
if(!defined($default_attr_type)) {
	$default_attr_type = "${arch}_attr_t";
}
70
71
if(!defined(%init_attr)) {
	%init_attr = (
72
		"$default_attr_type" => "\tinit_${arch}_attributes(res, irn_flags_, in_reqs, n_res);",
73
74
	);
}
75
76
77
78
79
80
81
82
if(!defined($default_cmp_attr)) {
	$default_cmp_attr = "${arch}_compare_attr";
}
if(!defined(%compare_attr)) {
	%compare_attr = (
		"${default_attr_type}" => "${default_cmp_attr}",
	);
}
83

Matthias Braun's avatar
Matthias Braun committed
84
85
# Operands are really just nodes with some special constraints, we check
# these and create new entries in the nodes hashmap
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
foreach my $op (keys(%operands)) {
	my %operand = %{ $operands{"$op"} };
	my %op_node;

	# constraints
	if(defined($operand{op_flags})) { die "Fatal error: operands can't have op_flags ($op)"; }
	if(defined($operand{cmp_attr})) { die "Fatal error: cmp_attr not allowed for operands ($op)"; }
	if(defined($operand{mode})) { die "Operand must not have a mode defined ($op)"; }
	if(defined($operand{out_arity})) { die "operand must not have out_arity defined ($op)"; }
	if(defined($nodes{$op})) { die "$op defined as operand and as node"; };


	foreach my $flag (keys(%operand)) {
		$op_node{$flag} = $operand{$flag};
	}
	$op_node{op_flags} = "O";
	$op_node{cmp_attr} = 'return 1;';
Michael Beck's avatar
Michael Beck committed
103
	$op_node{mode}     = 'mode_ANY';
104
105
106
107

	$nodes{$op} = \%op_node;
}

108
#print Dumper(%nodes);
109
#print Dumper(%operands);
110
111
112

# create c code file from specs

113
114
my @obst_limit_func;
my @obst_reg_reqs;
115
116
my @obst_opvar;       # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
my @obst_get_opvar;   # stack for the get_op_<arch>_<op-name>() functions
117
my $obst_constructor; # stack for node constructor functions
118
my @obst_new_irop;    # stack for the new_ir_op calls
119
my @obst_enum_op;     # stack for creating the <arch>_opcode enum
120
my $obst_header;      # stack for function prototypes
121
my @obst_is_archirn;  # stack for the is_$arch_irn() function
Christian Würdig's avatar
Christian Würdig committed
122
my @obst_cmp_attr;    # stack for the compare attribute functions
123
my $obst_proj = "";   # stack for the pn_ numbers
124
125
my $orig_op;
my $arity;
Christian Würdig's avatar
Christian Würdig committed
126
my $cmp_attr_func;
127
my $temp;
128
my $n_opcodes = 0;    # number of opcodes
129
130
my $ARITY_VARIABLE = -1;
my $ARITY_DYNAMIC  = -2;
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
my %requirements = ();
my %limit_bitsets = ();
my %reg2class = ();
my %regclass2len = ();

# build register->class hashes
foreach my $class_name (keys(%reg_classes)) {
	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;
}

157

158
159
160
# for registering additional opcodes
$n_opcodes += $additional_opcodes if (defined($additional_opcodes));

161
$obst_header .= "void ${arch}_create_opcodes(const arch_irn_ops_t *be_ops);\n";
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
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};
	}

	# determine arity
	my $arity = 0;
	if(exists($n->{"arity"})) {
		$arity = $n->{"arity"};
	} elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"in"})) {
		$arity = scalar(@{ $n->{"reg_req"}{"in"} });
	} elsif (exists($n->{"ins"})) {
		$arity = scalar(@{ $n->{"ins"} });
	}
	if($arity eq "variable") {
		$arity = $ARITY_VARIABLE;
	} elsif($arity eq "dynamic") {
		$arity = $ARITY_DYNAMIC;
	}

	# determine out arity
	my $out_arity = 0;
	if(exists($n->{"out_arity"})) {
		$out_arity = $n->{"out_arity"};
	} elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"out"})) {
		$out_arity = scalar(@{ $n->{"reg_req"}{"out"} });
	} elsif (exists($n->{"outs"})) {
		$out_arity = scalar(@{ $n->{"outs"} });
	}
	if($out_arity eq "variable") {
		$out_arity = $ARITY_VARIABLE;
	} elsif($out_arity eq "dynamic") {
		$out_arity = $ARITY_DYNAMIC;
	}
209
210
211
	if ($out_arity != 0 && $out_arity != 1 && !defined($known_mode)) {
		$known_mode = "mode_T";
	}
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

	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";
	if (!exists($n->{"args"})) { # default args
		if ($arity == $ARITY_VARIABLE) {
			$complete_args = ", int arity, ir_node *in[]";
		} elsif ($arity == $ARITY_DYNAMIC) {
			$complete_args = "";
		} else {
			for (my $i = 0; $i < $arity; $i++) {
				my $opname = "op${i}";
				if (exists($n->{"ins"})) {
					my @ins = @{ $n->{"ins"} };
					$opname = $ins[$i];
				}

				$complete_args .= ", ir_node *${opname}";
			}
		}
		if ($out_arity == $ARITY_VARIABLE) {
			$complete_args .= ", int n_res";
		}

		if (!defined($known_mode)) {
			$complete_args .= ", ir_mode *mode";
		}
	} else { # user defined args
		for my $href (@{ $n->{"args"} }) {
			$href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
			$complete_args  .= ", ".$href->{"type"}.$href->{"name"};
		}
	}

	# 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;
272
273
274
	ir_graph         *irg        = get_irn_irg(block);
	ir_op            *op         = op_${arch}_${op};
	arch_irn_flags_t  irn_flags_ = arch_irn_flags_none;
275
	ir_node          *res;
276
	backend_info_t   *info;
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
EOF

	if($arity == $ARITY_DYNAMIC) {
		$temp .= <<EOF;
	int             arity   = -1;
	ir_node       **in      = NULL;
EOF
	} elsif($arity == $ARITY_VARIABLE) {
	} else {
		$temp .= <<EOF;
	int             arity   = $arity;
EOF
		if($arity > 0) {
			$temp .= <<EOF;
	ir_node        *in[$arity];
EOF
		} else {
			$temp .= <<EOF;
	ir_node       **in      = NULL;
EOF
		}
	}
	if($out_arity == $ARITY_DYNAMIC) {
		$temp .= <<EOF;
	int             n_res   = -1;
EOF
	} elsif($out_arity == $ARITY_VARIABLE) {
	} else {
		$temp .= <<EOF;
	int             n_res   = ${out_arity};
EOF
	}

	if (defined($known_mode)) {
		$temp .= <<EOF;
	ir_mode        *mode    = ${known_mode};
EOF
	}

	undef my $in_req_var;
	undef my $out_req_var;

	my $set_out_reqs = "";

	# set up static variables for requirements and registers
	if (exists($n->{"reg_req"})) {
		my %req = %{ $n->{"reg_req"} };
		my $idx;

		undef my @in;
		@in = @{ $req{"in"} } if (exists($req{"in"}));
		undef my @out;
		@out = @{ $req{"out"} } if exists(($req{"out"}));

		for(my $idx = 0; $idx < $#in; $idx++) {
			my $req = $in[$idx];
			generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
		}
		for(my $idx = 0; $idx < $#out; $idx++) {
			my $req = $out[$idx];
			generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
		}

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

			$temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
			$temp .= "\t{\n";
			for ($idx = 0; $idx <= $#in; $idx++) {
				my $req = $in[$idx];
				my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
				$temp .= "\t\t& ${reqstruct},\n";
			}
			$temp .= "\t};\n";
		} else {
			if($arity > 0) {
				die "Fatal error: need in requirements for ${op}\n";
			}
			$temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
		}

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

			for ($idx = 0; $idx <= $#out; $idx++) {
				my $req = $out[$idx];
				my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
				$set_out_reqs .= <<EOF;
369
	info->out_infos[${idx}].req = &${reqstruct};
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
EOF
			}
		} else {
			if($out_arity > 0) {
				die "Fatal error: need out requirements for ${op}\n";
			}
		}
	} else {
		$temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
	}
	my $attr_type = $on->{attr_type};
	if(exists($n->{"init_attr"})) {
		$temp .= "\t${attr_type} *attr;\n";
	}

	$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";
404
405
406
407
408
409
		my %known_irn_flags = (
			"none"             => "arch_irn_flags_none",
			"dont_spill"       => "arch_irn_flags_dont_spill",
			"rematerializable" => "arch_irn_flags_rematerializable",
			"modify_flags"     => "arch_irn_flags_modify_flags",
			"simple_jump"      => "arch_irn_flags_simple_jump",
410
			"not_scheduled"    => "arch_irn_flags_not_scheduled",
411
		);
412
413
414
		if (defined(%custom_irn_flags)) {
			%known_irn_flags = (%known_irn_flags, %custom_irn_flags);
		}
415
416
417
		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";
418
			} else {
419
				$temp .= "\tirn_flags_ |= " . $known_irn_flags{$flag} . ";\n";
420
421
422
423
424
425
			}
		}
		$temp .= "\n";
	}

	# lookup init function
426
	my $attr_init_code = "(void)in;(void)irn_flags_;(void)in_reqs;(void)n_res;";
427
428
429
430
431
	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}'";
		}
432
433
434
435
436
437
438
439
440
441
442
443
	}
	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 */
	assert(op != NULL);
444
	res = new_ir_node(dbgi, irg, block, op, mode, arity, in);
445
446
447
448
449

	/* init node attributes */
	${attr_init_code}
	${custominit}
	info = be_get_info(res);
450
	(void) info; /* avoid potential warning */
451
${set_out_reqs}
452
453
454
455

EOF

	if (exists($n->{"init_attr"})) {
456
		$temp .= "\tattr = (${attr_type}*)get_irn_generic_attr(res);\n";
457
		$temp .= "\t(void) attr; /* avoid potential warning */\n";
458
459
460
461
462
463
		$temp .= "\t".$n->{"init_attr"}."\n";
	}

	$temp .= <<EOF;
	/* optimize node */
	res = optimize_node(res);
464
	irn_verify_irg(res, irg);
465
466
467
468
469
470
471
472
473
474

	return res;
EOF

	$obst_constructor .= $temp;

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

475
push(@obst_enum_op, "typedef enum ${arch}_opcodes {\n");
476
foreach my $op (keys(%nodes)) {
477
	my %n        = %{ $nodes{"$op"} };
478
	my $known_mode;
479
	my $num_outs = 0;
480
	my $out_arity;
481
	my @out_flags;
Christian Würdig's avatar
Christian Würdig committed
482

483
484
485
486
487
	# determine arity
	$arity = 0;
	if(exists($n{"arity"})) {
		$arity = $n{"arity"};
	} elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
488
		$arity = scalar(@{ $n{"reg_req"}{"in"} });
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
	} elsif (exists($n{"ins"})) {
		$arity = scalar(@{ $n{"ins"} });
	}
	if($arity eq "variable") {
		$arity = $ARITY_VARIABLE;
	} elsif($arity eq "dynamic") {
		$arity = $ARITY_DYNAMIC;
	}

	# determine out arity
	$out_arity = 0;
	if(exists($n{"out_arity"})) {
		$out_arity = $n{"out_arity"};
	} elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
		$out_arity = scalar(@{ $n{"reg_req"}{"out"} });
	} elsif (exists($n{"outs"})) {
		$out_arity = scalar(@{ $n{"outs"} });
	}
	if($out_arity eq "variable") {
		$out_arity = $ARITY_VARIABLE;
	} elsif($out_arity eq "dynamic") {
		$out_arity = $ARITY_DYNAMIC;
511
512
	}

Christian Würdig's avatar
Christian Würdig committed
513
514
	$orig_op = $op;
	$op      = $arch."_".$op;
515
	$temp    = "";
Christian Würdig's avatar
Christian Würdig committed
516

517
	# define proj numbers and in numbers
518
519
	if (exists($n{"outs"})) {
		undef my @outs;
520

521
		@outs = @{ $n{"outs"} };
522
		if($out_arity >= 0 && scalar(@outs) != $out_arity) {
523
			die "Fatal error: Op ${op} has different number of outs and out_arity\n";
524
525
		}

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

528
529
530
531
532
533
534
535
536
537
		if ($num_outs > 0) {
			$obst_proj .= "\nenum pn_${op} {\n";

			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";
538
539
			}

540
541
			$obst_proj .= "};\n";
		}
542
		# outs have names, it must be a mode_T node
543
544
545
		if (!defined($n{mode})) {
			$n{mode} = "mode_T";
		}
546
	}
547
548
549
550
	if (exists($n{"ins"})) {
		undef my @ins;

		@ins = @{ $n{"ins"} };
551
		if($arity >= 0 && scalar(@ins) != $arity) {
552
			die "Fatal error: Op ${op} has different number of ins and arity\n";
553
		}
554

555
556
557
558
559
560
		if ($#ins >= 0) {
			$obst_proj .= "\nenum n_$op {\n";
			for (my $idx = 0; $idx <= $#ins; $idx++) {
				$obst_proj .= "\tn_${op}_".$ins[$idx]." = ${idx},\n";
			}
			$obst_proj .= "};\n";
561
		}
562
563
	}

564
	# Create opcode
Christian Würdig's avatar
Christian Würdig committed
565
	push(@obst_opvar, "ir_op *op_$op = NULL;\n");
566
	push(@obst_get_opvar, "ir_op *get_op_$op(void)         { return op_$op; }\n");
567
	push(@obst_get_opvar, "int    is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
Christian Würdig's avatar
Christian Würdig committed
568
569
570

	push(@obst_is_archirn, "is_$op(node)");

571
572
573
574
575
	$obst_header .= <<EOF;
extern ir_op *op_${op};
ir_op *get_op_${op}(void);
int is_${op}(const ir_node *n);
EOF
Christian Würdig's avatar
Christian Würdig committed
576

577
	my $attr_type= $n{attr_type};
578
579
	if(!defined($attr_type)) {
		$attr_type = $default_attr_type;
580
		$n{attr_type} = $attr_type;
581
582
	}

583
584
585
586
587
588
	# determine hash function
	my $hash_func;
	if (exists($n{"hash_func"})) {
		$hash_func = $n{"hash_func"};
	}

589
	# determine compare function
Matthias Braun's avatar
Matthias Braun committed
590
591
592
593
	my $cmp_attr_func;
	if (exists($n{"cmp_attr"})) {
		my $cmpcode = $n{"cmp_attr"};

594
		push(@obst_cmp_attr, "static int cmp_attr_$op(const ir_node *a, const ir_node *b) {\n");
Matthias Braun's avatar
Matthias Braun committed
595
		if($cmpcode =~ m/attr_a/) {
596
			push(@obst_cmp_attr, "\tconst ${attr_type} *attr_a = get_irn_generic_attr_const(a);\n");
597
598
		} else {
			push(@obst_cmp_attr, "\t(void) a;\n");
Matthias Braun's avatar
Matthias Braun committed
599
600
		}
		if($cmpcode =~ m/attr_b/) {
601
			push(@obst_cmp_attr, "\tconst ${attr_type} *attr_b = get_irn_generic_attr_const(b);\n");
602
603
		} else {
			push(@obst_cmp_attr, "\t(void) b;\n");
604
		}
Matthias Braun's avatar
Matthias Braun committed
605
		push(@obst_cmp_attr, "\t${cmpcode}\n");
Christian Würdig's avatar
Christian Würdig committed
606
607
		push(@obst_cmp_attr, "}\n\n");

Matthias Braun's avatar
Matthias Braun committed
608
		$cmp_attr_func = "cmp_attr_${op}";
609
610
	} elsif ($attr_type eq "") {
		$cmp_attr_func = "NULL";
611
612
613
614
	} else {
		if(defined($compare_attr{${attr_type}})) {
			$cmp_attr_func = $compare_attr{${attr_type}};
		} else {
615
			die "Fatal error: No compare function defined for ${attr_type} attributes.";
616
		}
Christian Würdig's avatar
Christian Würdig committed
617
618
	}

619
620
621
	my %constructors;
	if (exists($n{constructors})) {
		%constructors = %{ $n{constructors} };
622
	} else {
623
624
625
626
627
628
629
		# Create 1 default constructor
		my %constructor = ();
		foreach my $a ("comment", "ins", "outs", "args", "attr", "units",
				"reg_req", "init_attr", "irn_flags", "mode", "arity",
				"out_arity", "custominit") {
			if (defined($n{$a})) {
				$constructor{$a} = $n{$a};
Christian Würdig's avatar
Christian Würdig committed
630
631
			}
		}
632
633
		%constructors = ( "" => \%constructor );
	}
634

635
636
637
638
639
640
641
	foreach my $constr (keys(%constructors)) {
		my %cstr = %{ $constructors{$constr} };
		# Copy some values from outer node if they don't exists in the constr
		foreach my $a ("ins", "outs", "irn_flags", "mode", "args", "attr",
		               "custominit") {
			if (!defined($cstr{$a}) && defined($n{$a})) {
				$cstr{$a} = $n{$a};
642
			}
Christian Würdig's avatar
Christian Würdig committed
643
		}
644
645
		create_constructor($orig_op, $constr, \%cstr, \%n);
	}
Christian Würdig's avatar
Christian Würdig committed
646
647

	# set default values for state and flags if not given
648
649
650
651
	$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
652

653
	push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
Matthias Braun's avatar
Matthias Braun committed
654
	push(@obst_new_irop, "\tops.be_ops        = be_ops;\n");
655
	push(@obst_new_irop, "\tops.dump_node     = ${dump_func};\n");
Christian Würdig's avatar
Christian Würdig committed
656

Matthias Braun's avatar
Matthias Braun committed
657
658
	if (defined($cmp_attr_func)) {
		push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
Christian Würdig's avatar
Christian Würdig committed
659
	}
660
661
	my $copy_attr_func = $copy_attr{$attr_type};
	if (!defined($copy_attr_func)) {
662
663
664
665
666
		if ($attr_type eq "") {
			$copy_attr_func = "NULL";
		} else {
			$copy_attr_func = $default_copy_attr;
		}
667
668
669
670
	}
	if (defined($copy_attr_func)) {
		push(@obst_new_irop, "\tops.copy_attr = ${copy_attr_func};\n");
	}
671
672
673
	if (defined($hash_func)) {
		push(@obst_new_irop, "\tops.hash = ${hash_func};\n");
	}
Christian Würdig's avatar
Christian Würdig committed
674

675
	my %known_flags = map { $_ => 1 } (
676
677
678
		"none", "labeled", "commutative", "cfopcode", "unknown_jump", "fragile",
		"forking", "highlevel", "constlike", "always_opt", "keep",
		"start_block", "uses_memory", "dump_noblock", "dump_noinput",
679
		"cse_neutral"
680
	);
Matthias Braun's avatar
Matthias Braun committed
681
	my $is_fragile = 0;
682
683
684
685
	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
686
687
688
		if ($flag eq "fragile") {
			$is_fragile = 1;
		}
689
690
691
692
	}
	my @mapped = map { "irop_flag_$_" } @{$n{"op_flags"}};
	my $op_flags = join('|', @mapped);

693
694
695
696
697
	my $attr_size = "0";
	if ($attr_type ne "") {
		$attr_size = "sizeof(${attr_type})"
	}

698
	$n_opcodes++;
699
	$temp  = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", $op_flags";
700
	$temp .= ", ".translate_arity($arity).", 0, ${attr_size}, &ops);\n";
Christian Würdig's avatar
Christian Würdig committed
701
	push(@obst_new_irop, $temp);
Matthias Braun's avatar
Matthias Braun committed
702
	if ($is_fragile) {
703
704
		push(@obst_new_irop, "\tir_op_set_memory_index(op_${op}, n_${op}_mem);\n");
		push(@obst_new_irop, "\tir_op_set_fragile_indices(op_${op}, pn_${op}_X_regular, pn_${op}_X_except);\n");
Matthias Braun's avatar
Matthias Braun committed
705
	}
706
	push(@obst_new_irop, "\tset_op_tag(op_$op, $arch\_op_tag);\n");
707
	if(defined($default_op_attr_type)) {
708
		push(@obst_new_irop, "\tattr = &attrs[iro_$op];\n");
709
710
711
712
713
714
		if(defined($n{op_attr_init})) {
			push(@obst_new_irop, "\t".$n{op_attr_init}."\n");
		}
		push(@obst_new_irop, "\tset_op_attr(op_$op, attr);\n");
	}

715
	push(@obst_enum_op, "\tiro_$op,\n");
Christian Würdig's avatar
Christian Würdig committed
716

717
	$obst_header .= "\n";
718
}
719
720
push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
721
722
push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
723
724
725

# emit the code

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

728
729
730
print OUT "#include \"gen_$arch\_regalloc_if.h\"\n";
print OUT "#include \"irverify_t.h\"\n";
print OUT "\n";
Christian Würdig's avatar
Christian Würdig committed
731
732
print OUT @obst_cmp_attr;
print OUT "\n";
733
734
735
736
print OUT @obst_opvar;
print OUT "\n";
print OUT @obst_get_opvar;
print OUT "\n";
737

738
print OUT<<EOF;
739

740
741
static int $arch\_opcode_start = -1;
static int $arch\_opcode_end   = -1;
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
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));
}

print OUT<<ENDOFISIRN;

Michael Beck's avatar
Michael Beck committed
767
/** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
768
#define $arch\_op_tag FOURCC('$a', '$b', '$c', '$d')
Michael Beck's avatar
Michael Beck committed
769

770
771
/** Return the opcode number of the first $arch opcode. */
int get_$arch\_opcode_first(void) {
772
	return $arch\_opcode_start;
773
774
}

775
776
/** Return the opcode number of the last $arch opcode + 1. */
int get_$arch\_opcode_last(void) {
777
	return $arch\_opcode_end;
778
779
}

Michael Beck's avatar
Michael Beck committed
780
781
/** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
int is_$arch\_op(const ir_op *op) {
782
	return get_op_tag(op) == $arch\_op_tag;
Michael Beck's avatar
Michael Beck committed
783
784
}

785
/** Return 1 if the given node is a $arch machine node, 0 otherwise */
786
int is_$arch\_irn(const ir_node *node) {
Michael Beck's avatar
Michael Beck committed
787
	return is_$arch\_op(get_irn_op(node));
788
789
}

790
int get_$arch\_irn_opcode(const ir_node *node) {
791
792
793
	if (is_$arch\_irn(node))
		return get_irn_opcode(node) - $arch\_opcode_start;
	return -1;
794
795
}

796
797
ENDOFISIRN

798
799
800
801
print OUT <<END;
#ifdef BIT
#undef BIT
#endif
802
#define BIT(x)  (1 << (x))
803
804
805
806
807
808
809
810
811

END

print OUT @obst_limit_func;
print OUT "\n";

print OUT @obst_reg_reqs;
print OUT "\n";

812
print OUT<<ENDOFMAIN;
813
814
$obst_constructor

815
/**
816
 * Creates the $arch specific Firm machine operations
817
818
 * needed for the assembler irgs.
 */
Matthias Braun's avatar
Matthias Braun committed
819
void $arch\_create_opcodes(const arch_irn_ops_t *be_ops) {
820
821
822
	ir_op_ops  ops;
	int        cur_opcode;
	static int run_once = 0;
823
824
ENDOFMAIN

825
826
	if (defined($default_op_attr_type)) {
		print OUT "\t$default_op_attr_type *attr, *attrs;\n";
827
828
829
	}

print OUT<<ENDOFMAIN;
830
831
832
833
834
835

	if (run_once)
		return;
	run_once = 1;

	cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
836

837
	$arch\_opcode_start = cur_opcode;
838
839
ENDOFMAIN

840
	if (defined($default_op_attr_type)) {
841
		print OUT "\tattrs = XMALLOCNZ(${default_op_attr_type}, iro_${arch}_last);\n";
842
843
	}

844
print OUT @obst_new_irop;
845
print OUT "\n";
846
847
print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
848
849
print OUT " + $additional_opcodes" if (defined($additional_opcodes));
print OUT ";\n";
850
851
852
853
print OUT "}\n";

close(OUT);

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

856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
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

EOF

873
print OUT @obst_enum_op;
874
print OUT <<EOF;
875
int is_${arch}_irn(const ir_node *node);
876
int is_${arch}_op(const ir_op *op);
877
878
879
880
881
882
883

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

884
885
#endif
EOF
886
887

close(OUT);
888
889
890
891
892

###
# Translates numeric arity into string constant.
###
sub translate_arity {
Christian Würdig's avatar
Christian Würdig committed
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
	my $arity = shift;

	if ($arity =~ /^\d+$/) {
		if    ($arity == 0) {
			return "oparity_zero";
		}
		elsif ($arity == 1) {
			return "oparity_unary";
		}
		elsif ($arity == 2) {
			return "oparity_binary";
		}
		elsif ($arity == 3) {
			return "oparity_trinary";
		}
		else {
909
			return "oparity_any";
Christian Würdig's avatar
Christian Würdig committed
910
		}
911
912
913
914
915
	} elsif ($arity == $ARITY_VARIABLE) {
		return "oparity_variable";
	} elsif ($arity == $ARITY_DYNAMIC) {
		return "oparity_dynamic";
	} else {
916
		die "Fatal error: Unknown arity $arity";
Christian Würdig's avatar
Christian Würdig committed
917
	}
918
}
919

920
sub mangle_requirements {
921
922
	my $reqs  = shift;
	my $class = shift;
923
	my $flags = shift;
924
925
926
927
928
929
930
931

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

	@alternatives = sort @alternatives;

932
	my $name = $class."_".join('_', @alternatives);
933
934
935
936
	if (defined($flags)) {
		$flags =~ s/\|/_/g;
		$name .= "_$flags";
	}
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

	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;
}

###
# 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;
}

###
963
# Returns the index of a given register within its register class.
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
# @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
###
sub build_inout_idx_class {
	my $n     = shift;
	my $op    = shift;
	my $is_in = shift;
	my @idx_class;

	my $inout = ($is_in ? "in" : "out");

	if (exists($n->{"reg_req"}{"$inout"})) {
		my @reqs = @{ $n->{"reg_req"}{"$inout"} };

		for (my $idx = 0; $idx <= $#reqs; $idx++) {
			my $class = undef;
990
			my ($req,) = split(/:/, $reqs[$idx]);
991

992
			if ($req eq "none") {
993
				$class = "none";
994
995
			} elsif (is_reg_class($req)) {
				$class = $req;
996
			} else {
997
				my @regs = split(/ /, $req);
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
GET_CLASS:		foreach my $reg (@regs) {
					if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
						$class = "UNKNOWN_CLASS";
					} else {
						$class = get_reg_class($reg);
						if (!defined $class) {
							die("Fatal error: Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
						} else {
							last GET_CLASS;
						} # !defined class
					} # if (reg =~ ...
				} # foreach
			} # if

			push(@idx_class, $class);
		} # for
	} # if

	return @idx_class;
}

###
# 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;
1030
1031
	my $same_pos      = 0;
	my $different_pos = 0;
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
	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);
1045
	my $flags = shift;
1046
1047
1048
1049
1050

	my @idx_class = build_inout_idx_class($node, $op, !$is_in);

	# set/unset registers
CHECK_REQS: foreach (@regs) {
1051
		if (!$is_in && /(!)?in_r(\d+)/) {
1052
1053
1054
1055
1056
1057
1058
1059
			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);
1060
1061
			}

1062
1063
1064
			if ($same_pos & $bit_pos) {
				if ($1) {
					print STDERR "conflicting !in and in constraints\n";
1065
				} else {
1066
					print STDERR "duplicate in constraint\n";
1067
				}
1068
1069
1070
1071
1072
1073
1074
				return (undef, undef, undef, undef);
			}

			if ($1) {
				$different_pos |= $bit_pos;
			} else {
				$same_pos      |= $bit_pos;
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
			}

			$class = $idx_class[$2 - 1];
			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";
1087
				return (undef, undef, undef, undef);
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
			}

			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";
1101
				return (undef, undef, undef, undef);
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
			}

			$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";
1112
			return (undef, undef, undef, undef);
1113
1114
1115
1116
1117
1118
1119
1120
		}

		# 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";
1121
			return (undef, undef, undef, undef);
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
		}

		# 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 .= "$_ ";
	}

	# don't allow ignore regs in negative constraints
	if($neg) {
		my @cur_class = @{ $reg_classes{"$class"} };
		for (my $idx = 0; $idx <= $#cur_class; $idx++) {
			if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
1137
1138
				my $reg    = $cur_class[$idx]{"name"};
				my $regix  = get_reg_index($reg);
1139
1140
1141
1142
1143
1144
1145
1146
				my $arrayp = $regix / 32;
				push(@{$limit_array[$arrayp]}, $reg);
				$limit_reqs .= "$reg ";
			}
		}
	}

	if ($has_limit == 1) {
1147
		$limit_name = "${arch}_limit_".mangle_requirements($limit_reqs, $class);
1148
1149

		if(defined($limit_bitsets{$limit_name})) {
1150
			$limit_name = $limit_bitsets{$limit_name};
1151
			return ($class, $limit_name, $same_pos, $different_pos);
1152
1153
1154
1155
		}

		$limit_bitsets{$limit_name} = $limit_name;

Michael Beck's avatar
Michael Beck committed
1156
		push(@obst_limit_func, "static const unsigned " . $limit_name . "[] = { ");
1157
1158
		my $first = 1;
		my $limitbitsetlen = $regclass2len{$class};
1159
		my $limitarraylen = ($limitbitsetlen+31) / 32;
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
		for(my $i = 0; $i < $limitarraylen; $i++) {

			my $limitarraypart = $limit_array[$i];
			if($first) {
				$first = 0;
			} else {
				push(@obst_limit_func, ", ");
			}
			my $temp;
			if($neg) {
				$temp = "0xFFFFFFFF";
			}
			foreach my $reg (@{$limitarraypart}) {
				if($neg) {
					$temp .= " & ~";
				} elsif(defined($temp)) {
					$temp .= " | ";
				}
1178
1179
1180
1181
				my $firstreg = uc($reg_classes{$class}[0]->{"name"});
				my $classuc = uc($class);
				my $reguc = uc($reg);
				$temp .= "BIT(REG_${classuc}_${reguc})";
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
			}
			if(defined($temp)) {
				push(@obst_limit_func, "${temp}");
			} else {
				push(@obst_limit_func, "0");
			}
		}
		push(@obst_limit_func, " };\n");
	}

1192
	return ($class, $limit_name, $same_pos, $different_pos);
1193
1194
1195
1196
1197
1198
}

###
# Generate register requirements structure
###
sub generate_requirements {
1199
	my ($reqs, $flags) = split(/:/, shift);
1200
1201
1202
1203
	my $node  = shift;
	my $op    = shift;
	my $idx   = shift;
	my $is_in = shift;
1204
	my $class = "";
1205
	my $width = 1;
1206
	my $result;
1207

1208
1209
1210
1211
1212
1213
1214
	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");
1215
1216
1217
1218
			} 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);
1219
1220
1221
1222
			}
		}
	}

1223
	if ($reqs eq "none") {
1224
1225
1226

		$result = <<EOF;
{
1227
1228
1229
	arch_register_req_type_none,
	NULL,                         /* regclass */
	NULL,                         /* limit bitset */
1230
	0,                            /* same pos */
1231
1232
	0,                            /* different pos */
	0                             /* width */
1233
1234
1235
1236
};

EOF
	} elsif (is_reg_class($reqs)) {
1237
1238
		push(@req_type_mask, "arch_register_req_type_normal");
		my $reqtype = join(" | ", @req_type_mask);
1239
1240
1241
		$class  = $reqs;
		$result = <<EOF;
{
1242
	${reqtype},
1243
	& ${arch}_reg_classes[CLASS_${arch}_${class}],
1244
	NULL,        /* limit bitset */
1245
	0,           /* same pos */
1246
	0,           /* different pos */
1247
	$width            /* width */
1248
1249
1250
1251
1252
};

EOF

	} else {
1253
		my ($regclass, $limit_bitset, $same_pos, $different_pos)
1254
			= build_subset_class_func($node, $op, $idx, $is_in, $reqs, $flags);
1255

1256
		if (!defined($regclass)) {