2
// Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
3
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
// This code is free software; you can redistribute it and/or modify it
6
// under the terms of the GNU General Public License version 2 only, as
7
// published by the Free Software Foundation.
9
// This code is distributed in the hope that it will be useful, but WITHOUT
10
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
// version 2 for more details (a copy is included in the LICENSE file that
13
// accompanied this code).
15
// You should have received a copy of the GNU General Public License version
16
// 2 along with this work; if not, write to the Free Software Foundation,
17
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
// Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
// or visit www.oracle.com if you need additional information or have any
26
// archDesc.cpp - Internal format for architecture definition
27
#include <unordered_set>
30
static FILE *errfile = stderr;
32
//--------------------------- utility functions -----------------------------
33
inline char toUpper(char lower) {
34
return (('a' <= lower && lower <= 'z') ? ((char) (lower + ('A'-'a'))) : lower);
36
char *toUpper(const char *str) {
37
char *upper = new char[strlen(str)+1];
39
const char *end = str + strlen(str);
40
for (; str < end; ++str, ++upper) {
41
*upper = toUpper(*str);
47
//---------------------------ChainList Methods-------------------------------
48
ChainList::ChainList() {
51
void ChainList::insert(const char *name, const char *cost, const char *rule) {
57
bool ChainList::search(const char *name) {
58
return _name.search(name);
61
void ChainList::reset() {
67
bool ChainList::iter(const char * &name, const char * &cost, const char * &rule) {
69
const char *n = _name.iter();
70
const char *c = _cost.iter();
71
const char *r = _rule.iter();
83
void ChainList::dump() {
87
void ChainList::output(FILE *fp) {
88
fprintf(fp, "\nChain Rules: output resets iterator\n");
89
const char *cost = nullptr;
90
const char *name = nullptr;
91
const char *rule = nullptr;
92
bool chains_exist = false;
93
for(reset(); (iter(name,cost,rule)) == true; ) {
94
fprintf(fp, "Chain to <%s> at cost #%s using %s_rule\n",name, cost ? cost : "0", rule);
95
// // Check for transitive chain rules
96
// Form *form = (Form *)_globalNames[rule];
97
// if (form->is_instruction()) {
98
// // chain_rule(fp, indent, name, cost, rule);
99
// chain_rule(fp, indent, name, cost, rule);
103
if( ! chains_exist ) {
104
fprintf(fp, "No entries in this ChainList\n");
109
//---------------------------MatchList Methods-------------------------------
110
bool MatchList::search(const char *opc, const char *res, const char *lch,
111
const char *rch, Predicate *pr) {
113
if ((res == _resultStr) || (res && _resultStr && !strcmp(res, _resultStr))) {
114
if ((lch == _lchild) || (lch && _lchild && !strcmp(lch, _lchild))) {
115
if ((rch == _rchild) || (rch && _rchild && !strcmp(rch, _rchild))) {
116
char * predStr = get_pred();
117
char * prStr = pr?pr->_pred:nullptr;
118
if (ADLParser::equivalent_expressions(prStr, predStr)) {
125
tmp = _next->search(opc, res, lch, rch, pr);
131
void MatchList::dump() {
135
void MatchList::output(FILE *fp) {
136
fprintf(fp, "\nMatchList output is Unimplemented();\n");
140
//---------------------------ArchDesc Constructor and Destructor-------------
143
: _globalNames(cmpstr,hashstr, Form::arena),
144
_globalDefs(cmpstr,hashstr, Form::arena),
145
_preproc_table(cmpstr,hashstr, Form::arena),
146
_idealIndex(cmpstr,hashstr, Form::arena),
147
_internalOps(cmpstr,hashstr, Form::arena),
148
_internalMatch(cmpstr,hashstr, Form::arena),
149
_chainRules(cmpstr,hashstr, Form::arena),
150
_cisc_spill_operand(nullptr),
151
_needs_deep_clone_jvms(false) {
153
// Initialize the opcode to MatchList table with nulls
154
for( int i=0; i<_last_opcode; ++i ) {
155
_mlistab[i] = nullptr;
158
// Set-up the global tables
159
initKeywords(_globalNames); // Initialize the Name Table with keywords
161
// Prime user-defined types with predefined types: Set, RegI, RegF, ...
164
// Initialize flags & counters
168
_disable_warnings = 0;
172
_adlocation_debug = 0;
173
_internalOpCounter = 0;
174
_cisc_spill_debug = false;
175
_short_branch_debug = false;
177
// Initialize match rule flags
178
for (int i = 0; i < _last_opcode; i++) {
179
_has_match_rule[i] = false;
182
// Error/Warning Counts
188
// Initialize I/O Files
189
_ADL_file._name = nullptr; _ADL_file._fp = nullptr;
190
// Machine dependent output files
191
_DFA_file._name = nullptr; _DFA_file._fp = nullptr;
192
_HPP_file._name = nullptr; _HPP_file._fp = nullptr;
193
_CPP_file._name = nullptr; _CPP_file._fp = nullptr;
194
_bug_file._name = "bugs.out"; _bug_file._fp = nullptr;
196
// Initialize Register & Pipeline Form Pointers
203
ArchDesc::~ArchDesc() {
208
//---------------------------ArchDesc methods: Public ----------------------
209
// Store forms according to type
210
void ArchDesc::addForm(PreHeaderForm *ptr) { _pre_header.addForm(ptr); };
211
void ArchDesc::addForm(HeaderForm *ptr) { _header.addForm(ptr); };
212
void ArchDesc::addForm(SourceForm *ptr) { _source.addForm(ptr); };
213
void ArchDesc::addForm(EncodeForm *ptr) { _encode = ptr; };
214
void ArchDesc::addForm(InstructForm *ptr) { _instructions.addForm(ptr); };
215
void ArchDesc::addForm(MachNodeForm *ptr) { _machnodes.addForm(ptr); };
216
void ArchDesc::addForm(OperandForm *ptr) { _operands.addForm(ptr); };
217
void ArchDesc::addForm(OpClassForm *ptr) { _opclass.addForm(ptr); };
218
void ArchDesc::addForm(AttributeForm *ptr) { _attributes.addForm(ptr); };
219
void ArchDesc::addForm(RegisterForm *ptr) { _register = ptr; };
220
void ArchDesc::addForm(FrameForm *ptr) { _frame = ptr; };
221
void ArchDesc::addForm(PipelineForm *ptr) { _pipeline = ptr; };
223
// Build MatchList array and construct MatchLists
224
void ArchDesc::generateMatchLists() {
225
// Call inspection routines to populate array
227
inspectInstructions();
230
// Build MatchList structures for operands
231
void ArchDesc::inspectOperands() {
233
// Iterate through all operands
236
for( ; (op = (OperandForm*)_operands.iter()) != nullptr;) {
237
// Construct list of top-level operands (components)
238
op->build_components();
240
// Ensure that match field is defined.
241
if ( op->_matrule == nullptr ) continue;
243
// Type check match rules
244
check_optype(op->_matrule);
246
// Construct chain rules
247
build_chain_rule(op);
249
MatchRule *mrule = op->_matrule;
250
Predicate *pred = op->_predicate;
252
// Grab the machine type of the operand
253
const char *rootOp = op->_ident;
254
mrule->_machType = rootOp;
256
// Check for special cases
257
if (strcmp(rootOp,"Universe")==0) continue;
258
if (strcmp(rootOp,"label")==0) continue;
260
assert( strcmp(rootOp,"sReg") != 0, "Disable untyped 'sReg'");
261
if (strcmp(rootOp,"sRegI")==0) continue;
262
if (strcmp(rootOp,"sRegP")==0) continue;
263
if (strcmp(rootOp,"sRegF")==0) continue;
264
if (strcmp(rootOp,"sRegD")==0) continue;
265
if (strcmp(rootOp,"sRegL")==0) continue;
267
// Cost for this match
268
const char *costStr = op->cost();
269
const char *defaultCost =
270
((AttributeForm*)_globalNames[AttributeForm::_op_cost])->_attrdef;
271
const char *cost = costStr? costStr : defaultCost;
273
// Find result type for match.
274
const char *result = op->reduce_result();
276
// Construct a MatchList for this entry.
277
// Iterate over the list to enumerate all match cases for operands with multiple match rules.
278
for (; mrule != nullptr; mrule = mrule->_next) {
279
mrule->_machType = rootOp;
280
buildMatchList(mrule, result, rootOp, pred, cost);
285
// Build MatchList structures for instructions
286
void ArchDesc::inspectInstructions() {
288
// Iterate through all instructions
289
_instructions.reset();
291
for( ; (instr = (InstructForm*)_instructions.iter()) != nullptr; ) {
292
// Construct list of top-level operands (components)
293
instr->build_components();
295
// Ensure that match field is defined.
296
if ( instr->_matrule == nullptr ) continue;
298
MatchRule &mrule = *instr->_matrule;
299
Predicate *pred = instr->build_predicate();
301
// Grab the machine type of the operand
302
const char *rootOp = instr->_ident;
303
mrule._machType = rootOp;
305
// Cost for this match
306
const char *costStr = instr->cost();
307
const char *defaultCost =
308
((AttributeForm*)_globalNames[AttributeForm::_ins_cost])->_attrdef;
309
const char *cost = costStr? costStr : defaultCost;
311
// Find result type for match
312
const char *result = instr->reduce_result();
314
if (( instr->is_ideal_branch() && instr->label_position() == -1) ||
315
(!instr->is_ideal_branch() && instr->label_position() != -1)) {
316
syntax_err(instr->_linenum, "%s: Only branches to a label are supported\n", rootOp);
319
Attribute *attr = instr->_attribs;
320
while (attr != nullptr) {
321
if (strcmp(attr->_ident,"ins_short_branch") == 0 &&
322
attr->int_val(*this) != 0) {
323
if (!instr->is_ideal_branch() || instr->label_position() == -1) {
324
syntax_err(instr->_linenum, "%s: Only short branch to a label is supported\n", rootOp);
326
instr->set_short_branch(true);
327
} else if (strcmp(attr->_ident,"ins_alignment") == 0 &&
328
attr->int_val(*this) != 0) {
329
instr->set_alignment(attr->int_val(*this));
331
attr = (Attribute *)attr->_next;
334
if (!instr->is_short_branch()) {
335
buildMatchList(instr->_matrule, result, mrule._machType, pred, cost);
340
static int setsResult(MatchRule &mrule) {
341
if (strcmp(mrule._name,"Set") == 0) return 1;
345
const char *ArchDesc::getMatchListIndex(MatchRule &mrule) {
346
if (setsResult(mrule)) {
348
return mrule._rChild->_opType;
351
return mrule._opType;
356
//------------------------------result of reduction----------------------------
359
//------------------------------left reduction---------------------------------
360
// Return the left reduction associated with an internal name
361
const char *ArchDesc::reduceLeft(char *internalName) {
362
const char *left = nullptr;
363
MatchNode *mnode = (MatchNode*)_internalMatch[internalName];
364
if (mnode->_lChild) {
365
mnode = mnode->_lChild;
366
left = mnode->_internalop ? mnode->_internalop : mnode->_opType;
372
//------------------------------right reduction--------------------------------
373
const char *ArchDesc::reduceRight(char *internalName) {
374
const char *right = nullptr;
375
MatchNode *mnode = (MatchNode*)_internalMatch[internalName];
376
if (mnode->_rChild) {
377
mnode = mnode->_rChild;
378
right = mnode->_internalop ? mnode->_internalop : mnode->_opType;
384
//------------------------------check_optype-----------------------------------
385
void ArchDesc::check_optype(MatchRule *mrule) {
386
MatchRule *rule = mrule;
389
// // Cycle through the list of match rules
391
// // Check for a filled in type field
392
// if (mrule->_opType == nullptr) {
393
// const Form *form = operands[_result];
394
// OpClassForm *opcForm = form ? form->is_opclass() : nullptr;
395
// assert(opcForm != nullptr, "Match Rule contains invalid operand name.");
397
// char *opType = opcForm->_ident;
401
//------------------------------add_chain_rule_entry--------------------------
402
void ArchDesc::add_chain_rule_entry(const char *src, const char *cost,
403
const char *result) {
404
// Look-up the operation in chain rule table
405
ChainList *lst = (ChainList *)_chainRules[src];
406
if (lst == nullptr) {
407
lst = new ChainList();
408
_chainRules.Insert(src, lst);
410
if (!lst->search(result)) {
411
if (cost == nullptr) {
412
cost = ((AttributeForm*)_globalNames[AttributeForm::_op_cost])->_attrdef;
414
lst->insert(result, cost, result);
418
//------------------------------build_chain_rule-------------------------------
419
void ArchDesc::build_chain_rule(OperandForm *oper) {
422
// Check for chain rules here
423
// If this is only a chain rule
424
if ((oper->_matrule) && (oper->_matrule->_lChild == nullptr) &&
425
(oper->_matrule->_rChild == nullptr)) {
428
const Form *form = _globalNames[oper->_matrule->_opType];
429
if ((form) && form->is_operand() &&
430
(form->ideal_only() == false)) {
431
add_chain_rule_entry(oper->_matrule->_opType, oper->cost(), oper->_ident);
434
// Check for additional chain rules
435
if (oper->_matrule->_next) {
436
rule = oper->_matrule;
439
// Any extra match rules after the first must be chain rules
440
const Form *form = _globalNames[rule->_opType];
441
if ((form) && form->is_operand() &&
442
(form->ideal_only() == false)) {
443
add_chain_rule_entry(rule->_opType, oper->cost(), oper->_ident);
445
} while(rule->_next != nullptr);
448
else if ((oper->_matrule) && (oper->_matrule->_next)) {
449
// Regardless of whether the first matchrule is a chain rule, check the list
450
rule = oper->_matrule;
453
// Any extra match rules after the first must be chain rules
454
const Form *form = _globalNames[rule->_opType];
455
if ((form) && form->is_operand() &&
456
(form->ideal_only() == false)) {
457
assert( oper->cost(), "This case expects null cost, not default cost");
458
add_chain_rule_entry(rule->_opType, oper->cost(), oper->_ident);
460
} while(rule->_next != nullptr);
465
//------------------------------buildMatchList---------------------------------
466
// operands and instructions provide the result
467
void ArchDesc::buildMatchList(MatchRule *mrule, const char *resultStr,
468
const char *rootOp, Predicate *pred,
470
const char *leftstr, *rightstr;
473
leftstr = rightstr = nullptr;
474
// Check for chain rule, and do not generate a match list for it
475
if ( mrule->is_chain_rule(_globalNames) ) {
479
// Identify index position among ideal operands
480
intptr_t index = _last_opcode;
481
const char *indexStr = getMatchListIndex(*mrule);
482
index = (intptr_t)_idealIndex[indexStr];
484
fprintf(stderr, "Ideal node missing: %s\n", indexStr);
485
assert(index != 0, "Failed lookup of ideal node\n");
488
// Check that this will be placed appropriately in the DFA
489
if (index >= _last_opcode) {
490
fprintf(stderr, "Invalid match rule %s <-- ( %s )\n",
491
resultStr ? resultStr : " ",
492
rootOp ? rootOp : " ");
493
assert(index < _last_opcode, "Matching item not in ideal graph\n");
498
// Walk the MatchRule, generating MatchList entries for each level
499
// of the rule (each nesting of parentheses)
501
if (!strcmp(mrule->_opType, "Set")) {
502
mnode = mrule->_rChild;
503
buildMList(mnode, rootOp, resultStr, pred, cost);
506
// Build MatchLists for children
507
// Check each child for an internal operand name, and use that name
508
// for the parent's matchlist entry if it exists
509
mnode = mrule->_lChild;
511
buildMList(mnode, nullptr, nullptr, nullptr, nullptr);
512
leftstr = mnode->_internalop ? mnode->_internalop : mnode->_opType;
514
mnode = mrule->_rChild;
516
buildMList(mnode, nullptr, nullptr, nullptr, nullptr);
517
rightstr = mnode->_internalop ? mnode->_internalop : mnode->_opType;
519
// Search for an identical matchlist entry already on the list
520
if ((_mlistab[index] == nullptr) ||
522
!_mlistab[index]->search(rootOp, resultStr, leftstr, rightstr, pred))) {
523
// Place this match rule at front of list
525
new MatchList(_mlistab[index], pred, cost,
526
rootOp, resultStr, leftstr, rightstr);
527
_mlistab[index] = mList;
531
// Recursive call for construction of match lists
532
void ArchDesc::buildMList(MatchNode *node, const char *rootOp,
533
const char *resultOp, Predicate *pred,
535
const char *leftstr, *rightstr;
536
const char *resultop;
541
leftstr = rightstr = nullptr;
542
// Do not process leaves of the Match Tree if they are not ideal
543
if ((node) && (node->_lChild == nullptr) && (node->_rChild == nullptr) &&
544
((form = (Form *)_globalNames[node->_opType]) != nullptr) &&
545
(!form->ideal_only())) {
549
// Identify index position among ideal operands
550
intptr_t index = _last_opcode;
551
const char *indexStr = node ? node->_opType : (char *) " ";
552
index = (intptr_t)_idealIndex[indexStr];
554
fprintf(stderr, "error: operand \"%s\" not found\n", indexStr);
555
assert(0, "fatal error");
558
if (node == nullptr) {
559
fprintf(stderr, "error: node is null\n");
560
assert(0, "fatal error");
562
// Build MatchLists for children
563
// Check each child for an internal operand name, and use that name
564
// for the parent's matchlist entry if it exists
565
mnode = node->_lChild;
567
buildMList(mnode, nullptr, nullptr, nullptr, nullptr);
568
leftstr = mnode->_internalop ? mnode->_internalop : mnode->_opType;
570
mnode = node->_rChild;
572
buildMList(mnode, nullptr, nullptr, nullptr, nullptr);
573
rightstr = mnode->_internalop ? mnode->_internalop : mnode->_opType;
575
// Grab the string for the opcode of this list entry
576
if (rootOp == nullptr) {
577
opcode = (node->_internalop) ? node->_internalop : node->_opType;
581
// Grab the string for the result of this list entry
582
if (resultOp == nullptr) {
583
resultop = (node->_internalop) ? node->_internalop : node->_opType;
585
else resultop = resultOp;
586
// Search for an identical matchlist entry already on the list
587
if ((_mlistab[index] == nullptr) || (_mlistab[index] &&
588
!_mlistab[index]->search(opcode, resultop, leftstr, rightstr, pred))) {
589
// Place this match rule at front of list
591
new MatchList(_mlistab[index],pred,cost,
592
opcode, resultop, leftstr, rightstr);
593
_mlistab[index] = mList;
597
// Count number of OperandForms defined
598
int ArchDesc::operandFormCount() {
599
// Only interested in ones with non-null match rule
600
int count = 0; _operands.reset();
602
for( ; (cur = (OperandForm*)_operands.iter()) != nullptr; ) {
603
if (cur->_matrule != nullptr) ++count;
608
// Count number of OpClassForms defined
609
int ArchDesc::opclassFormCount() {
610
// Only interested in ones with non-null match rule
611
int count = 0; _operands.reset();
613
for( ; (cur = (OpClassForm*)_opclass.iter()) != nullptr; ) {
619
// Count number of InstructForms defined
620
int ArchDesc::instructFormCount() {
621
// Only interested in ones with non-null match rule
622
int count = 0; _instructions.reset();
624
for( ; (cur = (InstructForm*)_instructions.iter()) != nullptr; ) {
625
if (cur->_matrule != nullptr) ++count;
631
//------------------------------get_preproc_def--------------------------------
632
// Return the textual binding for a given CPP flag name.
633
// Return null if there is no binding, or it has been #undef-ed.
634
char* ArchDesc::get_preproc_def(const char* flag) {
635
// In case of syntax errors, flag may take the value null.
636
SourceForm* deff = nullptr;
638
deff = (SourceForm*) _preproc_table[flag];
639
return (deff == nullptr) ? nullptr : deff->_code;
643
//------------------------------set_preproc_def--------------------------------
644
// Change or create a textual binding for a given CPP flag name.
645
// Giving null means the flag name is to be #undef-ed.
646
// In any case, _preproc_list collects all names either #defined or #undef-ed.
647
void ArchDesc::set_preproc_def(const char* flag, const char* def) {
648
SourceForm* deff = (SourceForm*) _preproc_table[flag];
649
if (deff == nullptr) {
650
deff = new SourceForm(nullptr);
651
_preproc_table.Insert(flag, deff);
652
_preproc_list.addName(flag); // this supports iteration
654
deff->_code = (char*) def;
658
bool ArchDesc::verify() {
661
assert( _register->verify(), "Register declarations failed verification");
663
fprintf(stderr,"\n");
664
// fprintf(stderr,"---------------------------- Verify Operands ---------------\n");
665
// _operands.verify();
666
// fprintf(stderr,"\n");
667
// fprintf(stderr,"---------------------------- Verify Operand Classes --------\n");
668
// _opclass.verify();
669
// fprintf(stderr,"\n");
670
// fprintf(stderr,"---------------------------- Verify Attributes ------------\n");
671
// _attributes.verify();
672
// fprintf(stderr,"\n");
674
fprintf(stderr,"---------------------------- Verify Instructions ----------------------------\n");
675
_instructions.verify();
677
fprintf(stderr,"\n");
679
// fprintf(stderr,"---------------------------- Verify Encodings --------------\n");
680
// _encode->verify();
683
//if (_pipeline) _pipeline->verify();
688
class MarkUsageFormClosure : public FormClosure {
691
std::unordered_set<Form*> *_visited;
694
MarkUsageFormClosure(ArchDesc* ad, std::unordered_set<Form*> *visit_map) {
696
_visited = visit_map;
698
virtual ~MarkUsageFormClosure() = default;
700
virtual void do_form(Form *form) {
701
if (_visited->find(form) == _visited->end()) {
702
_visited->insert(form);
703
form->forms_do(this);
707
virtual void do_form_by_name(const char* name) {
708
const Form* form = _ad->globalNames()[name];
710
do_form(const_cast<Form*>(form));
713
RegisterForm* regs = _ad->get_registers();
714
if (regs->getRegClass(name)) {
715
do_form(regs->getRegClass(name));
721
// check unused operands
722
bool ArchDesc::check_usage() {
723
if (_disable_warnings) {
727
std::unordered_set<Form*> visited;
728
MarkUsageFormClosure callback(this, &visited);
729
_instructions.reset();
730
// iterate all instruction to mark used form
732
for ( ; (instr = (InstructForm*)_instructions.iter()) != nullptr; ) {
733
callback.do_form(instr);
736
// these forms are coded in OperandForm::is_user_name_for_sReg
737
// it may happen no instruction use these operands, like stackSlotP in aarch64,
738
// but we can not desclare they are useless.
739
callback.do_form_by_name("stackSlotI");
740
callback.do_form_by_name("stackSlotP");
741
callback.do_form_by_name("stackSlotD");
742
callback.do_form_by_name("stackSlotF");
743
callback.do_form_by_name("stackSlotL");
745
// sReg* are initial created by adlc in ArchDesc::initBaseOpTypes()
746
// In ARM, no definition or usage in adfile, but they are reported as unused
747
callback.do_form_by_name("sRegI");
748
callback.do_form_by_name("sRegP");
749
callback.do_form_by_name("sRegD");
750
callback.do_form_by_name("sRegF");
751
callback.do_form_by_name("sRegL");
753
// special generic vector operands only used in Matcher::pd_specialize_generic_vector_operand
754
// x86_32 combine x86.ad and x86_32.ad, the vec*/legVec* can not be cleaned from IA32
756
callback.do_form_by_name("vecA");
757
callback.do_form_by_name("vecD");
758
callback.do_form_by_name("vecX");
759
#elif defined(IA32) || defined(AMD64)
760
callback.do_form_by_name("vecS");
761
callback.do_form_by_name("vecD");
762
callback.do_form_by_name("vecX");
763
callback.do_form_by_name("vecY");
764
callback.do_form_by_name("vecZ");
765
callback.do_form_by_name("legVecS");
766
callback.do_form_by_name("legVecD");
767
callback.do_form_by_name("legVecX");
768
callback.do_form_by_name("legVecY");
769
callback.do_form_by_name("legVecZ");
774
OperandForm* operand;
775
for ( ; (operand = (OperandForm*)_operands.iter()) != nullptr; ) {
776
if(visited.find(operand) == visited.end() && !operand->ideal_only()) {
777
fprintf(stderr, "\nWarning: unused operand (%s)", operand->_ident);
781
if (cnt) fprintf(stderr, "\n-------Warning: total %d unused operands\n", cnt);
786
void ArchDesc::dump() {
790
if (_register) _register->dump();
791
fprintf(stderr,"\n");
792
fprintf(stderr,"------------------ Dump Operands ---------------------\n");
794
fprintf(stderr,"\n");
795
fprintf(stderr,"------------------ Dump Operand Classes --------------\n");
797
fprintf(stderr,"\n");
798
fprintf(stderr,"------------------ Dump Attributes ------------------\n");
800
fprintf(stderr,"\n");
801
fprintf(stderr,"------------------ Dump Instructions -----------------\n");
802
_instructions.dump();
804
fprintf(stderr,"------------------ Dump Encodings --------------------\n");
807
if (_pipeline) _pipeline->dump();
811
//------------------------------init_keywords----------------------------------
812
// Load the keywords into the global name table
813
void ArchDesc::initKeywords(FormDict& names) {
814
// Insert keyword strings into Global Name Table. Keywords have a null value
815
// field for quick easy identification when checking identifiers.
816
names.Insert("instruct", nullptr);
817
names.Insert("operand", nullptr);
818
names.Insert("attribute", nullptr);
819
names.Insert("source", nullptr);
820
names.Insert("register", nullptr);
821
names.Insert("pipeline", nullptr);
822
names.Insert("constraint", nullptr);
823
names.Insert("predicate", nullptr);
824
names.Insert("encode", nullptr);
825
names.Insert("enc_class", nullptr);
826
names.Insert("interface", nullptr);
827
names.Insert("opcode", nullptr);
828
names.Insert("ins_encode", nullptr);
829
names.Insert("match", nullptr);
830
names.Insert("effect", nullptr);
831
names.Insert("expand", nullptr);
832
names.Insert("rewrite", nullptr);
833
names.Insert("reg_def", nullptr);
834
names.Insert("reg_class", nullptr);
835
names.Insert("alloc_class", nullptr);
836
names.Insert("resource", nullptr);
837
names.Insert("pipe_class", nullptr);
838
names.Insert("pipe_desc", nullptr);
842
//------------------------------internal_err----------------------------------
843
// Issue a parser error message, and skip to the end of the current line
844
void ArchDesc::internal_err(const char *fmt, ...) {
848
_internal_errs += emit_msg(0, INTERNAL_ERR, 0, fmt, args);
854
//------------------------------syntax_err----------------------------------
855
// Issue a parser error message, and skip to the end of the current line
856
void ArchDesc::syntax_err(int lineno, const char *fmt, ...) {
860
_internal_errs += emit_msg(0, SYNERR, lineno, fmt, args);
866
//------------------------------emit_msg---------------------------------------
867
// Emit a user message, typically a warning or error
868
int ArchDesc::emit_msg(int quiet, int flag, int line, const char *fmt,
870
static int last_lineno = -1;
875
case 0: pref = "Warning: "; break;
876
case 1: pref = "Syntax Error: "; break;
877
case 2: pref = "Semantic Error: "; break;
878
case 3: pref = "Internal Error: "; break;
879
default: assert(0, ""); break;
882
if (line == last_lineno) return 0;
885
if (!quiet) { /* no output if in quiet mode */
886
i = fprintf(errfile, "%s(%d) ", _ADL_file._name, line);
887
while (i++ <= 15) fputc(' ', errfile);
888
fprintf(errfile, "%-8s:", pref);
889
vfprintf(errfile, fmt, args);
890
fprintf(errfile, "\n");
897
// ---------------------------------------------------------------------------
898
//--------Utilities to build mappings for machine registers ------------------
899
// ---------------------------------------------------------------------------
901
// Construct the name of the register mask.
902
static const char *getRegMask(const char *reg_class_name) {
903
if( reg_class_name == nullptr ) return "RegMask::Empty";
905
if (strcmp(reg_class_name,"Universe")==0) {
906
return "RegMask::Empty";
907
} else if (strcmp(reg_class_name,"stack_slots")==0) {
908
return "(Compile::current()->FIRST_STACK_mask())";
909
} else if (strcmp(reg_class_name, "dynamic")==0) {
910
return "*_opnds[0]->in_RegMask(0)";
912
char *rc_name = toUpper(reg_class_name);
913
const char *mask = "_mask";
914
int length = (int)strlen(rc_name) + (int)strlen(mask) + 5;
915
char *regMask = new char[length];
916
snprintf_checked(regMask, length, "%s%s()", rc_name, mask);
922
// Convert a register class name to its register mask.
923
const char *ArchDesc::reg_class_to_reg_mask(const char *rc_name) {
924
const char *reg_mask = "RegMask::Empty";
927
RegClass *reg_class = _register->getRegClass(rc_name);
928
if (reg_class == nullptr) {
929
syntax_err(0, "Use of an undefined register class %s", rc_name);
933
// Construct the name of the register mask.
934
reg_mask = getRegMask(rc_name);
941
// Obtain the name of the RegMask for an OperandForm
942
const char *ArchDesc::reg_mask(OperandForm &opForm) {
943
const char *regMask = "RegMask::Empty";
945
// Check constraints on result's register class
946
const char *result_class = opForm.constrained_reg_class();
947
if (result_class == nullptr) {
949
syntax_err(opForm._linenum,
950
"Use of an undefined result class for operand: %s",
955
regMask = reg_class_to_reg_mask( result_class );
960
// Obtain the name of the RegMask for an InstructForm
961
const char *ArchDesc::reg_mask(InstructForm &inForm) {
962
const char *result = inForm.reduce_result();
964
if (result == nullptr) {
965
syntax_err(inForm._linenum,
966
"Did not find result operand or RegMask"
967
" for this instruction: %s",
972
// Instructions producing 'Universe' use RegMask::Empty
973
if (strcmp(result,"Universe") == 0) {
974
return "RegMask::Empty";
977
// Lookup this result operand and get its register class
978
Form *form = (Form*)_globalNames[result];
979
if (form == nullptr) {
980
syntax_err(inForm._linenum,
981
"Did not find result operand for result: %s", result);
984
OperandForm *oper = form->is_operand();
985
if (oper == nullptr) {
986
syntax_err(inForm._linenum, "Form is not an OperandForm:");
990
return reg_mask( *oper );
994
// Obtain the STACK_OR_reg_mask name for an OperandForm
995
char *ArchDesc::stack_or_reg_mask(OperandForm &opForm) {
996
// name of cisc_spillable version
997
const char *reg_mask_name = reg_mask(opForm);
999
if (reg_mask_name == nullptr) {
1000
syntax_err(opForm._linenum,
1001
"Did not find reg_mask for opForm: %s",
1006
const char *stack_or = "STACK_OR_";
1007
int length = (int)strlen(stack_or) + (int)strlen(reg_mask_name) + 1;
1008
char *result = new char[length];
1009
snprintf_checked(result, length, "%s%s", stack_or, reg_mask_name);
1014
// Record that the register class must generate a stack_or_reg_mask
1015
void ArchDesc::set_stack_or_reg(const char *reg_class_name) {
1017
RegClass *reg_class = _register->getRegClass(reg_class_name);
1018
reg_class->set_stack_version(true);
1023
// Return the type signature for the ideal operation
1024
const char *ArchDesc::getIdealType(const char *idealOp) {
1025
// Find last character in idealOp, it specifies the type
1027
const char *ptr = idealOp;
1028
for (; *ptr != '\0'; ++ptr) {
1032
// Match Vector types.
1033
if (strncmp(idealOp, "Vec",3)==0) {
1035
case 'A': return "TypeVect::VECTA";
1036
case 'S': return "TypeVect::VECTS";
1037
case 'D': return "TypeVect::VECTD";
1038
case 'X': return "TypeVect::VECTX";
1039
case 'Y': return "TypeVect::VECTY";
1040
case 'Z': return "TypeVect::VECTZ";
1042
internal_err("Vector type %s with unrecognized type\n",idealOp);
1046
if (strncmp(idealOp, "RegVectMask", 8) == 0) {
1047
return "TypeVect::VECTMASK";
1052
case 'I': return "TypeInt::INT";
1053
case 'P': return "TypePtr::BOTTOM";
1054
case 'N': return "TypeNarrowOop::BOTTOM";
1055
case 'F': return "Type::FLOAT";
1056
case 'D': return "Type::DOUBLE";
1057
case 'L': return "TypeLong::LONG";
1058
case 's': return "TypeInt::CC /*flags*/";
1062
// internal_err("Ideal type %s with unrecognized type\n",idealOp);
1071
OperandForm *ArchDesc::constructOperand(const char *ident,
1073
OperandForm *opForm = new OperandForm(ident, ideal_only);
1074
_globalNames.Insert(ident, opForm);
1081
// Import predefined base types: Set = 1, RegI, RegP, ...
1082
void ArchDesc::initBaseOpTypes() {
1083
// Create OperandForm and assign type for each opcode.
1084
for (int i = 1; i < _last_machine_leaf; ++i) {
1085
char *ident = (char *)NodeClassNames[i];
1086
constructOperand(ident, true);
1088
// Create InstructForm and assign type for each ideal instruction.
1089
for (int j = _last_machine_leaf+1; j < _last_opcode; ++j) {
1090
char *ident = (char *)NodeClassNames[j];
1091
if (!strcmp(ident, "ConI") || !strcmp(ident, "ConP") ||
1092
!strcmp(ident, "ConN") || !strcmp(ident, "ConNKlass") ||
1093
!strcmp(ident, "ConF") || !strcmp(ident, "ConD") ||
1094
!strcmp(ident, "ConL") || !strcmp(ident, "Con" ) ||
1095
!strcmp(ident, "Bool")) {
1096
constructOperand(ident, true);
1098
InstructForm *insForm = new InstructForm(ident, true);
1099
// insForm->_opcode = nextUserOpType(ident);
1100
_globalNames.Insert(ident, insForm);
1105
{ OperandForm *opForm;
1106
// Create operand type "Universe" for return instructions.
1107
const char *ident = "Universe";
1108
opForm = constructOperand(ident, false);
1110
// Create operand type "label" for branch targets
1112
opForm = constructOperand(ident, false);
1114
// !!!!! Update - when adding a new sReg/stackSlot type
1115
// Create operand types "sReg[IPFDL]" for stack slot registers
1116
opForm = constructOperand("sRegI", false);
1117
opForm->_constraint = new Constraint("ALLOC_IN_RC", "stack_slots");
1118
opForm = constructOperand("sRegP", false);
1119
opForm->_constraint = new Constraint("ALLOC_IN_RC", "stack_slots");
1120
opForm = constructOperand("sRegF", false);
1121
opForm->_constraint = new Constraint("ALLOC_IN_RC", "stack_slots");
1122
opForm = constructOperand("sRegD", false);
1123
opForm->_constraint = new Constraint("ALLOC_IN_RC", "stack_slots");
1124
opForm = constructOperand("sRegL", false);
1125
opForm->_constraint = new Constraint("ALLOC_IN_RC", "stack_slots");
1127
// Create operand type "method" for call targets
1129
opForm = constructOperand(ident, false);
1132
// Create Effect Forms for each of the legal effects
1133
// USE, DEF, USE_DEF, KILL, USE_KILL
1135
const char *ident = "USE";
1136
Effect *eForm = new Effect(ident);
1137
_globalNames.Insert(ident, eForm);
1139
eForm = new Effect(ident);
1140
_globalNames.Insert(ident, eForm);
1142
eForm = new Effect(ident);
1143
_globalNames.Insert(ident, eForm);
1145
eForm = new Effect(ident);
1146
_globalNames.Insert(ident, eForm);
1148
eForm = new Effect(ident);
1149
_globalNames.Insert(ident, eForm);
1151
eForm = new Effect(ident);
1152
_globalNames.Insert(ident, eForm);
1154
eForm = new Effect(ident);
1155
_globalNames.Insert(ident, eForm);
1157
eForm = new Effect(ident);
1158
_globalNames.Insert(ident, eForm);
1162
// Build mapping from ideal names to ideal indices
1164
for (idealIndex = 1; idealIndex < _last_machine_leaf; ++idealIndex) {
1165
const char *idealName = NodeClassNames[idealIndex];
1166
_idealIndex.Insert((void*) idealName, (void*) (intptr_t) idealIndex);
1168
for (idealIndex = _last_machine_leaf+1;
1169
idealIndex < _last_opcode; ++idealIndex) {
1170
const char *idealName = NodeClassNames[idealIndex];
1171
_idealIndex.Insert((void*) idealName, (void*) (intptr_t) idealIndex);
1177
//---------------------------addSUNcopyright-------------------------------
1178
// output SUN copyright info
1179
void ArchDesc::addSunCopyright(char* legal, int size, FILE *fp) {
1180
size_t count = fwrite(legal, 1, size, fp);
1181
assert(count == (size_t) size, "copyright info truncated");
1183
fprintf(fp,"// Machine Generated File. Do Not Edit!\n");
1188
//---------------------------addIncludeGuardStart--------------------------
1189
// output the start of an include guard.
1190
void ArchDesc::addIncludeGuardStart(ADLFILE &adlfile, const char* guardString) {
1191
// Build #include lines
1192
fprintf(adlfile._fp, "\n");
1193
fprintf(adlfile._fp, "#ifndef %s\n", guardString);
1194
fprintf(adlfile._fp, "#define %s\n", guardString);
1195
fprintf(adlfile._fp, "\n");
1199
//---------------------------addIncludeGuardEnd--------------------------
1200
// output the end of an include guard.
1201
void ArchDesc::addIncludeGuardEnd(ADLFILE &adlfile, const char* guardString) {
1202
// Build #include lines
1203
fprintf(adlfile._fp, "\n");
1204
fprintf(adlfile._fp, "#endif // %s\n", guardString);
1208
//---------------------------addInclude--------------------------
1209
// output the #include line for this file.
1210
void ArchDesc::addInclude(ADLFILE &adlfile, const char* fileName) {
1211
fprintf(adlfile._fp, "#include \"%s\"\n", fileName);
1215
void ArchDesc::addInclude(ADLFILE &adlfile, const char* includeDir, const char* fileName) {
1216
fprintf(adlfile._fp, "#include \"%s/%s\"\n", includeDir, fileName);
1220
//---------------------------addPreprocessorChecks-----------------------------
1221
// Output C preprocessor code to verify the backend compilation environment.
1222
// The idea is to force code produced by "adlc -DHS64" to be compiled by a
1223
// command of the form "CC ... -DHS64 ...", so that any #ifdefs in the source
1224
// blocks select C code that is consistent with adlc's selections of AD code.
1225
void ArchDesc::addPreprocessorChecks(FILE *fp) {
1227
_preproc_list.reset();
1228
if (_preproc_list.count() > 0 && !_preproc_list.current_is_signal()) {
1229
fprintf(fp, "// Check consistency of C++ compilation with ADLC options:\n");
1231
for (_preproc_list.reset(); (flag = _preproc_list.iter()) != nullptr; ) {
1232
if (_preproc_list.current_is_signal()) break;
1233
char* def = get_preproc_def(flag);
1234
fprintf(fp, "// Check adlc ");
1236
fprintf(fp, "-D%s=%s\n", flag, def);
1237
else fprintf(fp, "-U%s\n", flag);
1238
fprintf(fp, "#%s %s\n",
1239
def ? "ifndef" : "ifdef", flag);
1240
fprintf(fp, "# error \"%s %s be defined\"\n",
1241
flag, def ? "must" : "must not");
1242
fprintf(fp, "#endif // %s\n", flag);
1247
// Convert operand name into enum name
1248
const char *ArchDesc::machOperEnum(const char *opName) {
1249
return ArchDesc::getMachOperEnum(opName);
1252
// Convert operand name into enum name
1253
const char *ArchDesc::getMachOperEnum(const char *opName) {
1254
return (opName ? toUpper(opName) : opName);
1257
//---------------------------buildMustCloneMap-----------------------------
1258
// Flag cases when machine needs cloned values or instructions
1259
void ArchDesc::buildMustCloneMap(FILE *fp_hpp, FILE *fp_cpp) {
1260
// Build external declarations for mappings
1261
fprintf(fp_hpp, "// Mapping from machine-independent opcode to boolean\n");
1262
fprintf(fp_hpp, "// Flag cases where machine needs cloned values or instructions\n");
1263
fprintf(fp_hpp, "extern const char must_clone[];\n");
1264
fprintf(fp_hpp, "\n");
1266
// Build mapping from ideal names to ideal indices
1267
fprintf(fp_cpp, "\n");
1268
fprintf(fp_cpp, "// Mapping from machine-independent opcode to boolean\n");
1269
fprintf(fp_cpp, "const char must_clone[] = {\n");
1270
for (int idealIndex = 0; idealIndex < _last_opcode; ++idealIndex) {
1272
const char *idealName = NodeClassNames[idealIndex];
1273
// Previously selected constants for cloning
1275
// These are the current machine-dependent clones
1276
if ( strcmp(idealName,"CmpI") == 0
1277
|| strcmp(idealName,"CmpU") == 0
1278
|| strcmp(idealName,"CmpP") == 0
1279
|| strcmp(idealName,"CmpN") == 0
1280
|| strcmp(idealName,"CmpL") == 0
1281
|| strcmp(idealName,"CmpUL") == 0
1282
|| strcmp(idealName,"CmpD") == 0
1283
|| strcmp(idealName,"CmpF") == 0
1284
|| strcmp(idealName,"FastLock") == 0
1285
|| strcmp(idealName,"FastUnlock") == 0
1286
|| strcmp(idealName,"OverflowAddI") == 0
1287
|| strcmp(idealName,"OverflowAddL") == 0
1288
|| strcmp(idealName,"OverflowSubI") == 0
1289
|| strcmp(idealName,"OverflowSubL") == 0
1290
|| strcmp(idealName,"OverflowMulI") == 0
1291
|| strcmp(idealName,"OverflowMulL") == 0
1292
|| strcmp(idealName,"Bool") == 0
1293
|| strcmp(idealName,"Binary") == 0
1294
|| strcmp(idealName,"VectorTest") == 0 ) {
1295
// Removed ConI from the must_clone list. CPUs that cannot use
1296
// large constants as immediates manifest the constant as an
1297
// instruction. The must_clone flag prevents the constant from
1298
// floating up out of loops.
1301
fprintf(fp_cpp, " %d%s // %s: %d\n", must_clone,
1302
(idealIndex != (_last_opcode - 1)) ? "," : " // no trailing comma",
1303
idealName, idealIndex);
1305
// Finish defining table
1306
fprintf(fp_cpp, "};\n");