26d900910ab4a9ac1efce5639e255ca037383ede
2 * Copyright (c) 2017 The University of Virginia
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Alec Roelke
34 #include <type_traits>
38 #define CIOP(op, r, imm) asm volatile(op " %0,%1" : "+r" (r) : "i" (imm));
39 #define CROP(op, rd, rs) asm volatile(op " %0,%1" : "+r" (rd) : "r" (rs))
45 c_li(const int8_t imm
)
48 CIOP("c.li", rd
, imm
);
53 c_lui(const int8_t imm
)
56 CIOP("c.lui", rd
, imm
);
61 c_addi(int64_t r
, const int8_t imm
)
63 CIOP("c.addi", r
, imm
);
68 c_addiw(int64_t r
, const int8_t imm
)
70 CIOP("c.addiw", r
, imm
);
75 c_addi4spn(const int16_t imm
)
78 asm volatile("c.addi4spn %0,sp,%1" : "=r" (rd
) : "i" (imm
));
83 c_slli(uint64_t r
, uint8_t shamt
)
85 CIOP("c.slli", r
, shamt
);
90 c_srli(uint64_t r
, uint8_t shamt
)
92 CIOP("c.srli", r
, shamt
);
97 c_srai(int64_t r
, uint8_t shamt
)
99 CIOP("c.srai", r
, shamt
);
104 c_andi(uint64_t r
, uint8_t imm
)
106 CIOP("c.andi", r
, imm
);
114 CROP("c.mv", rd
, rs
);
119 c_add(int64_t rd
, int64_t rs
)
121 CROP("c.add", rd
, rs
);
126 c_and(int64_t rd
, int64_t rs
)
128 CROP("c.and", rd
, rs
);
133 c_or(int64_t rd
, int64_t rs
)
135 CROP("c.or", rd
, rs
);
140 c_xor(int64_t rd
, int64_t rs
)
142 CROP("c.xor", rd
, rs
);
147 c_sub(int64_t rd
, int64_t rs
)
149 CROP("c.sub", rd
, rs
);
154 c_addw(int64_t rd
, int64_t rs
)
156 CROP("c.addw", rd
, rs
);
161 c_subw(int64_t rd
, int64_t rs
)
163 CROP("c.subw", rd
, rs
);
167 template<typename M
, typename R
> inline R
174 asm volatile("c.lw %0,0(%1)" : "=r" (r
) : "r" (&m
) : "memory");
177 if (std::is_floating_point
<M
>::value
)
178 asm volatile("c.fld %0,0(%1)" : "=f" (r
) : "r" (&m
) : "memory");
180 asm volatile("c.ld %0,0(%1)" : "=r" (r
) : "r" (&m
) : "memory");
186 template<typename M
> inline M
193 asm volatile("c.sw %0,0(%1)" : : "r" (rs
), "r" (&mem
) : "memory");
196 if (std::is_floating_point
<M
>::value
)
197 asm volatile("c.fsd %0,0(%1)" : : "f" (rs
), "r" (&mem
) : "memory");
199 asm volatile("c.sd %0,0(%1)" : : "r" (rs
), "r" (&mem
) : "memory");
208 asm volatile goto("c.j %l[jallabel]" : : : : jallabel
);
218 asm volatile("auipc %0,0;"
231 asm volatile("auipc %0,0;"
245 asm volatile goto("c.beqz %0,%l[beqlabel]"
258 asm volatile goto("c.bnez %0,%l[beqlabel]"