comparison gen/naked.cpp @ 923:9bab304ed531

Added support for naked asm on OSX. (hopefully!)
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Tue, 03 Feb 2009 18:14:30 +0100
parents 545f54041d91
children 03d7c4aac654
comparison
equal deleted inserted replaced
922:0749c0757a43 923:9bab304ed531
102 102
103 std::ostringstream& asmstr = gIR->nakedAsm; 103 std::ostringstream& asmstr = gIR->nakedAsm;
104 104
105 // build function header 105 // build function header
106 106
107 // REALLY FIXME: this is most likely extremely platform dependent 107 // FIXME: could we perhaps use llvm asmwriter to give us these details ?
108 108
109 const char* mangle = fd->mangle(); 109 const char* mangle = fd->mangle();
110 const char* linkage = "globl";
111 std::string section = "text";
112 unsigned align = 16;
113
114 std::ostringstream tmpstr; 110 std::ostringstream tmpstr;
115 111
116 if (DtoIsTemplateInstance(fd)) 112 // osx is different
113 // also mangling has an extra underscore prefixed
114 if (global.params.os == OSMacOSX)
117 { 115 {
118 linkage = "weak"; 116 std::string section = "text";
119 tmpstr << "section\t.gnu.linkonce.t." << mangle << ",\"ax\",@progbits"; 117 bool weak = false;
120 section = tmpstr.str(); 118 if (DtoIsTemplateInstance(fd))
119 {
120 tmpstr << "section\t__TEXT,__textcoal_nt,coalesced,pure_instructions";
121 section = tmpstr.str();
122 weak = true;
123 }
124 asmstr << "\t." << section << std::endl;
125 asmstr << "\t.align\t4,0x90" << std::endl;
126 asmstr << "\t.globl\t_" << mangle << std::endl;
127 if (weak)
128 {
129 asmstr << "\t.weak_definition\t_" << mangle << std::endl;
130 }
131 asmstr << "_" << mangle << ":" << std::endl;
121 } 132 }
122 133 // this works on linux x86 32 and 64 bit
123 asmstr << "\t." << section << std::endl; 134 // assume it works everywhere else as well for now
124 asmstr << "\t.align\t" << align << std::endl; 135 else
125 asmstr << "\t." << linkage << "\t" << mangle << std::endl; 136 {
126 asmstr << "\t.type\t" << mangle << ",@function" << std::endl; 137 const char* linkage = "globl";
127 asmstr << mangle << ":" << std::endl; 138 std::string section = "text";
139 if (DtoIsTemplateInstance(fd))
140 {
141 linkage = "weak";
142 tmpstr << "section\t.gnu.linkonce.t." << mangle << ",\"ax\",@progbits";
143 section = tmpstr.str();
144 }
145 asmstr << "\t." << section << std::endl;
146 asmstr << "\t.align\t16" << std::endl;
147 asmstr << "\t." << linkage << "\t" << mangle << std::endl;
148 asmstr << "\t.type\t" << mangle << ",@function" << std::endl;
149 asmstr << mangle << ":" << std::endl;
150 }
128 151
129 // emit body 152 // emit body
130 fd->fbody->toNakedIR(gIR); 153 fd->fbody->toNakedIR(gIR);
131 154
132 // emit size after body 155 // emit size after body
133 // why? dunno, llvm seems to do it by default .. 156 // llvm does this on linux, but not on osx
134 asmstr << "\t.size\t" << mangle << ", .-" << mangle << std::endl << std::endl; 157 if (global.params.os != OSMacOSX)
158 {
159 asmstr << "\t.size\t" << mangle << ", .-" << mangle << std::endl << std::endl;
160 }
135 161
136 gIR->module->appendModuleInlineAsm(asmstr.str()); 162 gIR->module->appendModuleInlineAsm(asmstr.str());
137 asmstr.str(""); 163 asmstr.str("");
138 164
139 gIR->functions.pop_back(); 165 gIR->functions.pop_back();