1ece8a530Spatrick //===- MSP430.cpp ---------------------------------------------------------===//
2ece8a530Spatrick //
3ece8a530Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ece8a530Spatrick // See https://llvm.org/LICENSE.txt for license information.
5ece8a530Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ece8a530Spatrick //
7ece8a530Spatrick //===----------------------------------------------------------------------===//
8ece8a530Spatrick //
9ece8a530Spatrick // The MSP430 is a 16-bit microcontroller RISC architecture. The instruction set
10ece8a530Spatrick // has only 27 core instructions orthogonally augmented with a variety
11ece8a530Spatrick // of addressing modes for source and destination operands. Entire address space
12ece8a530Spatrick // of MSP430 is 64KB (the extended MSP430X architecture is not considered here).
13ece8a530Spatrick // A typical MSP430 MCU has several kilobytes of RAM and ROM, plenty
14ece8a530Spatrick // of peripherals and is generally optimized for a low power consumption.
15ece8a530Spatrick //
16ece8a530Spatrick //===----------------------------------------------------------------------===//
17ece8a530Spatrick
18ece8a530Spatrick #include "Symbols.h"
19ece8a530Spatrick #include "Target.h"
20ece8a530Spatrick #include "lld/Common/ErrorHandler.h"
21*dfe94b16Srobert #include "llvm/BinaryFormat/ELF.h"
22ece8a530Spatrick #include "llvm/Support/Endian.h"
23ece8a530Spatrick
24ece8a530Spatrick using namespace llvm;
25ece8a530Spatrick using namespace llvm::object;
26ece8a530Spatrick using namespace llvm::support::endian;
27ece8a530Spatrick using namespace llvm::ELF;
28bb684c34Spatrick using namespace lld;
29bb684c34Spatrick using namespace lld::elf;
30ece8a530Spatrick
31ece8a530Spatrick namespace {
32ece8a530Spatrick class MSP430 final : public TargetInfo {
33ece8a530Spatrick public:
34ece8a530Spatrick MSP430();
35ece8a530Spatrick RelExpr getRelExpr(RelType type, const Symbol &s,
36ece8a530Spatrick const uint8_t *loc) const override;
37bb684c34Spatrick void relocate(uint8_t *loc, const Relocation &rel,
38bb684c34Spatrick uint64_t val) const override;
39ece8a530Spatrick };
40ece8a530Spatrick } // namespace
41ece8a530Spatrick
MSP430()42ece8a530Spatrick MSP430::MSP430() {
43ece8a530Spatrick // mov.b #0, r3
44ece8a530Spatrick trapInstr = {0x43, 0x43, 0x43, 0x43};
45ece8a530Spatrick }
46ece8a530Spatrick
getRelExpr(RelType type,const Symbol & s,const uint8_t * loc) const47ece8a530Spatrick RelExpr MSP430::getRelExpr(RelType type, const Symbol &s,
48ece8a530Spatrick const uint8_t *loc) const {
49ece8a530Spatrick switch (type) {
50ece8a530Spatrick case R_MSP430_10_PCREL:
51ece8a530Spatrick case R_MSP430_16_PCREL:
52ece8a530Spatrick case R_MSP430_16_PCREL_BYTE:
53ece8a530Spatrick case R_MSP430_2X_PCREL:
54ece8a530Spatrick case R_MSP430_RL_PCREL:
55ece8a530Spatrick case R_MSP430_SYM_DIFF:
56ece8a530Spatrick return R_PC;
57ece8a530Spatrick default:
58ece8a530Spatrick return R_ABS;
59ece8a530Spatrick }
60ece8a530Spatrick }
61ece8a530Spatrick
relocate(uint8_t * loc,const Relocation & rel,uint64_t val) const62bb684c34Spatrick void MSP430::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
63bb684c34Spatrick switch (rel.type) {
64ece8a530Spatrick case R_MSP430_8:
65bb684c34Spatrick checkIntUInt(loc, val, 8, rel);
66ece8a530Spatrick *loc = val;
67ece8a530Spatrick break;
68ece8a530Spatrick case R_MSP430_16:
69ece8a530Spatrick case R_MSP430_16_PCREL:
70ece8a530Spatrick case R_MSP430_16_BYTE:
71ece8a530Spatrick case R_MSP430_16_PCREL_BYTE:
72bb684c34Spatrick checkIntUInt(loc, val, 16, rel);
73ece8a530Spatrick write16le(loc, val);
74ece8a530Spatrick break;
75ece8a530Spatrick case R_MSP430_32:
76bb684c34Spatrick checkIntUInt(loc, val, 32, rel);
77ece8a530Spatrick write32le(loc, val);
78ece8a530Spatrick break;
79ece8a530Spatrick case R_MSP430_10_PCREL: {
80ece8a530Spatrick int16_t offset = ((int16_t)val >> 1) - 1;
81bb684c34Spatrick checkInt(loc, offset, 10, rel);
82ece8a530Spatrick write16le(loc, (read16le(loc) & 0xFC00) | (offset & 0x3FF));
83ece8a530Spatrick break;
84ece8a530Spatrick }
85ece8a530Spatrick default:
86bb684c34Spatrick error(getErrorLocation(loc) + "unrecognized relocation " +
87bb684c34Spatrick toString(rel.type));
88ece8a530Spatrick }
89ece8a530Spatrick }
90ece8a530Spatrick
getMSP430TargetInfo()91bb684c34Spatrick TargetInfo *elf::getMSP430TargetInfo() {
92ece8a530Spatrick static MSP430 target;
93ece8a530Spatrick return ⌖
94ece8a530Spatrick }
95