llvm.org GIT mirror llvm / 572742e
Setting GlobalDirective in TargetAsmInfo by default rather than providing a misleading facility. It's used once in the MIPS backend and hardcoded as "\t.globl\t" everywhere else. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45676 91177308-0d34-0410-b5e6-96231b3b80d8 Gordon Henriksen 12 years ago
5 changed file(s) with 240 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
5656 /// X86_FastCall - 'fast' analog of X86_StdCall. Passes first two arguments
5757 /// in ECX:EDX registers, others - via stack. Callee is responsible for
5858 /// stack cleaning.
59 X86_FastCall = 65
59 X86_FastCall = 65,
60
61 /// X86_Ocaml - This is a weird ABI used by Objective Caml. Formally, it
62 /// supports only one to six integer/address arguments, all in-reg. It also
63 /// supports tail call emission.
64 X86_Ocaml = 66
6065 };
6166 } // End CallingConv namespace
6267
3535
3636 (void) llvm::createSimpleRegisterCoalescer();
3737
38 (void) llvm::createOcamlCollector();
3839 (void) llvm::createShadowStackCollector();
3940
4041 (void) llvm::createBURRListDAGScheduler(NULL, NULL, NULL);
0 //===-- OcamlCollector.cpp - Ocaml frametable emitter ---------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements lowering for the llvm.gc* intrinsics compatible with
10 // Objective Caml 3.10.0, which uses a liveness-accurate static stack map.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CodeGen/Collectors.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/Collector.h"
18 #include "llvm/CodeGen/CollectorMetadata.h"
19 #include "llvm/Function.h"
20 #include "llvm/Module.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Target/TargetAsmInfo.h"
24 #include "llvm/Target/TargetData.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include
27
28 using namespace llvm;
29
30 namespace {
31
32 class VISIBILITY_HIDDEN OcamlCollector : public Collector {
33 public:
34 OcamlCollector();
35
36 void beginAssembly(std::ostream &OS, AsmPrinter &AP,
37 const TargetAsmInfo &TAI);
38
39 void finishAssembly(std::ostream &OS, AsmPrinter &AP,
40 const TargetAsmInfo &TAI);
41 };
42
43 CollectorRegistry::Add
44 X("ocaml", "ocaml 3.10-compatible collector");
45
46 }
47
48 // -----------------------------------------------------------------------------
49
50 static void EmitCamlGlobal(const Module &M, std::ostream &OS, AsmPrinter &AP,
51 const TargetAsmInfo &TAI, const char *Id) {
52 const std::string &MId = M.getModuleIdentifier();
53
54 std::string Mangled;
55 Mangled += TAI.getGlobalPrefix();
56 Mangled += "caml";
57 size_t Letter = Mangled.size();
58 Mangled.append(MId.begin(), std::find(MId.begin(), MId.end(), '.'));
59 Mangled += "__";
60 Mangled += Id;
61
62 // Capitalize the first letter of the module name.
63 Mangled[Letter] = toupper(Mangled[Letter]);
64
65 if (const char *GlobalDirective = TAI.getGlobalDirective())
66 OS << GlobalDirective << Mangled << "\n";
67 OS << Mangled << ":\n";
68 }
69
70 Collector *llvm::createOcamlCollector() {
71 return new OcamlCollector();
72 }
73
74 OcamlCollector::OcamlCollector() {
75 NeededSafePoints = 1 << GC::PostCall;
76 }
77
78 void OcamlCollector::beginAssembly(std::ostream &OS, AsmPrinter &AP,
79 const TargetAsmInfo &TAI) {
80 AP.SwitchToTextSection(TAI.getTextSection());
81 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_begin");
82
83 AP.SwitchToDataSection(TAI.getDataSection());
84 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_begin");
85 }
86
87 /// emitAssembly - Print the frametable. The ocaml frametable format is thus:
88 ///
89 /// extern "C" struct align(sizeof(intptr_t)) {
90 /// uint16_t NumDescriptors;
91 /// struct align(sizeof(intptr_t)) {
92 /// void *ReturnAddress;
93 /// uint16_t FrameSize;
94 /// uint16_t NumLiveOffsets;
95 /// uint16_t LiveOffsets[NumLiveOffsets];
96 /// } Descriptors[NumDescriptors];
97 /// } caml${module}__frametable;
98 ///
99 /// Note that this precludes programs from stack frames larger than 64K
100 /// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
101 /// either condition is detected in a function which uses the collector.
102 ///
103 void OcamlCollector::finishAssembly(std::ostream &OS, AsmPrinter &AP,
104 const TargetAsmInfo &TAI) {
105 const char *AddressDirective;
106 int AddressAlignLog;
107 if (AP.TM.getTargetData()->getPointerSize() == sizeof(int32_t)) {
108 AddressDirective = TAI.getData32bitsDirective();
109 AddressAlignLog = 2;
110 } else {
111 AddressDirective = TAI.getData64bitsDirective();
112 AddressAlignLog = 3;
113 }
114
115 AP.SwitchToTextSection(TAI.getTextSection());
116 EmitCamlGlobal(getModule(), OS, AP, TAI, "code_end");
117
118 AP.SwitchToDataSection(TAI.getDataSection());
119 EmitCamlGlobal(getModule(), OS, AP, TAI, "data_end");
120
121 OS << AddressDirective << 0; // FIXME: Why does ocaml emit this??
122 AP.EOL();
123
124 AP.SwitchToDataSection(TAI.getDataSection());
125 EmitCamlGlobal(getModule(), OS, AP, TAI, "frametable");
126
127 for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
128 CollectorMetadata &MD = **FI;
129
130 OS << "\t" << TAI.getCommentString() << " live roots for "
131 << MD.getFunction().getNameStart() << "\n";
132
133 for (CollectorMetadata::iterator PI = MD.begin(),
134 PE = MD.end(); PI != PE; ++PI) {
135
136 uint64_t FrameSize = MD.getFrameSize();
137 if (FrameSize >= 2<<16) {
138 cerr << "Function '" << MD.getFunction().getNameStart()
139 << "' is too large for the ocaml collector! "
140 << "Frame size " << FrameSize << " >= 65536.\n";
141 abort(); // Very rude!
142 }
143
144 size_t LiveCount = MD.live_size(PI);
145 if (LiveCount >= 2<<16) {
146 cerr << "Function '" << MD.getFunction().getNameStart()
147 << "' is too large for the ocaml collector! "
148 << "Live root count " << LiveCount << " >= 65536.\n";
149 abort(); // Very rude!
150 }
151
152 OS << AddressDirective
153 << TAI.getPrivateGlobalPrefix() << "label" << PI->Num;
154 AP.EOL("call return address");
155
156 AP.EmitInt16(FrameSize);
157 AP.EOL("stack frame size");
158
159 AP.EmitInt16(LiveCount);
160 AP.EOL("live root count");
161
162 for (CollectorMetadata::live_iterator LI = MD.live_begin(PI),
163 LE = MD.live_end(PI);
164 LI != LE; ++LI) {
165 assert(LI->StackOffset < 2<<16 &&
166 "GC root stack offset is outside of fixed stack frame and out "
167 "of range for Ocaml collector!");
168
169 OS << "\t.word\t" << LI->StackOffset;
170 AP.EOL("stack offset");
171 }
172
173 AP.EmitAlignment(AddressAlignLog);
174 }
175 }
176 }
0 ; RUN: llvm-as < %s | llc -asm-verbose | grep {frame size} | grep -v 0x0
1
2 declare void @llvm.gcroot(i8** %value, i8* %tag)
3 declare void @g() gc "ocaml"
4
5 define void @f(i8* %arg.0, void()* %arg.1) gc "ocaml" {
6 entry:
7 %gcroot.0 = alloca i8*
8 call void @llvm.gcroot(i8** %gcroot.0, i8* null)
9 store i8* %arg.0, i8** %gcroot.0
10 call void @g()
11 call void %arg.1()
12 ret void
13 }
0 ; RUN: llvm-as < %s | llc | grep caml.*__frametable
1 ; RUN: llvm-as < %s | llc -march=x86 | grep {movl .0}
2
3 %struct.obj = type { i8*, %struct.obj* }
4
5 define %struct.obj* @fun(%struct.obj* %head) gc "ocaml" {
6 entry:
7 %gcroot.0 = alloca i8*
8 %gcroot.1 = alloca i8*
9
10 call void @llvm.gcroot(i8** %gcroot.0, i8* null)
11 call void @llvm.gcroot(i8** %gcroot.1, i8* null)
12
13 %local.0 = bitcast i8** %gcroot.0 to %struct.obj**
14 %local.1 = bitcast i8** %gcroot.1 to %struct.obj**
15
16 store %struct.obj* %head, %struct.obj** %local.0
17 br label %bb.loop
18 bb.loop:
19 %t0 = load %struct.obj** %local.0
20 %t1 = getelementptr %struct.obj* %t0, i32 0, i32 1
21 %t2 = bitcast %struct.obj* %t0 to i8*
22 %t3 = bitcast %struct.obj** %t1 to i8**
23 %t4 = call i8* @llvm.gcread(i8* %t2, i8** %t3)
24 %t5 = bitcast i8* %t4 to %struct.obj*
25 %t6 = icmp eq %struct.obj* %t5, null
26 br i1 %t6, label %bb.loop, label %bb.end
27 bb.end:
28 %t7 = malloc %struct.obj
29 store %struct.obj* %t7, %struct.obj** %local.1
30 %t8 = bitcast %struct.obj* %t7 to i8*
31 %t9 = load %struct.obj** %local.0
32 %t10 = getelementptr %struct.obj* %t9, i32 0, i32 1
33 %t11 = bitcast %struct.obj* %t9 to i8*
34 %t12 = bitcast %struct.obj** %t10 to i8**
35 call void @llvm.gcwrite(i8* %t8, i8* %t11, i8** %t12)
36 ret %struct.obj* %t7
37 }
38
39 declare void @llvm.gcroot(i8** %value, i8* %tag)
40 declare void @llvm.gcwrite(i8* %value, i8* %obj, i8** %field)
41 declare i8* @llvm.gcread(i8* %obj, i8** %field)