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: ~Backend()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 Btyped_identifierBtyped_identifier51 Btyped_identifier() 52 : name(), btype(NULL), location(UNKNOWN_LOCATION) 53 { } 54 Btyped_identifierBtyped_identifier55 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 the type that TO_TYPE points to (that is, PLACEHOLDER 130 // becomes the same type as TO_TYPE). Returns true on success, 131 // false on failure. 132 virtual bool 133 set_placeholder_pointer_type(Btype* placeholder, Btype* to_type) = 0; 134 135 // Fill in a placeholder pointer type as a function. This takes a 136 // type returned by placeholder_pointer_type and arranges for it to 137 // become a real Go function type (which corresponds to a C/C++ 138 // pointer to function type). FT will be something returned by the 139 // function_type method. Returns true on success, false on failure. 140 virtual bool 141 set_placeholder_function_type(Btype* placeholder, Btype* ft) = 0; 142 143 // Create a placeholder struct type. This is used for a named 144 // struct type, as with placeholder_pointer_type. It is also used 145 // for interface types, in which case NAME will be the empty string. 146 virtual Btype* 147 placeholder_struct_type(const std::string& name, Location) = 0; 148 149 // Fill in a placeholder struct type. This takes a type returned by 150 // placeholder_struct_type and arranges for it to become a real 151 // struct type. The parameter is as for struct_type. Returns true 152 // on success, false on failure. 153 virtual bool 154 set_placeholder_struct_type(Btype* placeholder, 155 const std::vector<Btyped_identifier>& fields) 156 = 0; 157 158 // Create a placeholder array type. This is used for a named array 159 // type, as with placeholder_pointer_type, to handle cases like 160 // type A []*A. 161 virtual Btype* 162 placeholder_array_type(const std::string& name, Location) = 0; 163 164 // Fill in a placeholder array type. This takes a type returned by 165 // placeholder_array_type and arranges for it to become a real array 166 // type. The parameters are as for array_type. Returns true on 167 // success, false on failure. 168 virtual bool 169 set_placeholder_array_type(Btype* placeholder, Btype* element_type, 170 Bexpression* length) = 0; 171 172 // Return a named version of a type. The location is the location 173 // of the type definition. This will not be called for a type 174 // created via placeholder_pointer_type, placeholder_struct_type, or 175 // placeholder_array_type.. (It may be called for a pointer, 176 // struct, or array type in a case like "type P *byte; type Q P".) 177 virtual Btype* 178 named_type(const std::string& name, Btype*, Location) = 0; 179 180 // Create a marker for a circular pointer type. Go pointer and 181 // function types can refer to themselves in ways that are not 182 // permitted in C/C++. When a circular type is found, this function 183 // is called for the circular reference. This permits the backend 184 // to decide how to handle such a type. PLACEHOLDER is the 185 // placeholder type which has already been created; if the backend 186 // is prepared to handle a circular pointer type, it may simply 187 // return PLACEHOLDER. FOR_FUNCTION is true if this is for a 188 // function type. 189 // 190 // For "type P *P" the sequence of calls will be 191 // bt1 = placeholder_pointer_type(); 192 // bt2 = circular_pointer_type(bt1, false); 193 // set_placeholder_pointer_type(bt1, bt2); 194 virtual Btype* 195 circular_pointer_type(Btype* placeholder, bool for_function) = 0; 196 197 // Return whether the argument could be a special type created by 198 // circular_pointer_type. This is used to introduce explicit type 199 // conversions where needed. If circular_pointer_type returns its 200 // PLACEHOLDER parameter, this may safely always return false. 201 virtual bool 202 is_circular_pointer_type(Btype*) = 0; 203 204 // Return the size of a type. 205 virtual size_t 206 type_size(Btype*) = 0; 207 208 // Return the alignment of a type. 209 virtual size_t 210 type_alignment(Btype*) = 0; 211 212 // Return the alignment of a struct field of this type. This is 213 // normally the same as type_alignment, but not always. 214 virtual size_t 215 type_field_alignment(Btype*) = 0; 216 217 // Return the offset of field INDEX in a struct type. INDEX is the 218 // entry in the FIELDS std::vector parameter of struct_type or 219 // set_placeholder_struct_type. 220 virtual size_t 221 type_field_offset(Btype*, size_t index) = 0; 222 223 // Expressions. 224 225 // Return an expression for a zero value of the given type. This is 226 // used for cases such as local variable initialization and 227 // converting nil to other types. 228 virtual Bexpression* 229 zero_expression(Btype*) = 0; 230 231 // Statements. 232 233 // Create an error statement. This is used for cases which should 234 // not occur in a correct program, in order to keep the compilation 235 // going without crashing. 236 virtual Bstatement* 237 error_statement() = 0; 238 239 // Create an expression statement. 240 virtual Bstatement* 241 expression_statement(Bexpression*) = 0; 242 243 // Create a variable initialization statement. This initializes a 244 // local variable at the point in the program flow where it is 245 // declared. 246 virtual Bstatement* 247 init_statement(Bvariable* var, Bexpression* init) = 0; 248 249 // Create an assignment statement. 250 virtual Bstatement* 251 assignment_statement(Bexpression* lhs, Bexpression* rhs, 252 Location) = 0; 253 254 // Create a return statement, passing the representation of the 255 // function and the list of values to return. 256 virtual Bstatement* 257 return_statement(Bfunction*, const std::vector<Bexpression*>&, 258 Location) = 0; 259 260 // Create an if statement. ELSE_BLOCK may be NULL. 261 virtual Bstatement* 262 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block, 263 Location) = 0; 264 265 // Create a switch statement where the case values are constants. 266 // CASES and STATEMENTS must have the same number of entries. If 267 // VALUE matches any of the list in CASES[i], which will all be 268 // integers, then STATEMENTS[i] is executed. STATEMENTS[i] will 269 // either end with a goto statement or will fall through into 270 // STATEMENTS[i + 1]. CASES[i] is empty for the default clause, 271 // which need not be last. 272 virtual Bstatement* 273 switch_statement(Bexpression* value, 274 const std::vector<std::vector<Bexpression*> >& cases, 275 const std::vector<Bstatement*>& statements, 276 Location) = 0; 277 278 // Create a single statement from two statements. 279 virtual Bstatement* 280 compound_statement(Bstatement*, Bstatement*) = 0; 281 282 // Create a single statement from a list of statements. 283 virtual Bstatement* 284 statement_list(const std::vector<Bstatement*>&) = 0; 285 286 // Blocks. 287 288 // Create a block. The frontend will call this function when it 289 // starts converting a block within a function. FUNCTION is the 290 // current function. ENCLOSING is the enclosing block; it will be 291 // NULL for the top-level block in a function. VARS is the list of 292 // local variables defined within this block; each entry will be 293 // created by the local_variable function. START_LOCATION is the 294 // location of the start of the block, more or less the location of 295 // the initial curly brace. END_LOCATION is the location of the end 296 // of the block, more or less the location of the final curly brace. 297 // The statements will be added after the block is created. 298 virtual Bblock* 299 block(Bfunction* function, Bblock* enclosing, 300 const std::vector<Bvariable*>& vars, 301 Location start_location, Location end_location) = 0; 302 303 // Add the statements to a block. The block is created first. Then 304 // the statements are created. Then the statements are added to the 305 // block. This will called exactly once per block. The vector may 306 // be empty if there are no statements. 307 virtual void 308 block_add_statements(Bblock*, const std::vector<Bstatement*>&) = 0; 309 310 // Return the block as a statement. This is used to include a block 311 // in a list of statements. 312 virtual Bstatement* 313 block_statement(Bblock*) = 0; 314 315 // Variables. 316 317 // Create an error variable. This is used for cases which should 318 // not occur in a correct program, in order to keep the compilation 319 // going without crashing. 320 virtual Bvariable* 321 error_variable() = 0; 322 323 // Create a global variable. PACKAGE_NAME is the name of the 324 // package where the variable is defined. PKGPATH is the package 325 // path for that package, from the -fgo-pkgpath or -fgo-prefix 326 // option. NAME is the name of the variable. BTYPE is the type of 327 // the variable. IS_EXTERNAL is true if the variable is defined in 328 // some other package. IS_HIDDEN is true if the variable is not 329 // exported (name begins with a lower case letter). 330 // IN_UNIQUE_SECTION is true if the variable should be put into a 331 // unique section if possible; this is intended to permit the linker 332 // to garbage collect the variable if it is not referenced. 333 // LOCATION is where the variable was defined. 334 virtual Bvariable* 335 global_variable(const std::string& package_name, 336 const std::string& pkgpath, 337 const std::string& name, 338 Btype* btype, 339 bool is_external, 340 bool is_hidden, 341 bool in_unique_section, 342 Location location) = 0; 343 344 // A global variable will 1) be initialized to zero, or 2) be 345 // initialized to a constant value, or 3) be initialized in the init 346 // function. In case 2, the frontend will call 347 // global_variable_set_init to set the initial value. If this is 348 // not called, the backend should initialize a global variable to 0. 349 // The init function may then assign a value to it. 350 virtual void 351 global_variable_set_init(Bvariable*, Bexpression*) = 0; 352 353 // Create a local variable. The frontend will create the local 354 // variables first, and then create the block which contains them. 355 // FUNCTION is the function in which the variable is defined. NAME 356 // is the name of the variable. TYPE is the type. IS_ADDRESS_TAKEN 357 // is true if the address of this variable is taken (this implies 358 // that the address does not escape the function, as otherwise the 359 // variable would be on the heap). LOCATION is where the variable 360 // is defined. For each local variable the frontend will call 361 // init_statement to set the initial value. 362 virtual Bvariable* 363 local_variable(Bfunction* function, const std::string& name, Btype* type, 364 bool is_address_taken, Location location) = 0; 365 366 // Create a function parameter. This is an incoming parameter, not 367 // a result parameter (result parameters are treated as local 368 // variables). The arguments are as for local_variable. 369 virtual Bvariable* 370 parameter_variable(Bfunction* function, const std::string& name, 371 Btype* type, bool is_address_taken, 372 Location location) = 0; 373 374 // Create a temporary variable. A temporary variable has no name, 375 // just a type. We pass in FUNCTION and BLOCK in case they are 376 // needed. If INIT is not NULL, the variable should be initialized 377 // to that value. Otherwise the initial value is irrelevant--the 378 // backend does not have to explicitly initialize it to zero. 379 // ADDRESS_IS_TAKEN is true if the programs needs to take the 380 // address of this temporary variable. LOCATION is the location of 381 // the statement or expression which requires creating the temporary 382 // variable, and may not be very useful. This function should 383 // return a variable which can be referenced later and should set 384 // *PSTATEMENT to a statement which initializes the variable. 385 virtual Bvariable* 386 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression* init, 387 bool address_is_taken, Location location, 388 Bstatement** pstatement) = 0; 389 390 // Create a named immutable initialized data structure. This is 391 // used for type descriptors and map descriptors. This returns a 392 // Bvariable because it corresponds to an initialized const global 393 // variable in C. 394 // 395 // NAME is the name to use for the initialized global variable which 396 // this call will create. 397 // 398 // IS_COMMON is true if NAME may be defined by several packages, and 399 // the linker should merge all such definitions. If IS_COMMON is 400 // false, NAME should be defined in only one file. In general 401 // IS_COMMON will be true for the type descriptor of an unnamed type 402 // or a builtin type. 403 // 404 // TYPE will be a struct type; the type of the returned expression 405 // must be a pointer to this struct type. 406 // 407 // We must create the named structure before we know its 408 // initializer, because the initializer may refer to its own 409 // address. After calling this the frontend will call 410 // immutable_struct_set_init. 411 virtual Bvariable* 412 immutable_struct(const std::string& name, bool is_common, Btype* type, 413 Location) = 0; 414 415 // Set the initial value of a variable created by immutable_struct. 416 // The NAME, IS_COMMON, TYPE, and location parameters are the same 417 // ones passed to immutable_struct. INITIALIZER will be a composite 418 // literal of type TYPE. It will not contain any function calls or 419 // anything else which can not be put into a read-only data section. 420 // It may contain the address of variables created by 421 // immutable_struct. 422 virtual void 423 immutable_struct_set_init(Bvariable*, const std::string& name, 424 bool is_common, Btype* type, Location, 425 Bexpression* initializer) = 0; 426 427 // Create a reference to a named immutable initialized data 428 // structure defined in some other package. This will be a 429 // structure created by a call to immutable_struct with the same 430 // NAME and TYPE and with IS_COMMON passed as false. This 431 // corresponds to an extern const global variable in C. 432 virtual Bvariable* 433 immutable_struct_reference(const std::string& name, Btype* type, 434 Location) = 0; 435 436 // Labels. 437 438 // Create a new label. NAME will be empty if this is a label 439 // created by the frontend for a loop construct. The location is 440 // where the the label is defined. 441 virtual Blabel* 442 label(Bfunction*, const std::string& name, Location) = 0; 443 444 // Create a statement which defines a label. This statement will be 445 // put into the codestream at the point where the label should be 446 // defined. 447 virtual Bstatement* 448 label_definition_statement(Blabel*) = 0; 449 450 // Create a goto statement to a label. 451 virtual Bstatement* 452 goto_statement(Blabel*, Location) = 0; 453 454 // Create an expression for the address of a label. This is used to 455 // get the return address of a deferred function which may call 456 // recover. 457 virtual Bexpression* 458 label_address(Blabel*, Location) = 0; 459 }; 460 461 // The backend interface has to define this function. 462 463 extern Backend* go_get_backend(); 464 465 // FIXME: Temporary helper functions while converting to new backend 466 // interface. 467 468 extern Btype* tree_to_type(tree); 469 extern Bexpression* tree_to_expr(tree); 470 extern Bstatement* tree_to_stat(tree); 471 extern Bfunction* tree_to_function(tree); 472 extern Bblock* tree_to_block(tree); 473 extern tree type_to_tree(Btype*); 474 extern tree expr_to_tree(Bexpression*); 475 extern tree stat_to_tree(Bstatement*); 476 extern tree block_to_tree(Bblock*); 477 extern tree var_to_tree(Bvariable*); 478 479 #endif // !defined(GO_BACKEND_H) 480