[llvm-commits] [llvm] r80235 - in /llvm/trunk: include/llvm/MC/MCStreamer.h lib/MC/MCMachOStreamer.cpp tools/llvm-mc/llvm-mc.cpp

Daniel Dunbar daniel at zuster.org
Thu Aug 27 01:17:51 PDT 2009


Author: ddunbar
Date: Thu Aug 27 03:17:51 2009
New Revision: 80235

URL: http://llvm.org/viewvc/llvm-project?rev=80235&view=rev
Log:
llvm-mc/Mach-O: Add MCCodeEmitter support, for encoding instructions.
 - No relocations yet, of course.

Modified:
    llvm/trunk/include/llvm/MC/MCStreamer.h
    llvm/trunk/lib/MC/MCMachOStreamer.cpp
    llvm/trunk/tools/llvm-mc/llvm-mc.cpp

Modified: llvm/trunk/include/llvm/MC/MCStreamer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCStreamer.h?rev=80235&r1=80234&r2=80235&view=diff

==============================================================================
--- llvm/trunk/include/llvm/MC/MCStreamer.h (original)
+++ llvm/trunk/include/llvm/MC/MCStreamer.h Thu Aug 27 03:17:51 2009
@@ -251,7 +251,8 @@
 
   /// createMachOStream - Create a machine code streamer which will generative
   /// Mach-O format object files.
-  MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS);
+  MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS,
+                                  MCCodeEmitter *CE = 0);
 
   /// createELFStreamer - Create a machine code streamer which will generative
   /// ELF format object files.

Modified: llvm/trunk/lib/MC/MCMachOStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCMachOStreamer.cpp?rev=80235&r1=80234&r2=80235&view=diff

==============================================================================
--- llvm/trunk/lib/MC/MCMachOStreamer.cpp (original)
+++ llvm/trunk/lib/MC/MCMachOStreamer.cpp Thu Aug 27 03:17:51 2009
@@ -11,9 +11,12 @@
 
 #include "llvm/MC/MCAssembler.h"
 #include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCCodeEmitter.h"
+#include "llvm/MC/MCInst.h"
 #include "llvm/MC/MCSection.h"
 #include "llvm/MC/MCSymbol.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/raw_ostream.h"
 using namespace llvm;
 
 namespace {
@@ -42,6 +45,8 @@
 private:
   MCAssembler Assembler;
 
+  MCCodeEmitter *Emitter;
+
   MCSectionData *CurSectionData;
 
   DenseMap<const MCSection*, MCSectionData*> SectionMap;
@@ -68,8 +73,9 @@
   }
 
 public:
-  MCMachOStreamer(MCContext &Context, raw_ostream &_OS)
-    : MCStreamer(Context), Assembler(_OS), CurSectionData(0) {}
+  MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter)
+    : MCStreamer(Context), Assembler(_OS), Emitter(_Emitter),
+      CurSectionData(0) {}
   ~MCMachOStreamer() {}
 
   const MCValue &AddValueSymbols(const MCValue &Value) {
@@ -302,13 +308,26 @@
 }
 
 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
-  llvm_unreachable("FIXME: Not yet implemented!");
+  // Scan for values.
+  for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
+    if (Inst.getOperand(i).isMCValue())
+      AddValueSymbols(Inst.getOperand(i).getMCValue());
+
+  if (!Emitter)
+    llvm_unreachable("no code emitter available!");
+
+  // FIXME: Relocations!
+  SmallString<256> Code;
+  raw_svector_ostream VecOS(Code);
+  Emitter->EncodeInstruction(Inst, VecOS);
+  EmitBytes(VecOS.str());
 }
 
 void MCMachOStreamer::Finish() {
   Assembler.Finish();
 }
 
-MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) {
-  return new MCMachOStreamer(Context, OS);
+MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS,
+                                      MCCodeEmitter *CE) {
+  return new MCMachOStreamer(Context, OS, CE);
 }

Modified: llvm/trunk/tools/llvm-mc/llvm-mc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/llvm-mc.cpp?rev=80235&r1=80234&r2=80235&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/llvm-mc.cpp (original)
+++ llvm/trunk/tools/llvm-mc/llvm-mc.cpp Thu Aug 27 03:17:51 2009
@@ -252,7 +252,8 @@
     Str.reset(createAsmStreamer(Ctx, *Out, *TAI, AP.get(), CE.get()));
   } else {
     assert(FileType == OFT_ObjectFile && "Invalid file type!");
-    Str.reset(createMachOStreamer(Ctx, *Out));
+    CE.reset(TheTarget->createCodeEmitter(*TM));
+    Str.reset(createMachOStreamer(Ctx, *Out, CE.get()));
   }
 
   AsmParser Parser(SrcMgr, Ctx, *Str.get());





More information about the llvm-commits mailing list