builtins.go raw

   1  package builder
   2  
   3  import (
   4  	"os"
   5  	"path/filepath"
   6  	"strings"
   7  
   8  	"moxie/compileopts"
   9  	"moxie/goenv"
  10  )
  11  
  12  // These are the GENERIC_SOURCES according to CMakeList.txt except for
  13  // divmodsi4.c and udivmodsi4.c.
  14  var genericBuiltins = []string{
  15  	"absvdi2.c",
  16  	"absvsi2.c",
  17  	"absvti2.c",
  18  	"adddf3.c",
  19  	"addsf3.c",
  20  	"addvdi3.c",
  21  	"addvsi3.c",
  22  	"addvti3.c",
  23  	"apple_versioning.c",
  24  	"ashldi3.c",
  25  	"ashlti3.c",
  26  	"ashrdi3.c",
  27  	"ashrti3.c",
  28  	"bswapdi2.c",
  29  	"bswapsi2.c",
  30  	"clzdi2.c",
  31  	"clzsi2.c",
  32  	"clzti2.c",
  33  	"cmpdi2.c",
  34  	"cmpti2.c",
  35  	"comparedf2.c",
  36  	"comparesf2.c",
  37  	"ctzdi2.c",
  38  	"ctzsi2.c",
  39  	"ctzti2.c",
  40  	"divdc3.c",
  41  	"divdf3.c",
  42  	"divdi3.c",
  43  	"divmoddi4.c",
  44  	//"divmodsi4.c",
  45  	"divmodti4.c",
  46  	"divsc3.c",
  47  	"divsf3.c",
  48  	"divsi3.c",
  49  	"divti3.c",
  50  	"extendsfdf2.c",
  51  	"extendhfsf2.c",
  52  	"ffsdi2.c",
  53  	"ffssi2.c",
  54  	"ffsti2.c",
  55  	"fixdfdi.c",
  56  	"fixdfsi.c",
  57  	"fixdfti.c",
  58  	"fixsfdi.c",
  59  	"fixsfsi.c",
  60  	"fixsfti.c",
  61  	"fixunsdfdi.c",
  62  	"fixunsdfsi.c",
  63  	"fixunsdfti.c",
  64  	"fixunssfdi.c",
  65  	"fixunssfsi.c",
  66  	"fixunssfti.c",
  67  	"floatdidf.c",
  68  	"floatdisf.c",
  69  	"floatsidf.c",
  70  	"floatsisf.c",
  71  	"floattidf.c",
  72  	"floattisf.c",
  73  	"floatundidf.c",
  74  	"floatundisf.c",
  75  	"floatunsidf.c",
  76  	"floatunsisf.c",
  77  	"floatuntidf.c",
  78  	"floatuntisf.c",
  79  	"fp_mode.c",
  80  	//"int_util.c",
  81  	"lshrdi3.c",
  82  	"lshrti3.c",
  83  	"moddi3.c",
  84  	"modsi3.c",
  85  	"modti3.c",
  86  	"muldc3.c",
  87  	"muldf3.c",
  88  	"muldi3.c",
  89  	"mulodi4.c",
  90  	"mulosi4.c",
  91  	"muloti4.c",
  92  	"mulsc3.c",
  93  	"mulsf3.c",
  94  	"multi3.c",
  95  	"mulvdi3.c",
  96  	"mulvsi3.c",
  97  	"mulvti3.c",
  98  	"negdf2.c",
  99  	"negdi2.c",
 100  	"negsf2.c",
 101  	"negti2.c",
 102  	"negvdi2.c",
 103  	"negvsi2.c",
 104  	"negvti2.c",
 105  	"os_version_check.c",
 106  	"paritydi2.c",
 107  	"paritysi2.c",
 108  	"parityti2.c",
 109  	"popcountdi2.c",
 110  	"popcountsi2.c",
 111  	"popcountti2.c",
 112  	"powidf2.c",
 113  	"powisf2.c",
 114  	"subdf3.c",
 115  	"subsf3.c",
 116  	"subvdi3.c",
 117  	"subvsi3.c",
 118  	"subvti3.c",
 119  	"trampoline_setup.c",
 120  	"truncdfhf2.c",
 121  	"truncdfsf2.c",
 122  	"truncsfhf2.c",
 123  	"ucmpdi2.c",
 124  	"ucmpti2.c",
 125  	"udivdi3.c",
 126  	"udivmoddi4.c",
 127  	//"udivmodsi4.c",
 128  	"udivmodti4.c",
 129  	"udivsi3.c",
 130  	"udivti3.c",
 131  	"umoddi3.c",
 132  	"umodsi3.c",
 133  	"umodti3.c",
 134  }
 135  
 136  // These are the GENERIC_TF_SOURCES as of LLVM 18.
 137  // They are not needed on all platforms (32-bit platforms usually don't need
 138  // these) but they seem to compile fine so it's easier to include them.
 139  var genericBuiltins128 = []string{
 140  	"addtf3.c",
 141  	"comparetf2.c",
 142  	"divtc3.c",
 143  	"divtf3.c",
 144  	"extenddftf2.c",
 145  	"extendhftf2.c",
 146  	"extendsftf2.c",
 147  	"fixtfdi.c",
 148  	"fixtfsi.c",
 149  	"fixtfti.c",
 150  	"fixunstfdi.c",
 151  	"fixunstfsi.c",
 152  	"fixunstfti.c",
 153  	"floatditf.c",
 154  	"floatsitf.c",
 155  	"floattitf.c",
 156  	"floatunditf.c",
 157  	"floatunsitf.c",
 158  	"floatuntitf.c",
 159  	"multc3.c",
 160  	"multf3.c",
 161  	"powitf2.c",
 162  	"subtf3.c",
 163  	"trunctfdf2.c",
 164  	"trunctfhf2.c",
 165  	"trunctfsf2.c",
 166  }
 167  
 168  var aeabiBuiltins = []string{
 169  	"arm/aeabi_cdcmp.S",
 170  	"arm/aeabi_cdcmpeq_check_nan.c",
 171  	"arm/aeabi_cfcmp.S",
 172  	"arm/aeabi_cfcmpeq_check_nan.c",
 173  	"arm/aeabi_dcmp.S",
 174  	"arm/aeabi_div0.c",
 175  	"arm/aeabi_drsub.c",
 176  	"arm/aeabi_fcmp.S",
 177  	"arm/aeabi_frsub.c",
 178  	"arm/aeabi_idivmod.S",
 179  	"arm/aeabi_ldivmod.S",
 180  	"arm/aeabi_memcmp.S",
 181  	"arm/aeabi_memcpy.S",
 182  	"arm/aeabi_memmove.S",
 183  	"arm/aeabi_memset.S",
 184  	"arm/aeabi_uidivmod.S",
 185  	"arm/aeabi_uldivmod.S",
 186  
 187  	// These two are not technically EABI builtins but are used by them and only
 188  	// seem to be used on ARM. LLVM seems to use __divsi3 and __modsi3 on most
 189  	// other architectures.
 190  	// Most importantly, they have a different calling convention on AVR so
 191  	// should not be used on AVR.
 192  	"divmodsi4.c",
 193  	"udivmodsi4.c",
 194  }
 195  
 196  var avrBuiltins = []string{
 197  	"avr/divmodhi4.S",
 198  	"avr/divmodqi4.S",
 199  	"avr/mulhi3.S",
 200  	"avr/mulqi3.S",
 201  	"avr/udivmodhi4.S",
 202  	"avr/udivmodqi4.S",
 203  }
 204  
 205  // Builtins needed specifically for windows/386.
 206  var windowsI386Builtins = []string{
 207  	"i386/chkstk.S", // also _alloca
 208  }
 209  
 210  // libCompilerRT is a library with symbols required by programs compiled with
 211  // LLVM. These symbols are for operations that cannot be emitted with a single
 212  // instruction or a short sequence of instructions for that target.
 213  //
 214  // For more information, see: https://compiler-rt.llvm.org/
 215  var libCompilerRT = Library{
 216  	name: "compiler-rt",
 217  	cflags: func(target, headerPath string) []string {
 218  		return []string{"-Werror", "-Wall", "-std=c11", "-nostdlibinc"}
 219  	},
 220  	sourceDir: func() string {
 221  		llvmDir := filepath.Join(goenv.Get("MOXIEROOT"), "llvm-project/compiler-rt/lib/builtins")
 222  		if _, err := os.Stat(llvmDir); err == nil {
 223  			// Release build.
 224  			return llvmDir
 225  		}
 226  		// Development build.
 227  		return filepath.Join(goenv.Get("MOXIEROOT"), "lib/compiler-rt-builtins")
 228  	},
 229  	librarySources: func(target string, _ bool) ([]string, error) {
 230  		builtins := append([]string{}, genericBuiltins...) // copy genericBuiltins
 231  		switch compileopts.CanonicalArchName(target) {
 232  		case "arm":
 233  			builtins = append(builtins, aeabiBuiltins...)
 234  		case "avr":
 235  			builtins = append(builtins, avrBuiltins...)
 236  		case "x86_64", "aarch64", "riscv64": // any 64-bit arch
 237  			builtins = append(builtins, genericBuiltins128...)
 238  		case "i386":
 239  			if strings.Split(target, "-")[2] == "windows" {
 240  				builtins = append(builtins, windowsI386Builtins...)
 241  			}
 242  		}
 243  		return builtins, nil
 244  	},
 245  }
 246