OSDN Git Service

compiler: Use backend interface for type sizes and alignments.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / backend.h
1 // backend.h -- Go frontend interface to backend  -*- C++ -*-
2
3 // Copyright 2011 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 #ifndef GO_BACKEND_H
8 #define GO_BACKEND_H
9
10 // Pointers to these types are created by the backend, passed to the
11 // frontend, and passed back to the backend.  The types must be
12 // defined by the backend using these names.
13
14 // The backend representation of a type.
15 class Btype;
16
17 // The backend represention of an expression.
18 class Bexpression;
19
20 // The backend representation of a statement.
21 class Bstatement;
22
23 // The backend representation of a function definition.
24 class Bfunction;
25
26 // The backend representation of a block.
27 class Bblock;
28
29 // The backend representation of a variable.
30 class Bvariable;
31
32 // The backend representation of a label.
33 class Blabel;
34
35 // The backend interface.  This is a pure abstract class that a
36 // specific backend will implement.
37
38 class Backend
39 {
40  public:
41   virtual ~Backend() { }
42
43   // Name/type/location.  Used for function parameters, struct fields,
44   // interface methods.
45   struct Btyped_identifier
46   {
47     std::string name;
48     Btype* btype;
49     Location location;
50
51     Btyped_identifier()
52       : name(), btype(NULL), location(UNKNOWN_LOCATION)
53     { }
54
55     Btyped_identifier(const std::string& a_name, Btype* a_btype,
56                      Location a_location)
57       : name(a_name), btype(a_btype), location(a_location)
58     { }
59   };
60
61   // Types.
62
63   // Produce an error type.  Actually the backend could probably just
64   // crash if this is called.
65   virtual Btype*
66   error_type() = 0;
67
68   // Get a void type.  This is used in (at least) two ways: 1) as the
69   // return type of a function with no result parameters; 2)
70   // unsafe.Pointer is represented as *void.
71   virtual Btype*
72   void_type() = 0;
73
74   // Get the unnamed boolean type.
75   virtual Btype*
76   bool_type() = 0;
77
78   // Get an unnamed integer type with the given signedness and number
79   // of bits.
80   virtual Btype*
81   integer_type(bool is_unsigned, int bits) = 0;
82
83   // Get an unnamed floating point type with the given number of bits
84   // (32 or 64).
85   virtual Btype*
86   float_type(int bits) = 0;
87
88   // Get an unnamed complex type with the given number of bits (64 or 128).
89   virtual Btype*
90   complex_type(int bits) = 0;
91
92   // Get a pointer type.
93   virtual Btype*
94   pointer_type(Btype* to_type) = 0;
95
96   // Get a function type.  The receiver, parameter, and results are
97   // generated from the types in the Function_type.  The Function_type
98   // is provided so that the names are available.
99   virtual Btype*
100   function_type(const Btyped_identifier& receiver,
101                 const std::vector<Btyped_identifier>& parameters,
102                 const std::vector<Btyped_identifier>& results,
103                 Location location) = 0;
104
105   // Get a struct type.
106   virtual Btype*
107   struct_type(const std::vector<Btyped_identifier>& fields) = 0;
108
109   // Get an array type.
110   virtual Btype*
111   array_type(Btype* element_type, Bexpression* length) = 0;
112
113   // Create a placeholder pointer type.  This is used for a named
114   // pointer type, since in Go a pointer type may refer to itself.
115   // NAME is the name of the type, and the location is where the named
116   // type is defined.  This function is also used for unnamed function
117   // types with multiple results, in which case the type has no name
118   // and NAME will be empty.  FOR_FUNCTION is true if this is for a Go
119   // function type, which corresponds to a C/C++ pointer to function
120   // type.  The return value will later be passed as the first
121   // parameter to set_placeholder_pointer_type or
122   // set_placeholder_function_type.
123   virtual Btype*
124   placeholder_pointer_type(const std::string& name, Location,
125                            bool for_function) = 0;
126
127   // Fill in a placeholder pointer type as a pointer.  This takes a
128   // type returned by placeholder_pointer_type and arranges for it to
129   // point to to_type.  Returns true on success, false on failure.
130   virtual bool
131   set_placeholder_pointer_type(Btype* placeholder, Btype* to_type) = 0;
132
133   // Fill in a placeholder pointer type as a function.  This takes a
134   // type returned by placeholder_pointer_type and arranges for it to
135   // become a real Go function type (which corresponds to a C/C++
136   // pointer to function type).  FT will be something returned by the
137   // function_type method.  Returns true on success, false on failure.
138   virtual bool
139   set_placeholder_function_type(Btype* placeholder, Btype* ft) = 0;
140
141   // Create a placeholder struct type.  This is used for a named
142   // struct type, as with placeholder_pointer_type.
143   virtual Btype*
144   placeholder_struct_type(const std::string& name, Location) = 0;
145
146   // Fill in a placeholder struct type.  This takes a type returned by
147   // placeholder_struct_type and arranges for it to become a real
148   // struct type.  The parameter is as for struct_type.  Returns true
149   // on success, false on failure.
150   virtual bool
151   set_placeholder_struct_type(Btype* placeholder,
152                               const std::vector<Btyped_identifier>& fields)
153                         = 0;
154
155   // Create a placeholder array type.  This is used for a named array
156   // type, as with placeholder_pointer_type, to handle cases like
157   // type A []*A.
158   virtual Btype*
159   placeholder_array_type(const std::string& name, Location) = 0;
160
161   // Fill in a placeholder array type.  This takes a type returned by
162   // placeholder_array_type and arranges for it to become a real array
163   // type.  The parameters are as for array_type.  Returns true on
164   // success, false on failure.
165   virtual bool
166   set_placeholder_array_type(Btype* placeholder, Btype* element_type,
167                              Bexpression* length) = 0;
168
169   // Return a named version of a type.  The location is the location
170   // of the type definition.  This will not be called for a type
171   // created via placeholder_pointer_type, placeholder_struct_type, or
172   // placeholder_array_type..  (It may be called for a pointer,
173   // struct, or array type in a case like "type P *byte; type Q P".)
174   virtual Btype*
175   named_type(const std::string& name, Btype*, Location) = 0;
176
177   // Create a marker for a circular pointer type.  Go pointer and
178   // function types can refer to themselves in ways that are not
179   // permitted in C/C++.  When a circular type is found, this function
180   // is called for the circular reference.  This permits the backend
181   // to decide how to handle such a type.  PLACEHOLDER is the
182   // placeholder type which has already been created; if the backend
183   // is prepared to handle a circular pointer type, it may simply
184   // return PLACEHOLDER.  FOR_FUNCTION is true if this is for a
185   // function type.
186   //
187   // For "type P *P" the sequence of calls will be
188   //   bt1 = placeholder_pointer_type();
189   //   bt2 = circular_pointer_type(bt1, false);
190   //   set_placeholder_pointer_type(bt1, bt2);
191   virtual Btype*
192   circular_pointer_type(Btype* placeholder, bool for_function) = 0;
193
194   // Return whether the argument could be a special type created by
195   // circular_pointer_type.  This is used to introduce explicit type
196   // conversions where needed.  If circular_pointer_type returns its
197   // PLACEHOLDER parameter, this may safely always return false.
198   virtual bool
199   is_circular_pointer_type(Btype*) = 0;
200
201   // Return the size of a type.
202   virtual size_t
203   type_size(Btype*) = 0;
204
205   // Return the alignment of a type.
206   virtual size_t
207   type_alignment(Btype*) = 0;
208
209   // Return the alignment of a struct field of this type.  This is
210   // normally the same as type_alignment, but not always.
211   virtual size_t
212   type_field_alignment(Btype*) = 0;
213
214   // Return the offset of field INDEX in a struct type.  INDEX is the
215   // entry in the FIELDS std::vector parameter of struct_type or
216   // set_placeholder_struct_type.
217   virtual size_t
218   type_field_offset(Btype*, size_t index) = 0;
219
220   // Expressions.
221
222   // Return an expression for a zero value of the given type.  This is
223   // used for cases such as local variable initialization and
224   // converting nil to other types.
225   virtual Bexpression*
226   zero_expression(Btype*) = 0;
227
228   // Statements.
229
230   // Create an error statement.  This is used for cases which should
231   // not occur in a correct program, in order to keep the compilation
232   // going without crashing.
233   virtual Bstatement*
234   error_statement() = 0;
235
236   // Create an expression statement.
237   virtual Bstatement*
238   expression_statement(Bexpression*) = 0;
239
240   // Create a variable initialization statement.  This initializes a
241   // local variable at the point in the program flow where it is
242   // declared.
243   virtual Bstatement*
244   init_statement(Bvariable* var, Bexpression* init) = 0;
245
246   // Create an assignment statement.
247   virtual Bstatement*
248   assignment_statement(Bexpression* lhs, Bexpression* rhs,
249                        Location) = 0;
250
251   // Create a return statement, passing the representation of the
252   // function and the list of values to return.
253   virtual Bstatement*
254   return_statement(Bfunction*, const std::vector<Bexpression*>&,
255                    Location) = 0;
256
257   // Create an if statement.  ELSE_BLOCK may be NULL.
258   virtual Bstatement*
259   if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
260                Location) = 0;
261
262   // Create a switch statement where the case values are constants.
263   // CASES and STATEMENTS must have the same number of entries.  If
264   // VALUE matches any of the list in CASES[i], which will all be
265   // integers, then STATEMENTS[i] is executed.  STATEMENTS[i] will
266   // either end with a goto statement or will fall through into
267   // STATEMENTS[i + 1].  CASES[i] is empty for the default clause,
268   // which need not be last.
269   virtual Bstatement*
270   switch_statement(Bexpression* value,
271                    const std::vector<std::vector<Bexpression*> >& cases,
272                    const std::vector<Bstatement*>& statements,
273                    Location) = 0;
274
275   // Create a single statement from two statements.
276   virtual Bstatement*
277   compound_statement(Bstatement*, Bstatement*) = 0;
278
279   // Create a single statement from a list of statements.
280   virtual Bstatement*
281   statement_list(const std::vector<Bstatement*>&) = 0;
282
283   // Blocks.
284
285   // Create a block.  The frontend will call this function when it
286   // starts converting a block within a function.  FUNCTION is the
287   // current function.  ENCLOSING is the enclosing block; it will be
288   // NULL for the top-level block in a function.  VARS is the list of
289   // local variables defined within this block; each entry will be
290   // created by the local_variable function.  START_LOCATION is the
291   // location of the start of the block, more or less the location of
292   // the initial curly brace.  END_LOCATION is the location of the end
293   // of the block, more or less the location of the final curly brace.
294   // The statements will be added after the block is created.
295   virtual Bblock*
296   block(Bfunction* function, Bblock* enclosing,
297         const std::vector<Bvariable*>& vars,
298         Location start_location, Location end_location) = 0;
299
300   // Add the statements to a block.  The block is created first.  Then
301   // the statements are created.  Then the statements are added to the
302   // block.  This will called exactly once per block.  The vector may
303   // be empty if there are no statements.
304   virtual void
305   block_add_statements(Bblock*, const std::vector<Bstatement*>&) = 0;
306
307   // Return the block as a statement.  This is used to include a block
308   // in a list of statements.
309   virtual Bstatement*
310   block_statement(Bblock*) = 0;
311
312   // Variables.
313
314   // Create an error variable.  This is used for cases which should
315   // not occur in a correct program, in order to keep the compilation
316   // going without crashing.
317   virtual Bvariable*
318   error_variable() = 0;
319
320   // Create a global variable.  PACKAGE_NAME is the name of the
321   // package where the variable is defined.  UNIQUE_PREFIX is the
322   // prefix for that package, from the -fgo-prefix option.  NAME is
323   // the name of the variable.  BTYPE is the type of the variable.
324   // IS_EXTERNAL is true if the variable is defined in some other
325   // package.  IS_HIDDEN is true if the variable is not exported (name
326   // begins with a lower case letter).  LOCATION is where the variable
327   // was defined.
328   virtual Bvariable*
329   global_variable(const std::string& package_name,
330                   const std::string& unique_prefix,
331                   const std::string& name,
332                   Btype* btype,
333                   bool is_external,
334                   bool is_hidden,
335                   Location location) = 0;
336
337   // A global variable will 1) be initialized to zero, or 2) be
338   // initialized to a constant value, or 3) be initialized in the init
339   // function.  In case 2, the frontend will call
340   // global_variable_set_init to set the initial value.  If this is
341   // not called, the backend should initialize a global variable to 0.
342   // The init function may then assign a value to it.
343   virtual void
344   global_variable_set_init(Bvariable*, Bexpression*) = 0;
345
346   // Create a local variable.  The frontend will create the local
347   // variables first, and then create the block which contains them.
348   // FUNCTION is the function in which the variable is defined.  NAME
349   // is the name of the variable.  TYPE is the type.  IS_ADDRESS_TAKEN
350   // is true if the address of this variable is taken (this implies
351   // that the address does not escape the function, as otherwise the
352   // variable would be on the heap).  LOCATION is where the variable
353   // is defined.  For each local variable the frontend will call
354   // init_statement to set the initial value.
355   virtual Bvariable*
356   local_variable(Bfunction* function, const std::string& name, Btype* type,
357                  bool is_address_taken, Location location) = 0;
358
359   // Create a function parameter.  This is an incoming parameter, not
360   // a result parameter (result parameters are treated as local
361   // variables).  The arguments are as for local_variable.
362   virtual Bvariable*
363   parameter_variable(Bfunction* function, const std::string& name,
364                      Btype* type, bool is_address_taken,
365                      Location location) = 0;
366
367   // Create a temporary variable.  A temporary variable has no name,
368   // just a type.  We pass in FUNCTION and BLOCK in case they are
369   // needed.  If INIT is not NULL, the variable should be initialized
370   // to that value.  Otherwise the initial value is irrelevant--the
371   // backend does not have to explicitly initialize it to zero.
372   // ADDRESS_IS_TAKEN is true if the programs needs to take the
373   // address of this temporary variable.  LOCATION is the location of
374   // the statement or expression which requires creating the temporary
375   // variable, and may not be very useful.  This function should
376   // return a variable which can be referenced later and should set
377   // *PSTATEMENT to a statement which initializes the variable.
378   virtual Bvariable*
379   temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression* init,
380                      bool address_is_taken, Location location,
381                      Bstatement** pstatement) = 0;
382
383   // Create a named immutable initialized data structure.  This is
384   // used for type descriptors and map descriptors.  This returns a
385   // Bvariable because it corresponds to an initialized const global
386   // variable in C.
387   //
388   // NAME is the name to use for the initialized global variable which
389   // this call will create.
390   //
391   // IS_COMMON is true if NAME may be defined by several packages, and
392   // the linker should merge all such definitions.  If IS_COMMON is
393   // false, NAME should be defined in only one file.  In general
394   // IS_COMMON will be true for the type descriptor of an unnamed type
395   // or a builtin type.
396   //
397   // TYPE will be a struct type; the type of the returned expression
398   // must be a pointer to this struct type.
399   // 
400   // We must create the named structure before we know its
401   // initializer, because the initializer may refer to its own
402   // address.  After calling this the frontend will call
403   // immutable_struct_set_init.
404   virtual Bvariable*
405   immutable_struct(const std::string& name, bool is_common, Btype* type,
406                    Location) = 0;
407
408   // Set the initial value of a variable created by immutable_struct.
409   // The NAME, IS_COMMON, TYPE, and location parameters are the same
410   // ones passed to immutable_struct.  INITIALIZER will be a composite
411   // literal of type TYPE.  It will not contain any function calls or
412   // anything else which can not be put into a read-only data section.
413   // It may contain the address of variables created by
414   // immutable_struct.
415   virtual void
416   immutable_struct_set_init(Bvariable*, const std::string& name,
417                             bool is_common, Btype* type, Location,
418                             Bexpression* initializer) = 0;
419
420   // Create a reference to a named immutable initialized data
421   // structure defined in some other package.  This will be a
422   // structure created by a call to immutable_struct with the same
423   // NAME and TYPE and with IS_COMMON passed as false.  This
424   // corresponds to an extern const global variable in C.
425   virtual Bvariable*
426   immutable_struct_reference(const std::string& name, Btype* type,
427                              Location) = 0;
428
429   // Labels.
430   
431   // Create a new label.  NAME will be empty if this is a label
432   // created by the frontend for a loop construct.  The location is
433   // where the the label is defined.
434   virtual Blabel*
435   label(Bfunction*, const std::string& name, Location) = 0;
436
437   // Create a statement which defines a label.  This statement will be
438   // put into the codestream at the point where the label should be
439   // defined.
440   virtual Bstatement*
441   label_definition_statement(Blabel*) = 0;
442
443   // Create a goto statement to a label.
444   virtual Bstatement*
445   goto_statement(Blabel*, Location) = 0;
446
447   // Create an expression for the address of a label.  This is used to
448   // get the return address of a deferred function which may call
449   // recover.
450   virtual Bexpression*
451   label_address(Blabel*, Location) = 0;
452 };
453
454 // The backend interface has to define this function.
455
456 extern Backend* go_get_backend();
457
458 // FIXME: Temporary helper functions while converting to new backend
459 // interface.
460
461 extern Btype* tree_to_type(tree);
462 extern Bexpression* tree_to_expr(tree);
463 extern Bstatement* tree_to_stat(tree);
464 extern Bfunction* tree_to_function(tree);
465 extern Bblock* tree_to_block(tree);
466 extern tree type_to_tree(Btype*);
467 extern tree expr_to_tree(Bexpression*);
468 extern tree stat_to_tree(Bstatement*);
469 extern tree block_to_tree(Bblock*);
470 extern tree var_to_tree(Bvariable*);
471
472 #endif // !defined(GO_BACKEND_H)