Skip to content

Commit ac8a954

Browse files
committed
MC: Sketch initial MCAsmLayout class, which encapsulates the current layout of an assembly file. The MCAsmLayout is also available for use by MCExpr::EvaluateAs{Absolute,Relocatable}, to allow target specific hooks and "absolutizing" of symbols.
llvm-svn: 98227
1 parent bd01967 commit ac8a954

File tree

6 files changed

+72
-14
lines changed

6 files changed

+72
-14
lines changed

llvm/include/llvm/MC/MCAsmLayout.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===- MCAsmLayout.h - Assembly Layout Object -------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_MC_MCASMLAYOUT_H
11+
#define LLVM_MC_MCASMLAYOUT_H
12+
13+
namespace llvm {
14+
class MCAssembler;
15+
16+
/// Encapsulates the layout of an assembly file at a particular point in time.
17+
///
18+
/// Assembly may requiring compute multiple layouts for a particular assembly
19+
/// file as part of the relaxation process. This class encapsulates the layout
20+
/// at a single point in time in such a way that it is always possible to
21+
/// efficiently compute the exact addresses of any symbol in the assembly file,
22+
/// even during the relaxation process.
23+
class MCAsmLayout {
24+
private:
25+
uint64_t CurrentLocation;
26+
27+
MCAssembler &Assembler;
28+
29+
public:
30+
MCAsmLayout(MCAssembler &_Assembler)
31+
: CurrentLocation(0), Assembler(_Assember) {}
32+
33+
/// Get the assembler object this is a layout for.
34+
MCAssembler &getAssembler() { return Assembler; }
35+
36+
/// Get the current location value, i.e. that value of the '.' expression.
37+
uin64_t getCurrentLocation() {
38+
return CurrentLocation;
39+
}
40+
41+
/// Set the current location.
42+
void setCurrentLocation(uint64_t Value) {
43+
CurrentLocation = Value;
44+
}
45+
};
46+
47+
} // end namespace llvm
48+
49+
#endif

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,8 @@ class MCAssembler {
625625

626626
MCContext &getContext() const { return Context; }
627627

628+
TargetAsmBackend &getBackend() const { return Backend; }
629+
628630
/// Finish - Do final processing and write the object to the output stream.
629631
void Finish();
630632

llvm/include/llvm/MC/MCExpr.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace llvm {
1717
class MCAsmInfo;
18+
class MCAsmLayout;
1819
class MCContext;
1920
class MCSymbol;
2021
class MCValue;
@@ -62,15 +63,19 @@ class MCExpr {
6263
/// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
6364
///
6465
/// @param Res - The absolute value, if evaluation succeeds.
66+
/// @param Layout - The assembler layout object to use for evaluating symbol
67+
/// values. If not given, then only non-symbolic expressions will be
68+
/// evaluated.
6569
/// @result - True on success.
66-
bool EvaluateAsAbsolute(int64_t &Res) const;
70+
bool EvaluateAsAbsolute(int64_t &Res, MCAsmLayout *Layout = 0) const;
6771

6872
/// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
6973
/// value, i.e. an expression of the fixed form (a - b + constant).
7074
///
7175
/// @param Res - The relocatable value, if evaluation succeeds.
76+
/// @param Layout - The assembler layout object to use for evaluating values.
7277
/// @result - True on success.
73-
bool EvaluateAsRelocatable(MCValue &Res) const;
78+
bool EvaluateAsRelocatable(MCValue &Res, MCAsmLayout *Layout = 0) const;
7479

7580
/// @}
7681

@@ -348,7 +353,8 @@ class MCTargetExpr : public MCExpr {
348353
public:
349354

350355
virtual void PrintImpl(raw_ostream &OS) const = 0;
351-
virtual bool EvaluateAsRelocatableImpl(MCValue &Res) const = 0;
356+
virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
357+
MCAsmLayout *Layout) const = 0;
352358

353359

354360
static bool classof(const MCExpr *E) {

llvm/lib/MC/MCExpr.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ void MCTargetExpr::Anchor() {}
142142

143143
/* *** */
144144

145-
bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
145+
bool MCExpr::EvaluateAsAbsolute(int64_t &Res, MCAsmLayout *Layout) const {
146146
MCValue Value;
147147

148-
if (!EvaluateAsRelocatable(Value) || !Value.isAbsolute())
148+
if (!EvaluateAsRelocatable(Value, Layout) || !Value.isAbsolute())
149149
return false;
150150

151151
Res = Value.getConstant();
@@ -174,10 +174,10 @@ static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A,
174174
return true;
175175
}
176176

177-
bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const {
177+
bool MCExpr::EvaluateAsRelocatable(MCValue &Res, MCAsmLayout *Layout) const {
178178
switch (getKind()) {
179179
case Target:
180-
return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res);
180+
return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
181181

182182
case Constant:
183183
Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
@@ -188,7 +188,7 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const {
188188

189189
// Evaluate recursively if this is a variable.
190190
if (Sym.isVariable())
191-
return Sym.getValue()->EvaluateAsRelocatable(Res);
191+
return Sym.getValue()->EvaluateAsRelocatable(Res, Layout);
192192

193193
Res = MCValue::get(&Sym, 0, 0);
194194
return true;
@@ -198,7 +198,7 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const {
198198
const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
199199
MCValue Value;
200200

201-
if (!AUE->getSubExpr()->EvaluateAsRelocatable(Value))
201+
if (!AUE->getSubExpr()->EvaluateAsRelocatable(Value, Layout))
202202
return false;
203203

204204
switch (AUE->getOpcode()) {
@@ -231,8 +231,8 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const {
231231
const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
232232
MCValue LHSValue, RHSValue;
233233

234-
if (!ABE->getLHS()->EvaluateAsRelocatable(LHSValue) ||
235-
!ABE->getRHS()->EvaluateAsRelocatable(RHSValue))
234+
if (!ABE->getLHS()->EvaluateAsRelocatable(LHSValue, Layout) ||
235+
!ABE->getRHS()->EvaluateAsRelocatable(RHSValue, Layout))
236236
return false;
237237

238238
// We only support a few operations on non-constant expressions, handle

llvm/lib/Target/X86/X86MCTargetExpr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ void X86MCTargetExpr::PrintImpl(raw_ostream &OS) const {
3636
}
3737
}
3838

39-
bool X86MCTargetExpr::EvaluateAsRelocatableImpl(MCValue &Res) const {
39+
bool X86MCTargetExpr::EvaluateAsRelocatableImpl(MCValue &Res,
40+
MCAsmLayout *Layout) const {
4041
// FIXME: I don't know if this is right, it followed MCSymbolRefExpr.
4142

4243
// Evaluate recursively if this is a variable.
4344
if (Sym->isVariable())
44-
return Sym->getValue()->EvaluateAsRelocatable(Res);
45+
return Sym->getValue()->EvaluateAsRelocatable(Res, Layout);
4546

4647
Res = MCValue::get(Sym, 0, 0);
4748
return true;

llvm/lib/Target/X86/X86MCTargetExpr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class X86MCTargetExpr : public MCTargetExpr {
4141
MCContext &Ctx);
4242

4343
void PrintImpl(raw_ostream &OS) const;
44-
bool EvaluateAsRelocatableImpl(MCValue &Res) const;
44+
bool EvaluateAsRelocatableImpl(MCValue &Res, MCAsmLayout *Layout) const;
4545
};
4646

4747
} // end namespace llvm

0 commit comments

Comments
 (0)