aboutsummaryrefslogtreecommitdiff
path: root/src/libmach/loc.c
blob: 00ce2e6804bdf8069171def9b8142eb41d8f6759 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <mach.h>

int
locfmt(Fmt *fmt)
{
	Loc l;

	l = va_arg(fmt->args, Loc);
	switch(l.type){
	default:
		return fmtprint(fmt, "<loc%d>", l.type);
	case LCONST:
		return fmtprint(fmt, "0x%lux", l.addr);
	case LADDR:
		return fmtprint(fmt, "*0x%lux", l.addr);
	case LOFFSET:
		return fmtprint(fmt, "%ld(%s)", l.offset, l.reg);
	case LREG:
		return fmtprint(fmt, "%s", l.reg);
	}
}

int
loccmp(Loc *a, Loc *b)
{
	int i;

	if(a->type < b->type)
		return -1;
	if(a->type > b->type)
		return 1;
	switch(a->type){
	default:
		return 0;
	case LADDR:
		if(a->addr < b->addr)
			return -1;
		if(a->addr > b->addr)
			return 1;
		return 0;
	case LOFFSET:
		i = strcmp(a->reg, b->reg);
		if(i != 0)
			return i;
		if(a->offset < b->offset)
			return -1;
		if(a->offset > b->offset)
			return 1;
		return 0;
	case LREG:
		return strcmp(a->reg, b->reg);
	}
}

int
lget1(Map *map, Regs *regs, Loc loc, uchar *a, uint n)
{
	if(locsimplify(map, regs, loc, &loc) < 0)
		return -1;
	if(loc.type == LADDR)
		return get1(map, loc.addr, a, n);
	/* could do more here - i'm lazy */
	werrstr("bad location for lget1");
	return -1;
}

int
lget2(Map *map, Regs *regs, Loc loc, u16int *u)
{
	u64int ul;

	if(locsimplify(map, regs, loc, &loc) < 0)
		return -1;
	if(loc.type == LADDR)
		return get2(map, loc.addr, u);
	if(loc.type == LCONST){
		*u = loc.addr;
		return 0;
	}
	if(loc.type == LREG){
		if(rget(regs, loc.reg, &ul) < 0)
			return -1;
		*u = ul;
		return 0;
	}
	werrstr("bad location for lget2");
	return -1;
}

int
lget4(Map *map, Regs *regs, Loc loc, u32int *u)
{
	u64int ul;

	if(locsimplify(map, regs, loc, &loc) < 0)
		return -1;
	if(loc.type == LADDR)
		return get4(map, loc.addr, u);
	if(loc.type == LCONST){
		*u = loc.addr;
		return 0;
	}
	if(loc.type == LREG){
		if(rget(regs, loc.reg, &ul) < 0)
			return -1;
		*u = ul;
		return 0;
	}
	werrstr("bad location for lget4");
	return -1;
}

int
lgeta(Map *map, Regs *regs, Loc loc, u64int *u)
{
	u32int v;

	if(machcpu == &machamd64)
		return lget8(map, regs, loc, u);
	if(lget4(map, regs, loc, &v) < 0)
		return -1;
	*u = v;
	return 4;
}

int
lget8(Map *map, Regs *regs, Loc loc, u64int *u)
{
	u64int ul;

	if(locsimplify(map, regs, loc, &loc) < 0)
		return -1;
	if(loc.type == LADDR)
		return get8(map, loc.addr, u);
	if(loc.type == LCONST){
		*u = loc.addr;
		return 0;
	}
	if(loc.type == LREG){
		if(rget(regs, loc.reg, &ul) < 0)
			return -1;
		*u = ul;
		return 0;
	}
	werrstr("bad location for lget8");
	return -1;
}

int
lput1(Map *map, Regs *regs, Loc loc, uchar *a, uint n)
{
	if(locsimplify(map, regs, loc, &loc) < 0)
		return -1;
	if(loc.type == LADDR)
		return put1(map, loc.addr, a, n);
	/* could do more here - i'm lazy */
	werrstr("bad location for lput1");
	return -1;
}

int
lput2(Map *map, Regs *regs, Loc loc, u16int u)
{
	if(locsimplify(map, regs, loc, &loc) < 0)
		return -1;
	if(loc.type == LADDR)
		return put2(map, loc.addr, u);
	if(loc.type == LREG)
		return rput(regs, loc.reg, u);
	werrstr("bad location for lput2");
	return -1;
}

int
lput4(Map *map, Regs *regs, Loc loc, u32int u)
{
	if(locsimplify(map, regs, loc, &loc) < 0)
		return -1;
	if(loc.type == LADDR)
		return put4(map, loc.addr, u);
	if(loc.type == LREG)
		return rput(regs, loc.reg, u);
	werrstr("bad location for lput4");
	return -1;
}

int
lput8(Map *map, Regs *regs, Loc loc, u64int u)
{
	if(locsimplify(map, regs, loc, &loc) < 0)
		return -1;
	if(loc.type == LADDR)
		return put8(map, loc.addr, u);
	if(loc.type == LREG)
		return rput(regs, loc.reg, u);
	werrstr("bad location for lput8");
	return -1;
}

static Loc zl;

Loc
locaddr(u64int addr)
{
	Loc l;

	l = zl;
	l.type = LADDR;
	l.addr = addr;
	return l;
}

Loc
locindir(char *reg, long offset)
{
	Loc l;

	l = zl;
	l.type = LOFFSET;
	l.reg = reg;
	l.offset = offset;
	l.addr = 0;	/* SHUT UP GCC 4.0 */
	return l;
}

Loc
locconst(u64int con)
{
	Loc l;

	l = zl;
	l.type = LCONST;
	l.addr = con;
	return l;
}

Loc
locnone(void)
{
	Loc l;

	l = zl;
	l.type = LNONE;
	return l;
}

Loc
locreg(char *reg)
{
	Loc l;

	l = zl;
	l.type = LREG;
	l.reg = reg;
	return l;
}

int
locsimplify(Map *map, Regs *regs, Loc loc, Loc *newloc)
{
	u64int u;

	if(loc.type == LOFFSET){
		if(rget(regs, loc.reg, &u) < 0)
			return -1;
		*newloc = locaddr(u + loc.offset);
	}else
		*newloc = loc;
	return 0;
}