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 #include <gmp.h> 11 #include <mpfr.h> 12 #include <mpc.h> 13 14 #include "operator.h" 15 16 // Pointers to these types are created by the backend, passed to the 17 // frontend, and passed back to the backend. The types must be 18 // defined by the backend using these names. 19 20 // The backend representation of a type. 21 class Btype; 22 23 // The backend represention of an expression. 24 class Bexpression; 25 26 // The backend representation of a statement. 27 class Bstatement; 28 29 // The backend representation of a function definition or declaration. 30 class Bfunction; 31 32 // The backend representation of a block. 33 class Bblock; 34 35 // The backend representation of a variable. 36 class Bvariable; 37 38 // The backend representation of a label. 39 class Blabel; 40 41 // The backend interface. This is a pure abstract class that a 42 // specific backend will implement. 43 44 class Backend 45 { 46 public: ~Backend()47 virtual ~Backend() { } 48 49 // Name/type/location. Used for function parameters, struct fields, 50 // interface methods. 51 struct Btyped_identifier 52 { 53 std::string name; 54 Btype* btype; 55 Location location; 56 Btyped_identifierBtyped_identifier57 Btyped_identifier() 58 : name(), btype(NULL), location(Linemap::unknown_location()) 59 { } 60 Btyped_identifierBtyped_identifier61 Btyped_identifier(const std::string& a_name, Btype* a_btype, 62 Location a_location) 63 : name(a_name), btype(a_btype), location(a_location) 64 { } 65 }; 66 67 // Types. 68 69 // Produce an error type. Actually the backend could probably just 70 // crash if this is called. 71 virtual Btype* 72 error_type() = 0; 73 74 // Get a void type. This is used in (at least) two ways: 1) as the 75 // return type of a function with no result parameters; 2) 76 // unsafe.Pointer is represented as *void. 77 virtual Btype* 78 void_type() = 0; 79 80 // Get the unnamed boolean type. 81 virtual Btype* 82 bool_type() = 0; 83 84 // Get an unnamed integer type with the given signedness and number 85 // of bits. 86 virtual Btype* 87 integer_type(bool is_unsigned, int bits) = 0; 88 89 // Get an unnamed floating point type with the given number of bits 90 // (32 or 64). 91 virtual Btype* 92 float_type(int bits) = 0; 93 94 // Get an unnamed complex type with the given number of bits (64 or 128). 95 virtual Btype* 96 complex_type(int bits) = 0; 97 98 // Get a pointer type. 99 virtual Btype* 100 pointer_type(Btype* to_type) = 0; 101 102 // Get a function type. The receiver, parameter, and results are 103 // generated from the types in the Function_type. The Function_type 104 // is provided so that the names are available. This should return 105 // not the type of a Go function (which is a pointer to a struct) 106 // but the type of a C function pointer (which will be used as the 107 // type of the first field of the struct). If there is more than 108 // one result, RESULT_STRUCT is a struct type to hold the results, 109 // and RESULTS may be ignored; if there are zero or one results, 110 // RESULT_STRUCT is NULL. 111 virtual Btype* 112 function_type(const Btyped_identifier& receiver, 113 const std::vector<Btyped_identifier>& parameters, 114 const std::vector<Btyped_identifier>& results, 115 Btype* result_struct, 116 Location location) = 0; 117 118 // Get a struct type. 119 virtual Btype* 120 struct_type(const std::vector<Btyped_identifier>& fields) = 0; 121 122 // Get an array type. 123 virtual Btype* 124 array_type(Btype* element_type, Bexpression* length) = 0; 125 126 // Create a placeholder pointer type. This is used for a named 127 // pointer type, since in Go a pointer type may refer to itself. 128 // NAME is the name of the type, and the location is where the named 129 // type is defined. This function is also used for unnamed function 130 // types with multiple results, in which case the type has no name 131 // and NAME will be empty. FOR_FUNCTION is true if this is for a C 132 // pointer to function type. A Go func type is represented as a 133 // pointer to a struct, and the first field of the struct is a C 134 // pointer to function. The return value will later be passed as 135 // the first parameter to set_placeholder_pointer_type or 136 // set_placeholder_function_type. 137 virtual Btype* 138 placeholder_pointer_type(const std::string& name, Location, 139 bool for_function) = 0; 140 141 // Fill in a placeholder pointer type as a pointer. This takes a 142 // type returned by placeholder_pointer_type and arranges for it to 143 // point to the type that TO_TYPE points to (that is, PLACEHOLDER 144 // becomes the same type as TO_TYPE). Returns true on success, 145 // false on failure. 146 virtual bool 147 set_placeholder_pointer_type(Btype* placeholder, Btype* to_type) = 0; 148 149 // Fill in a placeholder pointer type as a function. This takes a 150 // type returned by placeholder_pointer_type and arranges for it to 151 // become a real Go function type (which corresponds to a C/C++ 152 // pointer to function type). FT will be something returned by the 153 // function_type method. Returns true on success, false on failure. 154 virtual bool 155 set_placeholder_function_type(Btype* placeholder, Btype* ft) = 0; 156 157 // Create a placeholder struct type. This is used for a named 158 // struct type, as with placeholder_pointer_type. It is also used 159 // for interface types, in which case NAME will be the empty string. 160 virtual Btype* 161 placeholder_struct_type(const std::string& name, Location) = 0; 162 163 // Fill in a placeholder struct type. This takes a type returned by 164 // placeholder_struct_type and arranges for it to become a real 165 // struct type. The parameter is as for struct_type. Returns true 166 // on success, false on failure. 167 virtual bool 168 set_placeholder_struct_type(Btype* placeholder, 169 const std::vector<Btyped_identifier>& fields) 170 = 0; 171 172 // Create a placeholder array type. This is used for a named array 173 // type, as with placeholder_pointer_type, to handle cases like 174 // type A []*A. 175 virtual Btype* 176 placeholder_array_type(const std::string& name, Location) = 0; 177 178 // Fill in a placeholder array type. This takes a type returned by 179 // placeholder_array_type and arranges for it to become a real array 180 // type. The parameters are as for array_type. Returns true on 181 // success, false on failure. 182 virtual bool 183 set_placeholder_array_type(Btype* placeholder, Btype* element_type, 184 Bexpression* length) = 0; 185 186 // Return a named version of a type. The location is the location 187 // of the type definition. This will not be called for a type 188 // created via placeholder_pointer_type, placeholder_struct_type, or 189 // placeholder_array_type.. (It may be called for a pointer, 190 // struct, or array type in a case like "type P *byte; type Q P".) 191 virtual Btype* 192 named_type(const std::string& name, Btype*, Location) = 0; 193 194 // Create a marker for a circular pointer type. Go pointer and 195 // function types can refer to themselves in ways that are not 196 // permitted in C/C++. When a circular type is found, this function 197 // is called for the circular reference. This permits the backend 198 // to decide how to handle such a type. PLACEHOLDER is the 199 // placeholder type which has already been created; if the backend 200 // is prepared to handle a circular pointer type, it may simply 201 // return PLACEHOLDER. FOR_FUNCTION is true if this is for a 202 // function type. 203 // 204 // For "type P *P" the sequence of calls will be 205 // bt1 = placeholder_pointer_type(); 206 // bt2 = circular_pointer_type(bt1, false); 207 // set_placeholder_pointer_type(bt1, bt2); 208 virtual Btype* 209 circular_pointer_type(Btype* placeholder, bool for_function) = 0; 210 211 // Return whether the argument could be a special type created by 212 // circular_pointer_type. This is used to introduce explicit type 213 // conversions where needed. If circular_pointer_type returns its 214 // PLACEHOLDER parameter, this may safely always return false. 215 virtual bool 216 is_circular_pointer_type(Btype*) = 0; 217 218 // Return the size of a type. 219 virtual int64_t 220 type_size(Btype*) = 0; 221 222 // Return the alignment of a type. 223 virtual int64_t 224 type_alignment(Btype*) = 0; 225 226 // Return the alignment of a struct field of this type. This is 227 // normally the same as type_alignment, but not always. 228 virtual int64_t 229 type_field_alignment(Btype*) = 0; 230 231 // Return the offset of field INDEX in a struct type. INDEX is the 232 // entry in the FIELDS std::vector parameter of struct_type or 233 // set_placeholder_struct_type. 234 virtual int64_t 235 type_field_offset(Btype*, size_t index) = 0; 236 237 // Expressions. 238 239 // Return an expression for a zero value of the given type. This is 240 // used for cases such as local variable initialization and 241 // converting nil to other types. 242 virtual Bexpression* 243 zero_expression(Btype*) = 0; 244 245 // Create an error expression. This is used for cases which should 246 // not occur in a correct program, in order to keep the compilation 247 // going without crashing. 248 virtual Bexpression* 249 error_expression() = 0; 250 251 // Create a nil pointer expression. 252 virtual Bexpression* 253 nil_pointer_expression() = 0; 254 255 // Create a reference to a variable. 256 virtual Bexpression* 257 var_expression(Bvariable* var, Location) = 0; 258 259 // Create an expression that indirects through the pointer expression EXPR 260 // (i.e., return the expression for *EXPR). KNOWN_VALID is true if the pointer 261 // is known to point to a valid memory location. BTYPE is the expected type 262 // of the indirected EXPR. 263 virtual Bexpression* 264 indirect_expression(Btype* btype, Bexpression* expr, bool known_valid, 265 Location) = 0; 266 267 // Return an expression that declares a constant named NAME with the 268 // constant value VAL in BTYPE. 269 virtual Bexpression* 270 named_constant_expression(Btype* btype, const std::string& name, 271 Bexpression* val, Location) = 0; 272 273 // Return an expression for the multi-precision integer VAL in BTYPE. 274 virtual Bexpression* 275 integer_constant_expression(Btype* btype, mpz_t val) = 0; 276 277 // Return an expression for the floating point value VAL in BTYPE. 278 virtual Bexpression* 279 float_constant_expression(Btype* btype, mpfr_t val) = 0; 280 281 // Return an expression for the complex value VAL in BTYPE. 282 virtual Bexpression* 283 complex_constant_expression(Btype* btype, mpc_t val) = 0; 284 285 // Return an expression for the string value VAL. 286 virtual Bexpression* 287 string_constant_expression(const std::string& val) = 0; 288 289 // Return an expression for the boolean value VAL. 290 virtual Bexpression* 291 boolean_constant_expression(bool val) = 0; 292 293 // Return an expression for the real part of BCOMPLEX. 294 virtual Bexpression* 295 real_part_expression(Bexpression* bcomplex, Location) = 0; 296 297 // Return an expression for the imaginary part of BCOMPLEX. 298 virtual Bexpression* 299 imag_part_expression(Bexpression* bcomplex, Location) = 0; 300 301 // Return an expression for the complex number (BREAL, BIMAG). 302 virtual Bexpression* 303 complex_expression(Bexpression* breal, Bexpression* bimag, Location) = 0; 304 305 // Return an expression that converts EXPR to TYPE. 306 virtual Bexpression* 307 convert_expression(Btype* type, Bexpression* expr, Location) = 0; 308 309 // Create an expression for the address of a function. This is used to 310 // get the address of the code for a function. 311 virtual Bexpression* 312 function_code_expression(Bfunction*, Location) = 0; 313 314 // Create an expression that takes the address of an expression. 315 virtual Bexpression* 316 address_expression(Bexpression*, Location) = 0; 317 318 // Return an expression for the field at INDEX in BSTRUCT. 319 virtual Bexpression* 320 struct_field_expression(Bexpression* bstruct, size_t index, Location) = 0; 321 322 // Create an expression that executes BSTAT before BEXPR. 323 virtual Bexpression* 324 compound_expression(Bstatement* bstat, Bexpression* bexpr, Location) = 0; 325 326 // Return an expression that executes THEN_EXPR if CONDITION is true, or 327 // ELSE_EXPR otherwise and returns the result as type BTYPE, within the 328 // specified function FUNCTION. ELSE_EXPR may be NULL. BTYPE may be NULL. 329 virtual Bexpression* 330 conditional_expression(Bfunction* function, Btype* btype, 331 Bexpression* condition, Bexpression* then_expr, 332 Bexpression* else_expr, Location) = 0; 333 334 // Return an expression for the unary operation OP EXPR. 335 // Supported values of OP are (from operators.h): 336 // MINUS, NOT, XOR. 337 virtual Bexpression* 338 unary_expression(Operator op, Bexpression* expr, Location) = 0; 339 340 // Return an expression for the binary operation LEFT OP RIGHT. 341 // Supported values of OP are (from operators.h): 342 // EQEQ, NOTEQ, LT, LE, GT, GE, PLUS, MINUS, OR, XOR, MULT, DIV, MOD, 343 // LSHIFT, RSHIFT, AND, NOT. 344 virtual Bexpression* 345 binary_expression(Operator op, Bexpression* left, Bexpression* right, 346 Location) = 0; 347 348 // Return an expression that constructs BTYPE with VALS. BTYPE must be the 349 // backend representation a of struct. VALS must be in the same order as the 350 // corresponding fields in BTYPE. 351 virtual Bexpression* 352 constructor_expression(Btype* btype, const std::vector<Bexpression*>& vals, 353 Location) = 0; 354 355 // Return an expression that constructs an array of BTYPE with INDEXES and 356 // VALS. INDEXES and VALS must have the same amount of elements. Each index 357 // in INDEXES must be in the same order as the corresponding value in VALS. 358 virtual Bexpression* 359 array_constructor_expression(Btype* btype, 360 const std::vector<unsigned long>& indexes, 361 const std::vector<Bexpression*>& vals, 362 Location) = 0; 363 364 // Return an expression for the address of BASE[INDEX]. 365 // BASE has a pointer type. This is used for slice indexing. 366 virtual Bexpression* 367 pointer_offset_expression(Bexpression* base, Bexpression* index, 368 Location) = 0; 369 370 // Return an expression for ARRAY[INDEX] as an l-value. ARRAY is a valid 371 // fixed-length array, not a slice. 372 virtual Bexpression* 373 array_index_expression(Bexpression* array, Bexpression* index, Location) = 0; 374 375 // Create an expression for a call to FN with ARGS, taking place within 376 // caller CALLER. 377 virtual Bexpression* 378 call_expression(Bfunction *caller, Bexpression* fn, 379 const std::vector<Bexpression*>& args, 380 Bexpression* static_chain, Location) = 0; 381 382 // Return an expression that allocates SIZE bytes on the stack. 383 virtual Bexpression* 384 stack_allocation_expression(int64_t size, Location) = 0; 385 386 // Statements. 387 388 // Create an error statement. This is used for cases which should 389 // not occur in a correct program, in order to keep the compilation 390 // going without crashing. 391 virtual Bstatement* 392 error_statement() = 0; 393 394 // Create an expression statement within the specified function. 395 virtual Bstatement* 396 expression_statement(Bfunction*, Bexpression*) = 0; 397 398 // Create a variable initialization statement in the specified 399 // function. This initializes a local variable at the point in the 400 // program flow where it is declared. 401 virtual Bstatement* 402 init_statement(Bfunction*, Bvariable* var, Bexpression* init) = 0; 403 404 // Create an assignment statement within the specified function. 405 virtual Bstatement* 406 assignment_statement(Bfunction*, Bexpression* lhs, Bexpression* rhs, 407 Location) = 0; 408 409 // Create a return statement, passing the representation of the 410 // function and the list of values to return. 411 virtual Bstatement* 412 return_statement(Bfunction*, const std::vector<Bexpression*>&, 413 Location) = 0; 414 415 // Create an if statement within a function. ELSE_BLOCK may be NULL. 416 virtual Bstatement* 417 if_statement(Bfunction*, Bexpression* condition, 418 Bblock* then_block, Bblock* else_block, 419 Location) = 0; 420 421 // Create a switch statement where the case values are constants. 422 // CASES and STATEMENTS must have the same number of entries. If 423 // VALUE matches any of the list in CASES[i], which will all be 424 // integers, then STATEMENTS[i] is executed. STATEMENTS[i] will 425 // either end with a goto statement or will fall through into 426 // STATEMENTS[i + 1]. CASES[i] is empty for the default clause, 427 // which need not be last. FUNCTION is the current function. 428 virtual Bstatement* 429 switch_statement(Bfunction* function, Bexpression* value, 430 const std::vector<std::vector<Bexpression*> >& cases, 431 const std::vector<Bstatement*>& statements, 432 Location) = 0; 433 434 // Create a single statement from two statements. 435 virtual Bstatement* 436 compound_statement(Bstatement*, Bstatement*) = 0; 437 438 // Create a single statement from a list of statements. 439 virtual Bstatement* 440 statement_list(const std::vector<Bstatement*>&) = 0; 441 442 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if 443 // an exception occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and 444 // if not NULL, it will always be executed. This is used for handling defers 445 // in Go functions. In C++, the resulting code is of this form: 446 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; } 447 virtual Bstatement* 448 exception_handler_statement(Bstatement* bstat, Bstatement* except_stmt, 449 Bstatement* finally_stmt, Location) = 0; 450 451 // Blocks. 452 453 // Create a block. The frontend will call this function when it 454 // starts converting a block within a function. FUNCTION is the 455 // current function. ENCLOSING is the enclosing block; it will be 456 // NULL for the top-level block in a function. VARS is the list of 457 // local variables defined within this block; each entry will be 458 // created by the local_variable function. START_LOCATION is the 459 // location of the start of the block, more or less the location of 460 // the initial curly brace. END_LOCATION is the location of the end 461 // of the block, more or less the location of the final curly brace. 462 // The statements will be added after the block is created. 463 virtual Bblock* 464 block(Bfunction* function, Bblock* enclosing, 465 const std::vector<Bvariable*>& vars, 466 Location start_location, Location end_location) = 0; 467 468 // Add the statements to a block. The block is created first. Then 469 // the statements are created. Then the statements are added to the 470 // block. This will called exactly once per block. The vector may 471 // be empty if there are no statements. 472 virtual void 473 block_add_statements(Bblock*, const std::vector<Bstatement*>&) = 0; 474 475 // Return the block as a statement. This is used to include a block 476 // in a list of statements. 477 virtual Bstatement* 478 block_statement(Bblock*) = 0; 479 480 // Variables. 481 482 // Create an error variable. This is used for cases which should 483 // not occur in a correct program, in order to keep the compilation 484 // going without crashing. 485 virtual Bvariable* 486 error_variable() = 0; 487 488 // Create a global variable. NAME is the package-qualified name of 489 // the variable. ASM_NAME is the encoded identifier for the 490 // variable, incorporating the package, and made safe for the 491 // assembler. BTYPE is the type of the variable. IS_EXTERNAL is 492 // true if the variable is defined in some other package. IS_HIDDEN 493 // is true if the variable is not exported (name begins with a lower 494 // case letter). IN_UNIQUE_SECTION is true if the variable should 495 // be put into a unique section if possible; this is intended to 496 // permit the linker to garbage collect the variable if it is not 497 // referenced. LOCATION is where the variable was defined. 498 virtual Bvariable* 499 global_variable(const std::string& name, 500 const std::string& asm_name, 501 Btype* btype, 502 bool is_external, 503 bool is_hidden, 504 bool in_unique_section, 505 Location location) = 0; 506 507 // A global variable will 1) be initialized to zero, or 2) be 508 // initialized to a constant value, or 3) be initialized in the init 509 // function. In case 2, the frontend will call 510 // global_variable_set_init to set the initial value. If this is 511 // not called, the backend should initialize a global variable to 0. 512 // The init function may then assign a value to it. 513 virtual void 514 global_variable_set_init(Bvariable*, Bexpression*) = 0; 515 516 // Create a local variable. The frontend will create the local 517 // variables first, and then create the block which contains them. 518 // FUNCTION is the function in which the variable is defined. NAME 519 // is the name of the variable. TYPE is the type. DECL_VAR, if not 520 // null, gives the location at which the value of this variable may 521 // be found, typically used to create an inner-scope reference to an 522 // outer-scope variable, to extend the lifetime of the variable beyond 523 // the inner scope. IS_ADDRESS_TAKEN is true if the address of this 524 // variable is taken (this implies that the address does not escape 525 // the function, as otherwise the variable would be on the heap). 526 // LOCATION is where the variable is defined. For each local variable 527 // the frontend will call init_statement to set the initial value. 528 virtual Bvariable* 529 local_variable(Bfunction* function, const std::string& name, Btype* type, 530 Bvariable* decl_var, bool is_address_taken, Location location) = 0; 531 532 // Create a function parameter. This is an incoming parameter, not 533 // a result parameter (result parameters are treated as local 534 // variables). The arguments are as for local_variable. 535 virtual Bvariable* 536 parameter_variable(Bfunction* function, const std::string& name, 537 Btype* type, bool is_address_taken, 538 Location location) = 0; 539 540 // Create a static chain parameter. This is the closure parameter. 541 virtual Bvariable* 542 static_chain_variable(Bfunction* function, const std::string& name, 543 Btype* type, Location location) = 0; 544 545 // Create a temporary variable. A temporary variable has no name, 546 // just a type. We pass in FUNCTION and BLOCK in case they are 547 // needed. If INIT is not NULL, the variable should be initialized 548 // to that value. Otherwise the initial value is irrelevant--the 549 // backend does not have to explicitly initialize it to zero. 550 // ADDRESS_IS_TAKEN is true if the programs needs to take the 551 // address of this temporary variable. LOCATION is the location of 552 // the statement or expression which requires creating the temporary 553 // variable, and may not be very useful. This function should 554 // return a variable which can be referenced later and should set 555 // *PSTATEMENT to a statement which initializes the variable. 556 virtual Bvariable* 557 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression* init, 558 bool address_is_taken, Location location, 559 Bstatement** pstatement) = 0; 560 561 // Create an implicit variable that is compiler-defined. This is 562 // used when generating GC data and roots, when storing the values 563 // of a slice constructor, and for the zero value of types. This returns a 564 // Bvariable because it corresponds to an initialized variable in C. 565 // 566 // NAME is the name to use for the initialized variable this will create. 567 // 568 // ASM_NAME is encoded assembler-friendly version of the name, or the 569 // empty string if no encoding is needed. 570 // 571 // TYPE is the type of the implicit variable. 572 // 573 // IS_HIDDEN will be true if the descriptor should only be visible 574 // within the current object. 575 // 576 // IS_CONSTANT is true if the implicit variable should be treated like it is 577 // immutable. For slice initializers, if the values must be copied to the 578 // heap, the variable IS_CONSTANT. 579 // 580 // IS_COMMON is true if the implicit variable should 581 // be treated as a common variable (multiple definitions with 582 // different sizes permitted in different object files, all merged 583 // into the largest definition at link time); this will be true for 584 // the zero value. IS_HIDDEN and IS_COMMON will never both be true. 585 // 586 // If ALIGNMENT is not zero, it is the desired alignment of the variable. 587 virtual Bvariable* 588 implicit_variable(const std::string& name, const std::string& asm_name, 589 Btype* type, bool is_hidden, bool is_constant, 590 bool is_common, int64_t alignment) = 0; 591 592 593 // Set the initial value of a variable created by implicit_variable. 594 // This must be called even if there is no initializer, i.e., INIT is NULL. 595 // The NAME, TYPE, IS_HIDDEN, IS_CONSTANT, and IS_COMMON parameters are 596 // the same ones passed to implicit_variable. INIT will be a composite 597 // literal of type TYPE. It will not contain any function calls or anything 598 // else that can not be put into a read-only data section. 599 // It may contain the address of variables created by implicit_variable. 600 // 601 // If IS_COMMON is true, INIT will be NULL, and the 602 // variable should be initialized to all zeros. 603 virtual void 604 implicit_variable_set_init(Bvariable*, const std::string& name, Btype* type, 605 bool is_hidden, bool is_constant, bool is_common, 606 Bexpression* init) = 0; 607 608 // Create a reference to a named implicit variable defined in some 609 // other package. This will be a variable created by a call to 610 // implicit_variable with the same NAME, ASM_NAME and TYPE and with 611 // IS_COMMON passed as false. This corresponds to an extern global 612 // variable in C. 613 virtual Bvariable* 614 implicit_variable_reference(const std::string& name, 615 const std::string& asm_name, 616 Btype* type) = 0; 617 618 // Create a named immutable initialized data structure. This is 619 // used for type descriptors, map descriptors, and function 620 // descriptors. This returns a Bvariable because it corresponds to 621 // an initialized const variable in C. 622 // 623 // NAME is the name to use for the initialized global variable which 624 // this call will create. 625 // 626 // ASM_NAME is the encoded, assembler-friendly version of NAME, or 627 // the empty string if no encoding is needed. 628 // 629 // IS_HIDDEN will be true if the descriptor should only be visible 630 // within the current object. 631 // 632 // IS_COMMON is true if NAME may be defined by several packages, and 633 // the linker should merge all such definitions. If IS_COMMON is 634 // false, NAME should be defined in only one file. In general 635 // IS_COMMON will be true for the type descriptor of an unnamed type 636 // or a builtin type. IS_HIDDEN and IS_COMMON will never both be 637 // true. 638 // 639 // TYPE will be a struct type; the type of the returned expression 640 // must be a pointer to this struct type. 641 // 642 // We must create the named structure before we know its 643 // initializer, because the initializer may refer to its own 644 // address. After calling this the frontend will call 645 // immutable_struct_set_init. 646 virtual Bvariable* 647 immutable_struct(const std::string& name, 648 const std::string& asm_name, 649 bool is_hidden, bool is_common, 650 Btype* type, Location) = 0; 651 652 // Set the initial value of a variable created by immutable_struct. 653 // The NAME, IS_HIDDEN, IS_COMMON, TYPE, and location parameters are 654 // the same ones passed to immutable_struct. INITIALIZER will be a 655 // composite literal of type TYPE. It will not contain any function 656 // calls or anything else that can not be put into a read-only data 657 // section. It may contain the address of variables created by 658 // immutable_struct. 659 virtual void 660 immutable_struct_set_init(Bvariable*, const std::string& name, 661 bool is_hidden, bool is_common, Btype* type, 662 Location, Bexpression* initializer) = 0; 663 664 // Create a reference to a named immutable initialized data 665 // structure defined in some other package. This will be a 666 // structure created by a call to immutable_struct with the same 667 // NAME, ASM_NAME and TYPE and with IS_COMMON passed as false. This 668 // corresponds to an extern const global variable in C. 669 virtual Bvariable* 670 immutable_struct_reference(const std::string& name, 671 const std::string& asm_name, 672 Btype* type, Location) = 0; 673 674 // Labels. 675 676 // Create a new label. NAME will be empty if this is a label 677 // created by the frontend for a loop construct. The location is 678 // where the label is defined. 679 virtual Blabel* 680 label(Bfunction*, const std::string& name, Location) = 0; 681 682 // Create a statement which defines a label. This statement will be 683 // put into the codestream at the point where the label should be 684 // defined. 685 virtual Bstatement* 686 label_definition_statement(Blabel*) = 0; 687 688 // Create a goto statement to a label. 689 virtual Bstatement* 690 goto_statement(Blabel*, Location) = 0; 691 692 // Create an expression for the address of a label. This is used to 693 // get the return address of a deferred function which may call 694 // recover. 695 virtual Bexpression* 696 label_address(Blabel*, Location) = 0; 697 698 // Functions. 699 700 // Create an error function. This is used for cases which should 701 // not occur in a correct program, in order to keep the compilation 702 // going without crashing. 703 virtual Bfunction* 704 error_function() = 0; 705 706 // Declare or define a function of FNTYPE. 707 // NAME is the Go name of the function. ASM_NAME, if not the empty string, is 708 // the name that should be used in the symbol table; this will be non-empty if 709 // a magic extern comment is used. 710 // IS_VISIBLE is true if this function should be visible outside of the 711 // current compilation unit. IS_DECLARATION is true if this is a function 712 // declaration rather than a definition; the function definition will be in 713 // another compilation unit. 714 // IS_INLINABLE is true if the function can be inlined. 715 // DISABLE_SPLIT_STACK is true if this function may not split the stack; this 716 // is used for the implementation of recover. 717 // DOES_NOT_RETURN is true for a function that does not return; this is used 718 // for the implementation of panic. 719 // IN_UNIQUE_SECTION is true if this function should be put into a unique 720 // location if possible; this is used for field tracking. 721 virtual Bfunction* 722 function(Btype* fntype, const std::string& name, const std::string& asm_name, 723 bool is_visible, bool is_declaration, bool is_inlinable, 724 bool disable_split_stack, bool does_not_return, 725 bool in_unique_section, Location) = 0; 726 727 // Create a statement that runs all deferred calls for FUNCTION. This should 728 // be a statement that looks like this in C++: 729 // finish: 730 // try { DEFER_RETURN; } catch { CHECK_DEFER; goto finish; } 731 virtual Bstatement* 732 function_defer_statement(Bfunction* function, Bexpression* undefer, 733 Bexpression* check_defer, Location) = 0; 734 735 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION. 736 // This will only be called for a function definition. Returns true on 737 // success, false on failure. 738 virtual bool 739 function_set_parameters(Bfunction* function, 740 const std::vector<Bvariable*>& param_vars) = 0; 741 742 // Set the function body for FUNCTION using the code in CODE_STMT. Returns 743 // true on success, false on failure. 744 virtual bool 745 function_set_body(Bfunction* function, Bstatement* code_stmt) = 0; 746 747 // Look up a named built-in function in the current backend implementation. 748 // Returns NULL if no built-in function by that name exists. 749 virtual Bfunction* 750 lookup_builtin(const std::string&) = 0; 751 752 // Utility. 753 754 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS, 755 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally. 756 virtual void 757 write_global_definitions(const std::vector<Btype*>& type_decls, 758 const std::vector<Bexpression*>& constant_decls, 759 const std::vector<Bfunction*>& function_decls, 760 const std::vector<Bvariable*>& variable_decls) = 0; 761 762 // Write SIZE bytes of export data from BYTES to the proper 763 // section in the output object file. 764 virtual void 765 write_export_data(const char* bytes, unsigned int size) = 0; 766 }; 767 768 #endif // !defined(GO_BACKEND_H) 769