Pragma DtoGetPragma(Scope *sc, PragmaDeclaration *decl, std::string &arg1str) { Identifier *ident = decl->ident; Expressions *args = decl->args; Expression *expr = (args && args->dim > 0) ? (*args)[0]->semantic(sc) : 0; // pragma(LDC_intrinsic, "string") { funcdecl(s) } if (ident == Id::LDC_intrinsic) { if (!args || args->dim != 1 || !parseStringExp(expr, arg1str)) { error(Loc(), "requires exactly 1 string literal parameter"); fatal(); } // Recognize LDC-specific pragmas. struct LdcIntrinsic { std::string name; Pragma pragma; }; static LdcIntrinsic ldcIntrinsic[] = { { "bitop.bt", LLVMbitop_bt }, { "bitop.btc", LLVMbitop_btc }, { "bitop.btr", LLVMbitop_btr }, { "bitop.bts", LLVMbitop_bts }, { "bitop.vld", LLVMbitop_vld }, { "bitop.vst", LLVMbitop_vst }, }; static std::string prefix = "ldc."; if (arg1str.length() > prefix.length() && std::equal(prefix.begin(), prefix.end(), arg1str.begin())) { // Got ldc prefix, binary search through ldcIntrinsic. std::string name(arg1str.begin() + prefix.length(), arg1str.end()); size_t i = 0, j = sizeof(ldcIntrinsic) / sizeof(ldcIntrinsic[0]); do { size_t k = (i + j) / 2; int cmp = name.compare(ldcIntrinsic[k].name); if (!cmp) return ldcIntrinsic[k].pragma; else if (cmp < 0) j = k; else i = k + 1; } while (i != j); } return LLVMintrinsic; } // pragma(LDC_global_crt_ctor [, priority]) { funcdecl(s) } else if (ident == Id::LDC_global_crt_ctor || ident == Id::LDC_global_crt_dtor) { dinteger_t priority; if (args) { if (args->dim != 1 || !parseIntExp(expr, priority)) { error(Loc(), "requires at most 1 integer literal parameter"); fatal(); } if (priority > 65535) { error(Loc(), "priority may not be greater then 65535"); priority = 65535; } } else priority = 65535; char buf[8]; sprintf(buf, "%llu", static_cast<unsigned long long>(priority)); arg1str = std::string(buf); return ident == Id::LDC_global_crt_ctor ? LLVMglobal_crt_ctor : LLVMglobal_crt_dtor; } // pragma(LDC_no_typeinfo) { typedecl(s) } else if (ident == Id::LDC_no_typeinfo) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } return LLVMno_typeinfo; } // pragma(LDC_no_moduleinfo) ; else if (ident == Id::LDC_no_moduleinfo) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } sc->module->noModuleInfo = true; return LLVMignore; } // pragma(LDC_alloca) { funcdecl(s) } else if (ident == Id::LDC_alloca) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } return LLVMalloca; } // pragma(LDC_va_start) { templdecl(s) } else if (ident == Id::LDC_va_start) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } return LLVMva_start; } // pragma(LDC_va_copy) { funcdecl(s) } else if (ident == Id::LDC_va_copy) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } return LLVMva_copy; } // pragma(LDC_va_end) { funcdecl(s) } else if (ident == Id::LDC_va_end) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } return LLVMva_end; } // pragma(LDC_va_arg) { templdecl(s) } else if (ident == Id::LDC_va_arg) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } return LLVMva_arg; } // pragma(LDC_fence) { funcdecl(s) } else if (ident == Id::LDC_fence) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } return LLVMfence; } // pragma(LDC_atomic_load) { templdecl(s) } else if (ident == Id::LDC_atomic_load) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } return LLVMatomic_load; } // pragma(LDC_atomic_store) { templdecl(s) } else if (ident == Id::LDC_atomic_store) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } return LLVMatomic_store; } // pragma(LDC_atomic_cmp_xchg) { templdecl(s) } else if (ident == Id::LDC_atomic_cmp_xchg) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } return LLVMatomic_cmp_xchg; } // pragma(LDC_atomic_rmw, "string") { templdecl(s) } else if (ident == Id::LDC_atomic_rmw) { if (!args || args->dim != 1 || !parseStringExp(expr, arg1str)) { error(Loc(), "requires exactly 1 string literal parameter"); fatal(); } return LLVMatomic_rmw; } // pragma(LDC_verbose); else if (ident == Id::LDC_verbose) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } sc->module->llvmForceLogging = true; return LLVMignore; } // pragma(LDC_inline_asm) { templdecl(s) } else if (ident == Id::LDC_inline_asm) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } return LLVMinline_asm; } // pragma(LDC_inline_ir) { templdecl(s) } else if (ident == Id::LDC_inline_ir) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } return LLVMinline_ir; } // pragma(LDC_extern_weak) { vardecl(s) } else if (ident == Id::LDC_extern_weak) { if (args && args->dim > 0) { error(Loc(), "takes no parameters"); fatal(); } return LLVMextern_weak; } return LLVMnone; }
void PragmaDeclaration::semantic(Scope *sc) { // Should be merged with PragmaStatement #if IN_LLVM int llvm_internal = 0; std::string arg1str; #endif //printf("\tPragmaDeclaration::semantic '%s'\n",toChars()); if (ident == Id::msg) { if (args) { for (size_t i = 0; i < args->dim; i++) { Expression *e = (Expression *)args->data[i]; e = e->semantic(sc); e = e->optimize(WANTvalue | WANTinterpret); if (e->op == TOKstring) { StringExp *se = (StringExp *)e; fprintf(stdmsg, "%.*s", (int)se->len, (char *)se->string); } else fprintf(stdmsg, "%s", e->toChars()); } fprintf(stdmsg, "\n"); } goto Lnodecl; } else if (ident == Id::lib) { if (!args || args->dim != 1) error("string expected for library name"); else { Expression *e = (Expression *)args->data[0]; e = e->semantic(sc); e = e->optimize(WANTvalue | WANTinterpret); args->data[0] = (void *)e; if (e->op != TOKstring) error("string expected for library name, not '%s'", e->toChars()); else if (global.params.verbose) { StringExp *se = (StringExp *)e; char *name = (char *)mem.malloc(se->len + 1); memcpy(name, se->string, se->len); name[se->len] = 0; printf("library %s\n", name); mem.free(name); } } goto Lnodecl; } #if IN_GCC else if (ident == Id::GNU_asm) { if (! args || args->dim != 2) error("identifier and string expected for asm name"); else { Expression *e; Declaration *d = NULL; StringExp *s = NULL; e = (Expression *)args->data[0]; e = e->semantic(sc); if (e->op == TOKvar) { d = ((VarExp *)e)->var; if (! d->isFuncDeclaration() && ! d->isVarDeclaration()) d = NULL; } if (!d) error("first argument of GNU_asm must be a function or variable declaration"); e = (Expression *)args->data[1]; e = e->semantic(sc); e = e->optimize(WANTvalue); if (e->op == TOKstring && ((StringExp *)e)->sz == 1) s = ((StringExp *)e); else error("second argument of GNU_asm must be a char string"); if (d && s) d->c_ident = Lexer::idPool((char*) s->string); } goto Lnodecl; } #endif #if DMDV2 else if (ident == Id::startaddress) { if (!args || args->dim != 1) error("function name expected for start address"); else { Expression *e = (Expression *)args->data[0]; e = e->semantic(sc); e = e->optimize(WANTvalue | WANTinterpret); args->data[0] = (void *)e; Dsymbol *sa = getDsymbol(e); if (!sa || !sa->isFuncDeclaration()) error("function name expected for start address, not '%s'", e->toChars()); } goto Lnodecl; } #endif #if TARGET_NET else if (ident == Lexer::idPool("assembly")) { } #endif // TARGET_NET // LDC #if IN_LLVM // pragma(intrinsic, "string") { funcdecl(s) } else if (ident == Id::intrinsic) { Expression* expr = (Expression *)args->data[0]; expr = expr->semantic(sc); if (!args || args->dim != 1 || !parseStringExp(expr, arg1str)) { error("requires exactly 1 string literal parameter"); fatal(); } llvm_internal = LLVMintrinsic; } // pragma(notypeinfo) { typedecl(s) } else if (ident == Id::no_typeinfo) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } llvm_internal = LLVMno_typeinfo; } // pragma(nomoduleinfo) ; else if (ident == Id::no_moduleinfo) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } llvm_internal = LLVMno_moduleinfo; } // pragma(alloca) { funcdecl(s) } else if (ident == Id::Alloca) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } llvm_internal = LLVMalloca; } // pragma(va_start) { templdecl(s) } else if (ident == Id::vastart) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } llvm_internal = LLVMva_start; } // pragma(va_copy) { funcdecl(s) } else if (ident == Id::vacopy) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } llvm_internal = LLVMva_copy; } // pragma(va_end) { funcdecl(s) } else if (ident == Id::vaend) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } llvm_internal = LLVMva_end; } // pragma(va_arg) { templdecl(s) } else if (ident == Id::vaarg) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } llvm_internal = LLVMva_arg; } // pragma(fence) { templdecl(s) } else if (ident == Id::fence) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } llvm_internal = LLVMfence; } // pragma(atomic_load) { templdecl(s) } else if (ident == Id::atomic_load) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } llvm_internal = LLVMatomic_load; } // pragma(atomic_store) { templdecl(s) } else if (ident == Id::atomic_store) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } llvm_internal = LLVMatomic_store; } // pragma(atomic_cmp_xchg) { templdecl(s) } else if (ident == Id::atomic_cmp_xchg) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } llvm_internal = LLVMatomic_cmp_xchg; } // pragma(atomic_rmw, "string") { templdecl(s) } else if (ident == Id::atomic_rmw) { Expression* expr = (Expression *)args->data[0]; expr = expr->semantic(sc); if (!args || args->dim != 1 || !parseStringExp(expr, arg1str)) { error("requires exactly 1 string literal parameter"); fatal(); } llvm_internal = LLVMatomic_rmw; } // pragma(ldc, "string") { templdecl(s) } else if (ident == Id::ldc) { Expression* expr = (Expression *)args->data[0]; expr = expr->semantic(sc); if (!args || args->dim != 1 || !parseStringExp(expr, arg1str)) { error("requires exactly 1 string literal parameter"); fatal(); } else if (arg1str == "verbose") { sc->module->llvmForceLogging = true; } else { error("command '%s' invalid", expr->toChars()); fatal(); } } // pragma(llvm_inline_asm) { templdecl(s) } else if (ident == Id::llvm_inline_asm) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } llvm_internal = LLVMinline_asm; } #endif // LDC else if (ignoreUnsupportedPragmas) { if (global.params.verbose) { /* Print unrecognized pragmas */ printf("pragma %s", ident->toChars()); if (args) { for (size_t i = 0; i < args->dim; i++) { // ignore errors in ignored pragmas. global.gag++; unsigned errors_save = global.errors; Expression *e = (Expression *)args->data[i]; e = e->semantic(sc); e = e->optimize(WANTvalue | WANTinterpret); if (i == 0) printf(" ("); else printf(","); printf("%s", e->toChars()); // restore error state. global.gag--; global.errors = errors_save; } if (args->dim) printf(")"); } printf("\n"); } } else error("unrecognized pragma(%s)", ident->toChars()); if (decl) { for (unsigned i = 0; i < decl->dim; i++) { Dsymbol *s = (Dsymbol *)decl->data[i]; s->semantic(sc); // LDC #if IN_LLVM if (llvm_internal) { if (s->llvmInternal) { error("multiple LDC specific pragmas not allowed not affect the same declaration ('%s' at '%s')", s->toChars(), s->loc.toChars()); fatal(); } switch(llvm_internal) { case LLVMintrinsic: if (FuncDeclaration* fd = s->isFuncDeclaration()) { fd->llvmInternal = llvm_internal; fd->intrinsicName = arg1str; fd->linkage = LINKintrinsic; ((TypeFunction*)fd->type)->linkage = LINKintrinsic; } else if (TemplateDeclaration* td = s->isTemplateDeclaration()) { td->llvmInternal = llvm_internal; td->intrinsicName = arg1str; } else { error("only allowed on function declarations"); fatal(); } break; case LLVMatomic_rmw: if (TemplateDeclaration* td = s->isTemplateDeclaration()) { td->llvmInternal = llvm_internal; td->intrinsicName = arg1str; } else { error("the '%s' pragma is only allowed on template declarations", ident->toChars()); fatal(); } break; case LLVMva_start: case LLVMva_arg: case LLVMatomic_load: case LLVMatomic_store: case LLVMatomic_cmp_xchg: if (TemplateDeclaration* td = s->isTemplateDeclaration()) { if (td->parameters->dim != 1) { error("the '%s' pragma template must have exactly one template parameter", ident->toChars()); fatal(); } else if (!td->onemember) { error("the '%s' pragma template must have exactly one member", ident->toChars()); fatal(); } else if (td->overnext || td->overroot) { error("the '%s' pragma template must not be overloaded", ident->toChars()); fatal(); } td->llvmInternal = llvm_internal; } else { error("the '%s' pragma is only allowed on template declarations", ident->toChars()); fatal(); } break; case LLVMva_copy: case LLVMva_end: case LLVMfence: if (FuncDeclaration* fd = s->isFuncDeclaration()) { fd->llvmInternal = llvm_internal; } else { error("the '%s' pragma is only allowed on function declarations", ident->toChars()); fatal(); } break; case LLVMno_typeinfo: s->llvmInternal = llvm_internal; break; case LLVMalloca: if (FuncDeclaration* fd = s->isFuncDeclaration()) { fd->llvmInternal = llvm_internal; } else { error("the '%s' pragma must only be used on function declarations of type 'void* function(uint nbytes)'", ident->toChars()); fatal(); } break; case LLVMinline_asm: if (TemplateDeclaration* td = s->isTemplateDeclaration()) { if (td->parameters->dim > 1) { error("the '%s' pragma template must have exactly zero or one template parameters", ident->toChars()); fatal(); } else if (!td->onemember) { error("the '%s' pragma template must have exactly one member", ident->toChars()); fatal(); } td->llvmInternal = llvm_internal; } else { error("the '%s' pragma is only allowed on template declarations", ident->toChars()); fatal(); } break; default: warning("the LDC specific pragma '%s' is not yet implemented, ignoring", ident->toChars()); } } #endif // LDC } } return; Lnodecl: if (decl) error("pragma is missing closing ';'"); }
Pragma DtoGetPragma(Scope *sc, PragmaDeclaration *decl, std::string &arg1str) { Identifier *ident = decl->ident; Expressions *args = decl->args; // pragma(intrinsic, "string") { funcdecl(s) } if (ident == Id::intrinsic) { struct LdcIntrinsic { std::string name; Pragma pragma; }; static LdcIntrinsic ldcIntrinsic[] = { { "bitop.bt", LLVMbitop_bt }, { "bitop.btc", LLVMbitop_btc }, { "bitop.btr", LLVMbitop_btr }, { "bitop.bts", LLVMbitop_bts }, }; Expression* expr = static_cast<Expression *>(args->data[0]); expr = expr->semantic(sc); if (!args || args->dim != 1 || !parseStringExp(expr, arg1str)) { error("requires exactly 1 string literal parameter"); fatal(); } std::string prefix = "ldc."; if (arg1str.length() > prefix.length() && std::equal(prefix.begin(), prefix.end(), arg1str.begin())) { std::string name(arg1str.begin() + prefix.length(), arg1str.end()); int i = 0, j = sizeof(ldcIntrinsic) / sizeof(ldcIntrinsic[0]), k, l; do { k = (i + j) / 2; l = name.compare(ldcIntrinsic[k].name); if (!l) return ldcIntrinsic[k].pragma; else if (l < 0) j = k; else i = k + 1; } while (i != j); } return LLVMintrinsic; } // pragma(notypeinfo) { typedecl(s) } else if (ident == Id::no_typeinfo) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } return LLVMno_typeinfo; } // pragma(nomoduleinfo) ; else if (ident == Id::no_moduleinfo) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } return LLVMno_moduleinfo; } // pragma(alloca) { funcdecl(s) } else if (ident == Id::Alloca) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } return LLVMalloca; } // pragma(shufflevector) { funcdecl(s) } else if (ident == Id::Shufflevector) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } return LLVMshufflevector; } // pragma(va_start) { templdecl(s) } else if (ident == Id::vastart) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } return LLVMva_start; } // pragma(va_copy) { funcdecl(s) } else if (ident == Id::vacopy) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } return LLVMva_copy; } // pragma(va_end) { funcdecl(s) } else if (ident == Id::vaend) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } return LLVMva_end; } // pragma(va_arg) { templdecl(s) } else if (ident == Id::vaarg) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } return LLVMva_arg; } // pragma(fence) { funcdecl(s) } else if (ident == Id::fence) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } return LLVMfence; } // pragma(atomic_load) { templdecl(s) } else if (ident == Id::atomic_load) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } return LLVMatomic_load; } // pragma(atomic_store) { templdecl(s) } else if (ident == Id::atomic_store) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } return LLVMatomic_store; } // pragma(atomic_cmp_xchg) { templdecl(s) } else if (ident == Id::atomic_cmp_xchg) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } return LLVMatomic_cmp_xchg; } // pragma(atomic_rmw, "string") { templdecl(s) } else if (ident == Id::atomic_rmw) { Expression* expr = static_cast<Expression *>(args->data[0]); expr = expr->semantic(sc); if (!args || args->dim != 1 || !parseStringExp(expr, arg1str)) { error("requires exactly 1 string literal parameter"); fatal(); } return LLVMatomic_rmw; } // pragma(ldc, "string") { templdecl(s) } else if (ident == Id::ldc) { Expression* expr = static_cast<Expression *>(args->data[0]); expr = expr->semantic(sc); if (!args || args->dim != 1 || !parseStringExp(expr, arg1str)) { error("requires exactly 1 string literal parameter"); fatal(); } else if (arg1str == "verbose") { sc->module->llvmForceLogging = true; } else { error("command '%s' invalid", expr->toChars()); fatal(); } } // pragma(llvm_inline_asm) { templdecl(s) } else if (ident == Id::llvm_inline_asm) { if (args && args->dim > 0) { error("takes no parameters"); fatal(); } return LLVMinline_asm; } return LLVMnone; }