blob: 3bf05cbb04a234ccdb4ed4466c1df45ba0ae818a [file] [log] [blame]
Daniel Dunbara5cc0032009-06-24 01:03:06 +00001//===- AsmStreamerTest.cpp - Triple unit tests ----------------------------===//
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#include "gtest/gtest.h"
11#include "llvm/MC/MCContext.h"
Benjamin Kramer1a264be2009-07-31 19:12:33 +000012#include "llvm/MC/MCSection.h"
Daniel Dunbara5cc0032009-06-24 01:03:06 +000013#include "llvm/MC/MCStreamer.h"
Daniel Dunbar79262862009-06-24 16:05:35 +000014#include "llvm/MC/MCValue.h"
Daniel Dunbara5cc0032009-06-24 01:03:06 +000015#include "llvm/Support/raw_ostream.h"
16using namespace llvm;
17
18namespace {
19
20// Helper class.
21class StringAsmStreamer {
22 std::string Str;
23 raw_string_ostream OS;
24 MCContext Context;
25 MCStreamer *Streamer;
26
27public:
28 StringAsmStreamer() : OS(Str), Streamer(createAsmStreamer(Context, OS)) {}
29 ~StringAsmStreamer() {
30 delete Streamer;
31 }
32
33 MCContext &getContext() { return Context; }
34 MCStreamer &getStreamer() { return *Streamer; }
35
36 const std::string &getString() {
37 Streamer->Finish();
38 return Str;
39 }
40};
41
42TEST(AsmStreamer, EmptyOutput) {
43 StringAsmStreamer S;
Daniel Dunbar937e6e92009-06-29 19:57:24 +000044 EXPECT_EQ("", S.getString());
Daniel Dunbara5cc0032009-06-24 01:03:06 +000045}
46
47TEST(AsmStreamer, Sections) {
48 StringAsmStreamer S;
Benjamin Kramer1a264be2009-07-31 19:12:33 +000049 MCSection *Sec0 = MCSection::Create("foo", S.getContext());
Daniel Dunbara5cc0032009-06-24 01:03:06 +000050 S.getStreamer().SwitchSection(Sec0);
Daniel Dunbar937e6e92009-06-29 19:57:24 +000051 EXPECT_EQ(".section foo\n", S.getString());
Daniel Dunbara5cc0032009-06-24 01:03:06 +000052}
53
Daniel Dunbar79262862009-06-24 16:05:35 +000054TEST(AsmStreamer, Values) {
55 StringAsmStreamer S;
Benjamin Kramer1a264be2009-07-31 19:12:33 +000056 MCSection *Sec0 = MCSection::Create("foo", S.getContext());
Daniel Dunbar75a52922009-06-24 17:00:42 +000057 MCSymbol *A = S.getContext().CreateSymbol("a");
58 MCSymbol *B = S.getContext().CreateSymbol("b");
Daniel Dunbar79262862009-06-24 16:05:35 +000059 S.getStreamer().SwitchSection(Sec0);
60 S.getStreamer().EmitLabel(A);
61 S.getStreamer().EmitLabel(B);
62 S.getStreamer().EmitValue(MCValue::get(A, B, 10), 1);
63 S.getStreamer().EmitValue(MCValue::get(A, B, 10), 2);
64 S.getStreamer().EmitValue(MCValue::get(A, B, 10), 4);
65 S.getStreamer().EmitValue(MCValue::get(A, B, 10), 8);
Daniel Dunbar937e6e92009-06-29 19:57:24 +000066 EXPECT_EQ(".section foo\n\
Daniel Dunbar79262862009-06-24 16:05:35 +000067a:\n\
68b:\n\
69.byte a - b + 10\n\
70.short a - b + 10\n\
71.long a - b + 10\n\
Daniel Dunbar937e6e92009-06-29 19:57:24 +000072.quad a - b + 10\n", S.getString());
Daniel Dunbar79262862009-06-24 16:05:35 +000073}
74
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +000075TEST(AsmStreamer, Align) {
76 StringAsmStreamer S;
Benjamin Kramer1a264be2009-07-31 19:12:33 +000077 MCSection *Sec0 = MCSection::Create("foo", S.getContext());
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +000078 S.getStreamer().SwitchSection(Sec0);
79 S.getStreamer().EmitValueToAlignment(4);
80 S.getStreamer().EmitValueToAlignment(4, /*Value=*/12, /*ValueSize=*/2);
81 S.getStreamer().EmitValueToAlignment(8, /*Value=*/12, /*ValueSize=*/4,
82 /*MaxBytesToEmit=*/24);
Daniel Dunbar937e6e92009-06-29 19:57:24 +000083 EXPECT_EQ(".section foo\n\
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +000084.p2align 2, 0\n\
85.p2alignw 2, 12\n\
Daniel Dunbar937e6e92009-06-29 19:57:24 +000086.p2alignl 3, 12, 24\n", S.getString());
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +000087}
88
89TEST(AsmStreamer, Org) {
90 StringAsmStreamer S;
Benjamin Kramer1a264be2009-07-31 19:12:33 +000091 MCSection *Sec0 = MCSection::Create("foo", S.getContext());
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +000092 S.getStreamer().SwitchSection(Sec0);
93 MCSymbol *A = S.getContext().CreateSymbol("a");
94 S.getStreamer().EmitLabel(A);
95 S.getStreamer().EmitValueToOffset(MCValue::get(A, 0, 4), 32);
Daniel Dunbar937e6e92009-06-29 19:57:24 +000096 EXPECT_EQ(".section foo\n\
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +000097a:\n\
Daniel Dunbar937e6e92009-06-29 19:57:24 +000098.org a + 4, 32\n", S.getString());
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +000099}
100
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000101}