1 /*
2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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.
8 *
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).
14 *
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.
18 *
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
21 * questions.
22 *
23 */
24
25 // FORMS.CPP - Definitions for ADL Parser Generic & Utility Forms Classes
26 #include "adlc.hpp"
27
28 //------------------------------Static Initializers----------------------------
29 // allocate arena used by forms
30 AdlArena *Form::arena = Form::generate_arena(); // = Form::generate_arena();
31 AdlArena *Form::generate_arena() {
32 return (new AdlArena);
33 }
34
35 //------------------------------NameList---------------------------------------
36 // reserved user-defined string
37 const char *NameList::_signal = "$$SIGNAL$$";
38 const char *NameList::_signal2 = "$$SIGNAL2$$";
39 const char *NameList::_signal3 = "$$SIGNAL3$$";
40
41 // Constructor and Destructor
42 NameList::NameList() : _cur(0), _max(4), _iter(0), _justReset(true) {
43 _names = (const char**) AdlAllocateHeap(_max*sizeof(char*));
44 }
45 NameList::~NameList() {
46 // The following free is a double-free, and crashes the program:
47 //free(_names); // not owner of strings
48 }
49
50 void NameList::addName(const char *name) {
51 if (_cur == _max) {
52 _names = (const char**) AdlReAllocateHeap(_names, (_max *=2)*sizeof(char*));
53 }
54 _names[_cur++] = name;
55 }
56
57 void NameList::add_signal() {
58 addName( _signal );
59 }
60 void NameList::clear() {
61 _cur = 0;
62 _iter = 0;
63 _justReset = true;
64 // _max = 4; Already allocated
65 }
66
67 int NameList::count() const { return _cur; }
68
69 void NameList::reset() { _iter = 0; _justReset = true;}
70 const char *NameList::iter() {
71 if (_justReset) {_justReset=false; return (_iter < _cur ? _names[_iter] : nullptr);}
72 else return (_iter <_cur-1 ? _names[++_iter] : nullptr);
73 }
74 const char *NameList::current() { return (_iter < _cur ? _names[_iter] : nullptr); }
75 const char *NameList::peek(int skip) { return (_iter + skip < _cur ? _names[_iter + skip] : nullptr); }
76
77 // Return 'true' if current entry is signal
78 bool NameList::current_is_signal() {
79 const char *entry = current();
80 return is_signal(entry);
81 }
82
83 // Return true if entry is a signal
84 bool NameList::is_signal(const char *entry) {
85 return ( (strcmp(entry,NameList::_signal) == 0) ? true : false);
86 }
87
88 // Search for a name in the list
89 bool NameList::search(const char *name) {
90 const char *entry;
91 for(reset(); (entry = iter()) != nullptr; ) {
92 if(!strcmp(entry,name)) return true;
93 }
94 return false;
95 }
96
97 // Return index of name in list
98 int NameList::index(const char *name) {
99 int cnt = 0;
100 const char *entry;
101 for(reset(); (entry = iter()) != nullptr; ) {
102 if(!strcmp(entry,name)) return cnt;
103 cnt++;
104 }
105 return Not_in_list;
106 }
107
108 // Return name at index in list
109 const char *NameList::name(intptr_t index) {
110 return ( index < _cur ? _names[index] : nullptr);
111 }
112
113 void NameList::dump() { output(stderr); }
114
115 void NameList::output(FILE *fp) {
116 fprintf(fp, "\n");
117
118 // Run iteration over all entries, independent of position of iterator.
119 const char *name = nullptr;
120 int iter = 0;
121 bool justReset = true;
122
123 while( ( name = (justReset ?
124 (justReset=false, (iter < _cur ? _names[iter] : nullptr)) :
125 (iter < _cur-1 ? _names[++iter] : nullptr)) )
126 != nullptr ) {
127 fprintf( fp, " %s,\n", name);
128 }
129 fprintf(fp, "\n");
130 }
131
132 //------------------------------NameAndList------------------------------------
133 // Storage for a name and an associated list of names
134 NameAndList::NameAndList(char *name) : _name(name) {
135 }
136 NameAndList::~NameAndList() {
137 }
138
139 // Add to entries in list
140 void NameAndList::add_entry(const char *entry) {
141 _list.addName(entry);
142 }
143
144 // Access the name and its associated list.
145 const char *NameAndList::name() const { return _name; }
146 void NameAndList::reset() { _list.reset(); }
147 const char *NameAndList::iter() { return _list.iter(); }
148
149 // Return the "index" entry in the list, zero-based
150 const char *NameAndList::operator[](int index) {
151 assert( index >= 0, "Internal Error(): index less than 0.");
152
153 _list.reset();
154 const char *entry = _list.iter();
155 // Iterate further if it isn't at index 0.
156 for ( int position = 0; position != index; ++position ) {
157 entry = _list.iter();
158 }
159
160 return entry;
161 }
162
163
164 void NameAndList::dump() { output(stderr); }
165 void NameAndList::output(FILE *fp) {
166 fprintf(fp, "\n");
167
168 // Output the Name
169 fprintf(fp, "Name == %s", (_name ? _name : "") );
170
171 // Output the associated list of names
172 const char *name;
173 fprintf(fp, " (");
174 for (reset(); (name = iter()) != nullptr;) {
175 fprintf(fp, " %s,\n", name);
176 }
177 fprintf(fp, ")");
178 fprintf(fp, "\n");
179 }
180
181 //------------------------------Form-------------------------------------------
182 OpClassForm *Form::is_opclass() const {
183 return nullptr;
184 }
185
186 OperandForm *Form::is_operand() const {
187 return nullptr;
188 }
189
190 InstructForm *Form::is_instruction() const {
191 return nullptr;
192 }
193
194 MachNodeForm *Form::is_machnode() const {
195 return nullptr;
196 }
197
198 AttributeForm *Form::is_attribute() const {
199 return nullptr;
200 }
201
202 Effect *Form::is_effect() const {
203 return nullptr;
204 }
205
206 ResourceForm *Form::is_resource() const {
207 return nullptr;
208 }
209
210 PipeClassForm *Form::is_pipeclass() const {
211 return nullptr;
212 }
213
214 Form::DataType Form::ideal_to_const_type(const char *name) const {
215 if( name == nullptr ) { return Form::none; }
216
217 if (strcmp(name,"ConI")==0) return Form::idealI;
218 if (strcmp(name,"ConP")==0) return Form::idealP;
219 if (strcmp(name,"ConN")==0) return Form::idealN;
220 if (strcmp(name,"ConNKlass")==0) return Form::idealNKlass;
221 if (strcmp(name,"ConL")==0) return Form::idealL;
222 if (strcmp(name,"ConF")==0) return Form::idealF;
223 if (strcmp(name,"ConH")==0) return Form::idealH;
224 if (strcmp(name,"ConD")==0) return Form::idealD;
225 if (strcmp(name,"Bool")==0) return Form::idealI;
226
227 return Form::none;
228 }
229
230 Form::DataType Form::ideal_to_sReg_type(const char *name) const {
231 if( name == nullptr ) { return Form::none; }
232
233 if (strcmp(name,"sRegI")==0) return Form::idealI;
234 if (strcmp(name,"sRegP")==0) return Form::idealP;
235 if (strcmp(name,"sRegF")==0) return Form::idealF;
236 if (strcmp(name,"sRegD")==0) return Form::idealD;
237 if (strcmp(name,"sRegL")==0) return Form::idealL;
238 return Form::none;
239 }
240
241 Form::DataType Form::ideal_to_Reg_type(const char *name) const {
242 if( name == nullptr ) { return Form::none; }
243
244 if (strcmp(name,"RegI")==0) return Form::idealI;
245 if (strcmp(name,"RegP")==0) return Form::idealP;
246 if (strcmp(name,"RegF")==0) return Form::idealF;
247 if (strcmp(name,"RegD")==0) return Form::idealD;
248 if (strcmp(name,"RegL")==0) return Form::idealL;
249
250 return Form::none;
251 }
252
253 // True if 'opType', an ideal name, loads or stores.
254 Form::DataType Form::is_load_from_memory(const char *opType) const {
255 if( strcmp(opType,"LoadB")==0 ) return Form::idealB;
256 if( strcmp(opType,"LoadUB")==0 ) return Form::idealB;
257 if( strcmp(opType,"LoadUS")==0 ) return Form::idealC;
258 if( strcmp(opType,"LoadD")==0 ) return Form::idealD;
259 if( strcmp(opType,"LoadD_unaligned")==0 ) return Form::idealD;
260 if( strcmp(opType,"LoadF")==0 ) return Form::idealF;
261 if( strcmp(opType,"LoadI")==0 ) return Form::idealI;
262 if( strcmp(opType,"LoadKlass")==0 ) return Form::idealP;
263 if( strcmp(opType,"LoadNKlass")==0 ) return Form::idealNKlass;
264 if( strcmp(opType,"LoadL")==0 ) return Form::idealL;
265 if( strcmp(opType,"LoadL_unaligned")==0 ) return Form::idealL;
266 if( strcmp(opType,"LoadP")==0 ) return Form::idealP;
267 if( strcmp(opType,"LoadN")==0 ) return Form::idealN;
268 if( strcmp(opType,"LoadRange")==0 ) return Form::idealI;
269 if( strcmp(opType,"LoadS")==0 ) return Form::idealS;
270 if( strcmp(opType,"LoadVector")==0 ) return Form::idealV;
271 if( strcmp(opType,"LoadVectorGather")==0 ) return Form::idealV;
272 if( strcmp(opType,"LoadVectorGatherMasked")==0 ) return Form::idealV;
273 if( strcmp(opType,"LoadVectorMasked")==0 ) return Form::idealV;
274 assert( strcmp(opType,"Load") != 0, "Must type Loads" );
275 return Form::none;
276 }
277
278 Form::DataType Form::is_store_to_memory(const char *opType) const {
279 if( strcmp(opType,"StoreB")==0) return Form::idealB;
280 if( strcmp(opType,"StoreC")==0) return Form::idealC;
281 if( strcmp(opType,"StoreD")==0) return Form::idealD;
282 if( strcmp(opType,"StoreF")==0) return Form::idealF;
283 if( strcmp(opType,"StoreI")==0) return Form::idealI;
284 if( strcmp(opType,"StoreL")==0) return Form::idealL;
285 if( strcmp(opType,"StoreLSpecial")==0) return Form::idealL;
286 if( strcmp(opType,"StoreP")==0) return Form::idealP;
287 if( strcmp(opType,"StoreN")==0) return Form::idealN;
288 if( strcmp(opType,"StoreNKlass")==0) return Form::idealNKlass;
289 if( strcmp(opType,"StoreVector")==0 ) return Form::idealV;
290 if( strcmp(opType,"StoreVectorScatter")==0 ) return Form::idealV;
291 if( strcmp(opType,"StoreVectorScatterMasked")==0 ) return Form::idealV;
292 if( strcmp(opType,"StoreVectorMasked")==0 ) return Form::idealV;
293 assert( strcmp(opType,"Store") != 0, "Must type Stores" );
294 return Form::none;
295 }
296
297 Form::InterfaceType Form::interface_type(FormDict &globals) const {
298 return Form::no_interface;
299 }
300
301 //------------------------------FormList---------------------------------------
302 // Destructor
303 FormList::~FormList() {
304 // // This list may not own its elements
305 // Form *cur = _root;
306 // Form *next = nullptr;
307 // for( ; (cur = next) != nullptr; ) {
308 // next = (Form *)cur->_next;
309 // delete cur;
310 // }
311 };
312
313 //------------------------------FormDict---------------------------------------
314 // Constructor
315 FormDict::FormDict( CmpKey cmp, Hash hash, AdlArena *arena )
316 : _form(cmp, hash, arena) {
317 }
318 FormDict::~FormDict() {
319 }
320
321 // Return # of name-Form pairs in dict
322 int FormDict::Size(void) const {
323 return _form.Size();
324 }
325
326 // Insert inserts the given key-value pair into the dictionary. The prior
327 // value of the key is returned; null if the key was not previously defined.
328 const Form *FormDict::Insert(const char *name, Form *form) {
329 return (Form*)_form.Insert((void*)name, (void*)form);
330 }
331
332 // Finds the value of a given key; or null if not found.
333 // The dictionary is NOT changed.
334 const Form *FormDict::operator [](const char *name) const {
335 return (Form*)_form[name];
336 }
337
338 //------------------------------FormDict::private------------------------------
339 // Disable public use of constructor, copy-ctor, operator =, operator ==
340 FormDict::FormDict( ) : _form(cmpkey,hashkey) {
341 assert( false, "NotImplemented");
342 }
343 FormDict::FormDict( const FormDict & fd) : _form(fd._form) {
344 }
345 FormDict &FormDict::operator =( const FormDict &rhs) {
346 assert( false, "NotImplemented");
347 _form = rhs._form;
348 return *this;
349 }
350 // == compares two dictionaries; they must have the same keys (their keys
351 // must match using CmpKey) and they must have the same values (pointer
352 // comparison). If so 1 is returned, if not 0 is returned.
353 bool FormDict::operator ==(const FormDict &d) const {
354 assert( false, "NotImplemented");
355 return false;
356 }
357
358 // Print out the dictionary contents as key-value pairs
359 static void dumpkey (const void* key) { fprintf(stdout, "%s", (char*) key); }
360 static void dumpform(const void* form) { fflush(stdout); ((Form*)form)->dump(); }
361
362 void FormDict::dump() {
363 _form.print(dumpkey, dumpform);
364 }
365
366 void FormDict::forms_do(FormClosure* f) {;
367 DictI iter(&_form);
368 for( ; iter.test(); ++iter ) {
369 Form* form = (Form*) iter._value;
370 assert(form != nullptr, "sanity");
371 f->do_form(form);
372 }
373 }
374
375 //------------------------------SourceForm-------------------------------------
376 SourceForm::SourceForm(char* code) : _code(code) { }; // Constructor
377 SourceForm::~SourceForm() {
378 }
379
380 void SourceForm::dump() { // Debug printer
381 output(stderr);
382 }
383
384 void SourceForm::output(FILE *fp) {
385 fprintf(fp,"\n//%s\n%s\n",classname(),(_code?_code:""));
386 }
387
388 void FormClosure::do_form(Form* form) {
389 assert(false, "should not reach here");
390 }
391
392 void FormClosure::do_form_by_name(const char* name) {
393 assert(false, "should not reach here");
394 }