@@ -186,6 +186,7 @@ typedef enum _ir_type {
186
186
* num - number: argument number (PARAM)
187
187
* prb - branch probability 1-99 (0 - unspecified): (IF_TRUE, IF_FALSE, CASE_VAL, CASE_DEFAULT)
188
188
* opt - optional number
189
+ * pro - function prototype
189
190
*
190
191
* The order of IR opcodes is carefully selected for efficient folding.
191
192
* - foldable instruction go first
@@ -246,6 +247,7 @@ typedef enum _ir_type {
246
247
_ (INT2FP , d1 , def , ___ , ___ ) /* int to float conversion */ \
247
248
_ (FP2INT , d1 , def , ___ , ___ ) /* float to int conversion */ \
248
249
_ (FP2FP , d1 , def , ___ , ___ ) /* float to float conversion */ \
250
+ _ (PROTO , d1X1 , def , pro , ___ ) /* apply function prototype */ \
249
251
\
250
252
/* overflow-check */ \
251
253
_ (ADD_OV , d2C , def , def , ___ ) /* addition */ \
@@ -376,7 +378,6 @@ typedef int32_t ir_ref;
376
378
#define IR_CONSTS_LIMIT_MIN (-(IR_TRUE - 1))
377
379
#define IR_INSNS_LIMIT_MIN (IR_UNUSED + 1)
378
380
379
-
380
381
#ifndef IR_64
381
382
# define ADDR_MEMBER uintptr_t addr;
382
383
#else
@@ -395,6 +396,8 @@ typedef union _ir_val {
395
396
int32_t i32 ;
396
397
float f ;
397
398
ADDR_MEMBER
399
+ ir_ref name ;
400
+ ir_ref str ;
398
401
IR_STRUCT_LOHI (
399
402
union {
400
403
uint16_t u16 ;
@@ -417,12 +420,6 @@ typedef union _ir_val {
417
420
} ir_val ;
418
421
#undef ADDR_MEMBER
419
422
420
- /* IR constant flags */
421
- #define IR_CONST_EMIT (1<<0)
422
- #define IR_CONST_FASTCALL_FUNC (1<<1)
423
- #define IR_CONST_VARARG_FUNC (1<<2)
424
- #define IR_CONST_BUILTIN_FUNC (1<<3)
425
-
426
423
/* IR Instruction */
427
424
typedef struct _ir_insn {
428
425
IR_STRUCT_LOHI (
@@ -438,7 +435,7 @@ typedef struct _ir_insn {
438
435
union {
439
436
uint16_t inputs_count ; /* number of input control edges for MERGE, PHI, CALL, TAILCALL */
440
437
uint16_t prev_insn_offset ; /* 16-bit backward offset from current instruction for CSE */
441
- uint16_t const_flags ; /* flag to emit constant in rodat section */
438
+ uint16_t proto ;
442
439
}
443
440
);
444
441
uint32_t optx ;
@@ -482,38 +479,40 @@ ir_ref ir_strtab_lookup(ir_strtab *strtab, const char *str, uint32_t len, ir_ref
482
479
ir_ref ir_strtab_find (const ir_strtab * strtab , const char * str , uint32_t len );
483
480
ir_ref ir_strtab_update (ir_strtab * strtab , const char * str , uint32_t len , ir_ref val );
484
481
const char * ir_strtab_str (const ir_strtab * strtab , ir_ref idx );
482
+ const char * ir_strtab_strl (const ir_strtab * strtab , ir_ref idx , size_t * len );
485
483
void ir_strtab_apply (const ir_strtab * strtab , ir_strtab_apply_t func );
486
484
void ir_strtab_free (ir_strtab * strtab );
487
485
488
486
/* IR Context Flags */
489
487
#define IR_FUNCTION (1<<0) /* Generate a function. */
490
488
#define IR_FASTCALL_FUNC (1<<1) /* Generate a function with fastcall calling convention, x86 32-bit only. */
491
489
#define IR_VARARG_FUNC (1<<2)
492
- #define IR_STATIC (1<<3)
493
- #define IR_EXTERN (1<<4)
494
- #define IR_CONST (1<<5)
495
-
496
- #define IR_SKIP_PROLOGUE (1<<6) /* Don't generate function prologue. */
497
- #define IR_USE_FRAME_POINTER (1<<7)
498
- #define IR_PREALLOCATED_STACK (1<<8)
499
- #define IR_NO_STACK_COMBINE (1<<9)
500
- #define IR_START_BR_TARGET (1<<10)
501
- #define IR_ENTRY_BR_TARGET (1<<11)
502
- #define IR_GEN_ENDBR (1<<12)
503
- #define IR_MERGE_EMPTY_ENTRIES (1<<13)
504
-
505
- #define IR_OPT_FOLDING (1<<18)
506
- #define IR_OPT_CFG (1<<19) /* merge BBs, by remove END->BEGIN nodes during CFG construction */
507
- #define IR_OPT_CODEGEN (1<<20)
508
- #define IR_GEN_NATIVE (1<<23)
509
- #define IR_GEN_CODE (1<<24) /* C or LLVM */
490
+ #define IR_BUILTIN_FUNC (1<<3)
491
+ #define IR_STATIC (1<<4)
492
+ #define IR_EXTERN (1<<5)
493
+ #define IR_CONST (1<<6)
494
+
495
+ #define IR_SKIP_PROLOGUE (1<<8) /* Don't generate function prologue. */
496
+ #define IR_USE_FRAME_POINTER (1<<9)
497
+ #define IR_PREALLOCATED_STACK (1<<10)
498
+ #define IR_NO_STACK_COMBINE (1<<11)
499
+ #define IR_START_BR_TARGET (1<<12)
500
+ #define IR_ENTRY_BR_TARGET (1<<13)
501
+ #define IR_GEN_ENDBR (1<<14)
502
+ #define IR_MERGE_EMPTY_ENTRIES (1<<15)
503
+
504
+ #define IR_OPT_FOLDING (1<<16)
505
+ #define IR_OPT_CFG (1<<17) /* merge BBs, by remove END->BEGIN nodes during CFG construction */
506
+ #define IR_OPT_CODEGEN (1<<18)
507
+ #define IR_GEN_NATIVE (1<<19)
508
+ #define IR_GEN_CODE (1<<20) /* C or LLVM */
510
509
511
510
/* debug related */
512
511
#ifdef IR_DEBUG
513
- # define IR_DEBUG_SCCP (1<<27)
514
- # define IR_DEBUG_GCM (1<<28)
515
- # define IR_DEBUG_SCHEDULE (1<<29)
516
- # define IR_DEBUG_RA (1<<30)
512
+ # define IR_DEBUG_SCCP (1<<27)
513
+ # define IR_DEBUG_GCM (1<<28)
514
+ # define IR_DEBUG_SCHEDULE (1<<29)
515
+ # define IR_DEBUG_RA (1<<30)
517
516
#endif
518
517
519
518
typedef struct _ir_ctx ir_ctx ;
@@ -627,9 +626,9 @@ ir_ref ir_const_char(ir_ctx *ctx, char c);
627
626
ir_ref ir_const_float (ir_ctx * ctx , float c );
628
627
ir_ref ir_const_double (ir_ctx * ctx , double c );
629
628
ir_ref ir_const_addr (ir_ctx * ctx , uintptr_t c );
630
- ir_ref ir_const_func_addr (ir_ctx * ctx , uintptr_t c , uint16_t flags );
631
629
632
- ir_ref ir_const_func (ir_ctx * ctx , ir_ref str , uint16_t flags );
630
+ ir_ref ir_const_func_addr (ir_ctx * ctx , uintptr_t c , ir_ref proto );
631
+ ir_ref ir_const_func (ir_ctx * ctx , ir_ref str , ir_ref proto );
633
632
ir_ref ir_const_sym (ir_ctx * ctx , ir_ref str );
634
633
ir_ref ir_const_str (ir_ctx * ctx , ir_ref str );
635
634
@@ -640,6 +639,26 @@ void ir_print_const(const ir_ctx *ctx, const ir_insn *insn, FILE *f, bool quoted
640
639
ir_ref ir_str (ir_ctx * ctx , const char * s );
641
640
ir_ref ir_strl (ir_ctx * ctx , const char * s , size_t len );
642
641
const char * ir_get_str (const ir_ctx * ctx , ir_ref idx );
642
+ const char * ir_get_strl (const ir_ctx * ctx , ir_ref idx , size_t * len );
643
+
644
+ #define IR_MAX_PROTO_PARAMS 255
645
+
646
+ typedef struct _ir_proto_t {
647
+ uint8_t flags ;
648
+ uint8_t ret_type ;
649
+ uint8_t params_count ;
650
+ uint8_t param_types [5 ];
651
+ } ir_proto_t ;
652
+
653
+ ir_ref ir_proto_0 (ir_ctx * ctx , uint8_t flags , ir_type ret_type );
654
+ ir_ref ir_proto_1 (ir_ctx * ctx , uint8_t flags , ir_type ret_type , ir_type t1 );
655
+ ir_ref ir_proto_2 (ir_ctx * ctx , uint8_t flags , ir_type ret_type , ir_type t1 , ir_type t2 );
656
+ ir_ref ir_proto_3 (ir_ctx * ctx , uint8_t flags , ir_type ret_type , ir_type t1 , ir_type t2 , ir_type t3 );
657
+ ir_ref ir_proto_4 (ir_ctx * ctx , uint8_t flags , ir_type ret_type , ir_type t1 , ir_type t2 , ir_type t3 ,
658
+ ir_type t4 );
659
+ ir_ref ir_proto_5 (ir_ctx * ctx , uint8_t flags , ir_type ret_type , ir_type t1 , ir_type t2 , ir_type t3 ,
660
+ ir_type t4 , ir_type t5 );
661
+ ir_ref ir_proto (ir_ctx * ctx , uint8_t flags , ir_type ret_type , uint32_t params_counts , uint8_t * param_types );
643
662
644
663
ir_ref ir_emit (ir_ctx * ctx , uint32_t opt , ir_ref op1 , ir_ref op2 , ir_ref op3 );
645
664
@@ -768,9 +787,9 @@ struct _ir_loader {
768
787
bool (* init_module ) (ir_loader * loader , const char * name , const char * filename , const char * target );
769
788
bool (* external_sym_dcl ) (ir_loader * loader , const char * name , uint32_t flags );
770
789
bool (* external_func_dcl ) (ir_loader * loader , const char * name ,
771
- uint32_t flags , ir_type ret_type , uint32_t params_count , ir_type * param_types );
790
+ uint32_t flags , ir_type ret_type , uint32_t params_count , const uint8_t * param_types );
772
791
bool (* forward_func_dcl ) (ir_loader * loader , const char * name ,
773
- uint32_t flags , ir_type ret_type , uint32_t params_count , ir_type * param_types );
792
+ uint32_t flags , ir_type ret_type , uint32_t params_count , const uint8_t * param_types );
774
793
bool (* sym_dcl ) (ir_loader * loader , const char * name , uint32_t flags , size_t size , bool has_data );
775
794
bool (* sym_data ) (ir_loader * loader , ir_type type , uint32_t count , const void * data );
776
795
bool (* sym_data_ref ) (ir_loader * loader , ir_op op , const char * ref );
@@ -789,6 +808,7 @@ int ir_load_llvm_bitcode(ir_loader *loader, const char *filename);
789
808
int ir_load_llvm_asm (ir_loader * loader , const char * filename );
790
809
791
810
/* IR save API (implementation in ir_save.c) */
811
+ void ir_print_proto (const ir_ctx * ctx , ir_ref proto , FILE * f );
792
812
void ir_save (const ir_ctx * ctx , FILE * f );
793
813
794
814
/* IR debug dump API (implementation in ir_dump.c) */
@@ -802,11 +822,11 @@ void ir_dump_codegen(const ir_ctx *ctx, FILE *f);
802
822
803
823
/* IR to C conversion (implementation in ir_emit_c.c) */
804
824
int ir_emit_c (ir_ctx * ctx , const char * name , FILE * f );
805
- void ir_emit_c_func_decl (const char * name , uint32_t flags , ir_type ret_type , uint32_t params_count , ir_type * param_types , FILE * f );
825
+ void ir_emit_c_func_decl (const char * name , uint32_t flags , ir_type ret_type , uint32_t params_count , const uint8_t * param_types , FILE * f );
806
826
807
827
/* IR to LLVM conversion (implementation in ir_emit_llvm.c) */
808
828
int ir_emit_llvm (ir_ctx * ctx , const char * name , FILE * f );
809
- void ir_emit_llvm_func_decl (const char * name , uint32_t flags , ir_type ret_type , uint32_t params_count , ir_type * param_types , FILE * f );
829
+ void ir_emit_llvm_func_decl (const char * name , uint32_t flags , ir_type ret_type , uint32_t params_count , const uint8_t * param_types , FILE * f );
810
830
811
831
/* IR verification API (implementation in ir_check.c) */
812
832
bool ir_check (const ir_ctx * ctx );
0 commit comments