1 1.1 mrg 2 1.1 mrg /* Compiler implementation of the D programming language 3 1.1.1.3 mrg * Copyright (C) 2013-2022 by The D Language Foundation, All Rights Reserved 4 1.1 mrg * written by Iain Buclaw 5 1.1.1.3 mrg * https://www.digitalmars.com 6 1.1 mrg * Distributed under the Boost Software License, Version 1.0. 7 1.1.1.3 mrg * https://www.boost.org/LICENSE_1_0.txt 8 1.1 mrg * https://github.com/dlang/dmd/blob/master/src/dmd/target.h 9 1.1 mrg */ 10 1.1 mrg 11 1.1 mrg #pragma once 12 1.1 mrg 13 1.1 mrg // This file contains a data structure that describes a back-end target. 14 1.1 mrg // At present it is incomplete, but in future it should grow to contain 15 1.1 mrg // most or all target machine and target O/S specific information. 16 1.1 mrg #include "globals.h" 17 1.1 mrg #include "tokens.h" 18 1.1 mrg 19 1.1 mrg class ClassDeclaration; 20 1.1 mrg class Dsymbol; 21 1.1 mrg class Expression; 22 1.1.1.3 mrg class FuncDeclaration; 23 1.1.1.3 mrg class Statement; 24 1.1 mrg class Type; 25 1.1.1.2 mrg class TypeTuple; 26 1.1.1.3 mrg class TypeFunction; 27 1.1.1.3 mrg 28 1.1.1.3 mrg enum class CPU : unsigned char 29 1.1.1.3 mrg { 30 1.1.1.3 mrg x87, 31 1.1.1.3 mrg mmx, 32 1.1.1.3 mrg sse, 33 1.1.1.3 mrg sse2, 34 1.1.1.3 mrg sse3, 35 1.1.1.3 mrg ssse3, 36 1.1.1.3 mrg sse4_1, 37 1.1.1.3 mrg sse4_2, 38 1.1.1.3 mrg avx, // AVX1 instruction set 39 1.1.1.3 mrg avx2, // AVX2 instruction set 40 1.1.1.3 mrg avx512, // AVX-512 instruction set 41 1.1.1.3 mrg 42 1.1.1.3 mrg // Special values that don't survive past the command line processing 43 1.1.1.3 mrg baseline, // (default) the minimum capability CPU 44 1.1.1.3 mrg native // the machine the compiler is being run on 45 1.1.1.3 mrg }; 46 1.1.1.3 mrg 47 1.1.1.3 mrg struct TargetC 48 1.1.1.3 mrg { 49 1.1.1.3 mrg enum class Runtime : unsigned char 50 1.1.1.3 mrg { 51 1.1.1.3 mrg Unspecified, 52 1.1.1.3 mrg Bionic, 53 1.1.1.3 mrg DigitalMars, 54 1.1.1.3 mrg Glibc, 55 1.1.1.3 mrg Microsoft, 56 1.1.1.3 mrg Musl, 57 1.1.1.3 mrg Newlib, 58 1.1.1.3 mrg UClibc, 59 1.1.1.3 mrg WASI, 60 1.1.1.3 mrg }; 61 1.1.1.3 mrg 62 1.1.1.3 mrg enum class BitFieldStyle : unsigned char 63 1.1.1.3 mrg { 64 1.1.1.3 mrg Unspecified, 65 1.1.1.3 mrg DM, // Digital Mars 32 bit C compiler 66 1.1.1.3 mrg MS, // Microsoft 32 and 64 bit C compilers 67 1.1.1.3 mrg // https://docs.microsoft.com/en-us/cpp/c-language/c-bit-fields?view=msvc-160 68 1.1.1.3 mrg // https://docs.microsoft.com/en-us/cpp/cpp/cpp-bit-fields?view=msvc-160 69 1.1.1.3 mrg Gcc_Clang, // gcc and clang 70 1.1.1.3 mrg }; 71 1.1.1.3 mrg 72 1.1.1.3 mrg uint8_t crtDestructorsSupported; // Not all platforms support crt_destructor 73 1.1.1.3 mrg uint8_t boolsize; // size of a C '_Bool' type 74 1.1.1.3 mrg uint8_t shortsize; // size of a C 'short' or 'unsigned short' type 75 1.1.1.3 mrg uint8_t intsize; // size of a C 'int' or 'unsigned int' type 76 1.1.1.3 mrg uint8_t longsize; // size of a C 'long' or 'unsigned long' type 77 1.1.1.3 mrg uint8_t long_longsize; // size of a C 'long long' or 'unsigned long long' type 78 1.1.1.3 mrg uint8_t long_doublesize; // size of a C 'long double' 79 1.1.1.3 mrg uint8_t wchar_tsize; // size of a C 'wchar_t' type 80 1.1.1.3 mrg Runtime runtime; 81 1.1.1.3 mrg BitFieldStyle bitFieldStyle; // different C compilers do it differently 82 1.1.1.3 mrg }; 83 1.1.1.3 mrg 84 1.1.1.3 mrg struct TargetCPP 85 1.1.1.3 mrg { 86 1.1.1.3 mrg enum class Runtime : unsigned char 87 1.1.1.3 mrg { 88 1.1.1.3 mrg Unspecified, 89 1.1.1.3 mrg Clang, 90 1.1.1.3 mrg DigitalMars, 91 1.1.1.3 mrg Gcc, 92 1.1.1.3 mrg Microsoft, 93 1.1.1.3 mrg Sun 94 1.1.1.3 mrg }; 95 1.1.1.3 mrg bool reverseOverloads; // with dmc and cl, overloaded functions are grouped and in reverse order 96 1.1.1.3 mrg bool exceptions; // set if catching C++ exceptions is supported 97 1.1.1.3 mrg bool twoDtorInVtable; // target C++ ABI puts deleting and non-deleting destructor into vtable 98 1.1.1.3 mrg bool splitVBasetable; // set if C++ ABI uses separate tables for virtual functions and virtual bases 99 1.1.1.3 mrg bool wrapDtorInExternD; // set if C++ dtors require a D wrapper to be callable from runtime 100 1.1.1.3 mrg Runtime runtime; 101 1.1.1.3 mrg 102 1.1.1.3 mrg const char *toMangle(Dsymbol *s); 103 1.1.1.3 mrg const char *typeInfoMangle(ClassDeclaration *cd); 104 1.1.1.3 mrg const char *thunkMangle(FuncDeclaration *fd, int offset); 105 1.1.1.3 mrg const char *typeMangle(Type *t); 106 1.1.1.3 mrg Type *parameterType(Type *p); 107 1.1.1.3 mrg bool fundamentalType(const Type *t, bool& isFundamental); 108 1.1.1.3 mrg unsigned derivedClassOffset(ClassDeclaration *baseClass); 109 1.1.1.3 mrg }; 110 1.1.1.3 mrg 111 1.1.1.3 mrg struct TargetObjC 112 1.1.1.3 mrg { 113 1.1.1.3 mrg bool supported; // set if compiler can interface with Objective-C 114 1.1.1.3 mrg }; 115 1.1 mrg 116 1.1 mrg struct Target 117 1.1 mrg { 118 1.1.1.3 mrg typedef unsigned char OS; 119 1.1.1.3 mrg enum 120 1.1.1.3 mrg { 121 1.1.1.3 mrg /* These are mutually exclusive; one and only one is set. 122 1.1.1.3 mrg * Match spelling and casing of corresponding version identifiers 123 1.1.1.3 mrg */ 124 1.1.1.3 mrg OS_Freestanding = 0, 125 1.1.1.3 mrg OS_linux = 1, 126 1.1.1.3 mrg OS_Windows = 2, 127 1.1.1.3 mrg OS_OSX = 4, 128 1.1.1.3 mrg OS_OpenBSD = 8, 129 1.1.1.3 mrg OS_FreeBSD = 0x10, 130 1.1.1.3 mrg OS_Solaris = 0x20, 131 1.1.1.3 mrg OS_DragonFlyBSD = 0x40, 132 1.1.1.3 mrg 133 1.1.1.3 mrg // Combination masks 134 1.1.1.3 mrg all = OS_linux | OS_Windows | OS_OSX | OS_OpenBSD | OS_FreeBSD | OS_Solaris | OS_DragonFlyBSD, 135 1.1.1.3 mrg Posix = OS_linux | OS_OSX | OS_OpenBSD | OS_FreeBSD | OS_Solaris | OS_DragonFlyBSD, 136 1.1.1.3 mrg }; 137 1.1.1.3 mrg 138 1.1.1.3 mrg OS os; 139 1.1.1.3 mrg uint8_t osMajor; 140 1.1.1.3 mrg // D ABI 141 1.1.1.3 mrg uint8_t ptrsize; 142 1.1.1.3 mrg uint8_t realsize; // size a real consumes in memory 143 1.1.1.3 mrg uint8_t realpad; // 'padding' added to the CPU real size to bring it up to realsize 144 1.1.1.3 mrg uint8_t realalignsize; // alignment for reals 145 1.1.1.3 mrg uint8_t classinfosize; // size of 'ClassInfo' 146 1.1.1.3 mrg uint64_t maxStaticDataSize; // maximum size of static data 147 1.1.1.3 mrg 148 1.1.1.3 mrg // C ABI 149 1.1.1.3 mrg TargetC c; 150 1.1.1.3 mrg 151 1.1.1.3 mrg // C++ ABI 152 1.1.1.3 mrg TargetCPP cpp; 153 1.1.1.3 mrg 154 1.1.1.3 mrg // Objective-C ABI 155 1.1.1.3 mrg TargetObjC objc; 156 1.1.1.3 mrg 157 1.1.1.3 mrg DString architectureName; // name of the platform architecture (e.g. X86_64) 158 1.1.1.3 mrg CPU cpu; // CPU instruction set to target 159 1.1.1.3 mrg bool is64bit; // generate 64 bit code for x86_64; true by default for 64 bit dmd 160 1.1.1.3 mrg bool isLP64; // pointers are 64 bits 161 1.1.1.3 mrg 162 1.1.1.3 mrg // Environmental 163 1.1.1.3 mrg DString obj_ext; /// extension for object files 164 1.1.1.3 mrg DString lib_ext; /// extension for static library files 165 1.1.1.3 mrg DString dll_ext; /// extension for dynamic library files 166 1.1.1.3 mrg bool run_noext; /// allow -run sources without extensions 167 1.1.1.3 mrg bool omfobj; /// for Win32: write OMF object files instead of COFF 168 1.1 mrg 169 1.1 mrg template <typename T> 170 1.1 mrg struct FPTypeProperties 171 1.1 mrg { 172 1.1.1.3 mrg real_t max; 173 1.1.1.3 mrg real_t min_normal; 174 1.1.1.3 mrg real_t nan; 175 1.1.1.3 mrg real_t infinity; 176 1.1.1.3 mrg real_t epsilon; 177 1.1.1.3 mrg 178 1.1.1.3 mrg int64_t dig; 179 1.1.1.3 mrg int64_t mant_dig; 180 1.1.1.3 mrg int64_t max_exp; 181 1.1.1.3 mrg int64_t min_exp; 182 1.1.1.3 mrg int64_t max_10_exp; 183 1.1.1.3 mrg int64_t min_10_exp; 184 1.1 mrg }; 185 1.1 mrg 186 1.1.1.3 mrg FPTypeProperties<float> FloatProperties; 187 1.1.1.3 mrg FPTypeProperties<double> DoubleProperties; 188 1.1.1.3 mrg FPTypeProperties<real_t> RealProperties; 189 1.1.1.3 mrg 190 1.1.1.3 mrg private: 191 1.1.1.3 mrg Type *tvalist; 192 1.1.1.3 mrg const Param *params; 193 1.1 mrg 194 1.1.1.3 mrg public: 195 1.1.1.3 mrg void _init(const Param& params); 196 1.1 mrg // Type sizes and support. 197 1.1.1.3 mrg unsigned alignsize(Type *type); 198 1.1.1.3 mrg unsigned fieldalign(Type *type); 199 1.1.1.3 mrg Type *va_listType(const Loc &loc, Scope *sc); // get type of va_list 200 1.1.1.3 mrg int isVectorTypeSupported(int sz, Type *type); 201 1.1.1.3 mrg bool isVectorOpSupported(Type *type, EXP op, Type *t2 = NULL); 202 1.1 mrg // ABI and backend. 203 1.1.1.3 mrg LINK systemLinkage(); 204 1.1.1.3 mrg TypeTuple *toArgTypes(Type *t); 205 1.1.1.3 mrg bool isReturnOnStack(TypeFunction *tf, bool needsThis); 206 1.1.1.3 mrg bool preferPassByRef(Type *t); 207 1.1.1.3 mrg Expression *getTargetInfo(const char* name, const Loc& loc); 208 1.1.1.3 mrg bool isCalleeDestroyingArgs(TypeFunction* tf); 209 1.1.1.3 mrg bool libraryObjectMonitors(FuncDeclaration *fd, Statement *fbody); 210 1.1.1.3 mrg bool supportsLinkerDirective() const; 211 1.1.1.3 mrg void addPredefinedGlobalIdentifiers() const; 212 1.1 mrg }; 213 1.1.1.3 mrg 214 1.1.1.3 mrg extern Target target; 215