forked from eugenechevski/CodeGeneratorSPL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmachine_types.c
More file actions
executable file
·100 lines (89 loc) · 3.23 KB
/
machine_types.c
File metadata and controls
executable file
·100 lines (89 loc) · 3.23 KB
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
// $Id: machine_types.c,v 1.2 2024/10/23 13:38:20 leavens Exp $
#include "machine_types.h"
#include "utilities.h"
// Return the sign-extended equivalent of i
word_type machine_types_sgnExt(immediate_type i) {
return i; // preserves the value
}
// Return the zero-extended equivalent of i
uword_type machine_types_zeroExt(immediate_type i) {
return (uword_type) i;
}
// Return the offset given by o,
// which is the sign extension of o
word_type machine_types_formOffset(immediate_type o) {
return machine_types_sgnExt(o);
}
// Return an address formed by concatenating the instruction address, a,
// with the high-order (4) bits of the PC.
address_type machine_types_formAddress(address_type PC, address_type a) {
return ((0xF0000000 & PC) | (0x0FFFFFF & a));
}
// Check that o can be represented as an offset field in an instruction
// bail with an error message if not (so doesn't return if wrong)
void machine_types_check_fits_in_offset(word_type o)
{
if (o > NINEBITSMAXSIGNED) {
bail_with_error("Offset is too large: %d", o);
} else if (o < NINEBITSMINSIGNED) {
bail_with_error("Offset is too small: %d", o);
}
}
// Check that arg can be represented as an arg field in an instruction,
// bail with an error message if not (so doesn't return if wrong)
void machine_types_check_fits_in_arg(word_type arg)
{
if (arg > TWELVEBITSMAXSIGNED) {
bail_with_error("12 bit argument is too large: %d", arg);
} else if (arg < TWELVEBITSMINSIGNED) {
bail_with_error("12 bit argument is too small: %d", arg);
}
}
// Check that s can be represented as a shift field in an instruction,
// bail with an error message if not (so doesn't return if wrong)
void machine_types_check_fits_in_shift(word_type s)
{
if (s > TWELVEBITSMAXUNSIGNED) {
bail_with_error("Shift is too large: %u", s);
}
}
// Check that immed can be represented as a signed immediate field,
// bail with an error message if not (so doesn't return if wrong)
void machine_types_check_fits_in_immed(word_type immed)
{
if (immed > SIXTEENBITSMAXSIGNED) {
bail_with_error("Immediate argument is too large: %d",
immed);
} else if (immed < SIXTEENBITSMINSIGNED) {
bail_with_error("Immediate argument is too small: %d",
immed);
}
}
// Check that arg can be represented as unsigned immediate field,
// bail with an error message if not (so doesn't return if wrong)
void machine_types_check_fits_in_uimmed(word_type arg)
{
if (arg > SIXTEENBITSMAXUNSIGNED) {
bail_with_error("Unsigned immediate argument is too large: %u", arg);
}
}
// Check that addr can be represented as an address field in an instruction,
// bail with an error message if not (so doesn't return if wrong)
void machine_types_check_fits_in_addr(address_type addr)
{
if (addr > TWENTYEIGHTBITSMAXUNSIGNED) {
bail_with_error("Address is too large: %u", addr);
}
}
// Return the nearest multiple of BYTES_PER_WORD
// that is greater than or equal to n
int machine_types_round_up_to_wordsize(unsigned int n)
{
int rem = n % BYTES_PER_WORD;
if (rem == 0) {
return n;
} else {
// assert(((n + (BYTES_PER_WORD - rem)) % BYTES_PER_WORD) == 0);
return n + (BYTES_PER_WORD - rem);
}
}