Skip to content

Commit 2fa3233

Browse files
committed
Add support for loongarch
1 parent ec815ed commit 2fa3233

File tree

2 files changed

+133
-6
lines changed

2 files changed

+133
-6
lines changed

src/libply/Makefile.am

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ AM_YFLAGS = -d -Wall
88
AM_LFLAGS = --header-file=lexer.h
99

1010
EXTRA_DIST = grammar.c grammar.h lexer.c lexer.h \
11-
arch/aarch64.c \
12-
arch/arm.c \
13-
arch/mips.c \
14-
arch/powerpc.c \
15-
arch/riscv32.c \
16-
arch/riscv64.c \
11+
arch/aarch64.c \
12+
arch/arm.c \
13+
arch/loongarch64.c \
14+
arch/mips.c \
15+
arch/powerpc.c \
16+
arch/riscv32.c \
17+
arch/riscv64.c \
1718
arch/x86_64.c
1819

1920
BUILT_SOURCES = grammar.h lexer.h

src/libply/arch/loongarch64.c

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright Tobias Waldekranz <[email protected]>
3+
*
4+
* SPDX-License-Identifier: GPL-2.0
5+
*/
6+
7+
#include <assert.h>
8+
9+
#include <ply/internal.h>
10+
11+
#define arch_typedef(_a, _t) { \
12+
.ttype = T_TYPEDEF, \
13+
.tdef = { .name = #_a, .type = _t }, \
14+
}
15+
16+
struct type t_s8 = arch_typedef(s8, &t_schar);
17+
struct type t_u8 = arch_typedef(u8, &t_uchar);
18+
struct type t_s16 = arch_typedef(s16, &t_sshort);
19+
struct type t_u16 = arch_typedef(u16, &t_ushort);
20+
struct type t_s32 = arch_typedef(s32, &t_sint);
21+
struct type t_u32 = arch_typedef(u32, &t_uint);
22+
struct type t_s64 = arch_typedef(s64, &t_slong);
23+
struct type t_u64 = arch_typedef(u64, &t_ulong);
24+
25+
static int reg_fprint(struct type *t, FILE *fp, const void *data)
26+
{
27+
return fprintf(fp, "%#lx", *((unsigned long *)data));
28+
}
29+
30+
struct type t_reg_t = {
31+
.ttype = T_TYPEDEF,
32+
.tdef = {
33+
.name = "reg_t",
34+
.type = &t_ulong,
35+
},
36+
37+
.fprint = reg_fprint,
38+
};
39+
40+
struct tfield f_pt_regs_fields[] = {
41+
{ .name = "pc", .type = &t_reg_t },
42+
{ .name = "ra", .type = &t_reg_t },
43+
{ .name = "tp", .type = &t_reg_t },
44+
{ .name = "sp", .type = &t_reg_t },
45+
{ .name = "a0", .type = &t_reg_t },
46+
{ .name = "a1", .type = &t_reg_t },
47+
{ .name = "a2", .type = &t_reg_t },
48+
{ .name = "a3", .type = &t_reg_t },
49+
{ .name = "a4", .type = &t_reg_t },
50+
{ .name = "a5", .type = &t_reg_t },
51+
{ .name = "a6", .type = &t_reg_t },
52+
{ .name = "a7", .type = &t_reg_t },
53+
{ .name = "t0", .type = &t_reg_t },
54+
{ .name = "t1", .type = &t_reg_t },
55+
{ .name = "t2", .type = &t_reg_t },
56+
{ .name = "t3", .type = &t_reg_t },
57+
{ .name = "t4", .type = &t_reg_t },
58+
{ .name = "t5", .type = &t_reg_t },
59+
{ .name = "t6", .type = &t_reg_t },
60+
{ .name = "t7", .type = &t_reg_t },
61+
{ .name = "t8", .type = &t_reg_t },
62+
{ .name = "r21", .type = &t_reg_t },
63+
{ .name = "fp", .type = &t_reg_t },
64+
{ .name = "s0", .type = &t_reg_t },
65+
{ .name = "s1", .type = &t_reg_t },
66+
{ .name = "s2", .type = &t_reg_t },
67+
{ .name = "s3", .type = &t_reg_t },
68+
{ .name = "s4", .type = &t_reg_t },
69+
{ .name = "s5", .type = &t_reg_t },
70+
{ .name = "s6", .type = &t_reg_t },
71+
{ .name = "s7", .type = &t_reg_t },
72+
{ .name = "s8", .type = &t_reg_t },
73+
74+
{ .type = NULL }
75+
};
76+
77+
struct type t_pt_regs = {
78+
.ttype = T_STRUCT,
79+
80+
.sou = {
81+
.name = "pt_regs",
82+
.fields = f_pt_regs_fields,
83+
},
84+
};
85+
86+
struct type *arch_types[] = {
87+
&t_s8, &t_u8,
88+
&t_s16, &t_u16,
89+
&t_s32, &t_u32,
90+
&t_s64, &t_u64,
91+
&t_reg_t, &t_pt_regs,
92+
93+
NULL
94+
};
95+
96+
const char *arch_register_argument(int num)
97+
{
98+
switch (num) {
99+
case 0: return "a0";
100+
case 1: return "a1";
101+
case 2: return "a2";
102+
case 3: return "a3";
103+
case 4: return "a4";
104+
case 5: return "a5";
105+
case 6: return "a6";
106+
}
107+
108+
return NULL;
109+
}
110+
111+
const char *arch_register_pc(void)
112+
{
113+
return "pc";
114+
}
115+
116+
const char *arch_register_return(void)
117+
{
118+
return "a0";
119+
}
120+
121+
__attribute__((constructor))
122+
static void arch_init(void)
123+
{
124+
type_struct_layout(&t_pt_regs);
125+
type_add_list(arch_types);
126+
}

0 commit comments

Comments
 (0)