summaryrefslogtreecommitdiff
path: root/tests/tst-line-runner.sh
blob: ed62877fadf6db1a04c396ebe75445e833f70ff5 (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
#!/bin/sh
set -eu

: "${CPU_AS:?CPU_AS is not set}"

tmp="${TMPDIR:-/tmp}/libcpu-line-$$"
trap 'rm -rf "$tmp"' EXIT HUP INT TERM
mkdir -p "$tmp"

# --help remains a compact public interface.  Preprocessor source-control
# details belong to the documentation, not to the command-line synopsis.
"$CPU_AS" --help >"$tmp/help.txt" 2>&1
grep -q -- '--help' "$tmp/help.txt"
grep -q -- '-o <file>' "$tmp/help.txt"
if grep -q -- '#line' "$tmp/help.txt"; then
  echo "mcpu-as --help unexpectedly documents obsolete #line" >&2
  exit 1
fi

# The assembler accepts the usual UNIX-style option ordering.  -c is no
# longer part of the interface because mcpu-as always produces ET_REL.
cat >"$tmp/cli.s" <<'SRC'
.text
.global helper
helper:
        RET
SRC
"$CPU_AS" -o "$tmp/cli-before.o" "$tmp/cli.s" >/dev/null
"$CPU_AS" "$tmp/cli.s" -o "$tmp/cli-after.o" >/dev/null
test -s "$tmp/cli-before.o"
test -s "$tmp/cli-after.o"
cmp "$tmp/cli-before.o" "$tmp/cli-after.o"
if "$CPU_AS" -c "$tmp/cli.s" -o "$tmp/cli-old.o" >"$tmp/out.txt" 2>"$tmp/err.txt"; then
  echo "mcpu-as unexpectedly accepted obsolete -c" >&2
  exit 1
fi
grep -Fq 'mcpu-as: unknown option -c' "$tmp/err.txt"

# Native diagnostics count every ordinary physical source line, including a
# zero-length line.
cat >"$tmp/physical.s" <<'SRC'
.text

BAD_OPCODE
SRC
if "$CPU_AS" -o "$tmp/physical.o" "$tmp/physical.s" >"$tmp/out.txt" 2>"$tmp/err.txt"; then
  echo "mcpu-as unexpectedly accepted invalid physical-line source" >&2
  exit 1
fi
grep -Fq "$tmp/physical.s:3: error:" "$tmp/err.txt"

# Preprocessor line markers set the logical number and optional filename of the
# source line immediately following the marker.  Trailing preprocessor flags
# are deliberately ignored by mcpu-as.
cat >"$tmp/marker.S" <<'SRC'
# 7 "included source.s" 1 3 4 anything-after-the-filename-is-ignored
.text
THIS_IS_NOT_AN_INSTRUCTION
SRC
if "$CPU_AS" -o "$tmp/marker.o" "$tmp/marker.S" >"$tmp/out.txt" 2>"$tmp/err.txt"; then
  echo "mcpu-as unexpectedly accepted invalid marker source" >&2
  exit 1
fi
grep -Fq 'included source.s:8: error:' "$tmp/err.txt"

# The logical counter continues across empty ordinary lines.
cat >"$tmp/blank-lines.S" <<'SRC'
# 4 "dyn.s"

.text
helper:
BAD_OPCODE
SRC
if "$CPU_AS" -o "$tmp/blank-lines.o" "$tmp/blank-lines.S" >"$tmp/out.txt" 2>"$tmp/err.txt"; then
  echo "mcpu-as unexpectedly accepted invalid blank-line source" >&2
  exit 1
fi
grep -Fq 'dyn.s:7: error:' "$tmp/err.txt"

# Omitting the filename retains the previous logical filename.  Leading zeroes
# remain decimal: 0200 means line 200, not octal 128.
cat >"$tmp/preserve.s" <<'SRC'
# 100 "first-name.s"
# 0200
.text
BAD_OPCODE
SRC
if "$CPU_AS" -o "$tmp/preserve.o" "$tmp/preserve.s" >"$tmp/out.txt" 2>"$tmp/err.txt"; then
  echo "mcpu-as unexpectedly accepted invalid preserve test" >&2
  exit 1
fi
grep -Fq 'first-name.s:201: error:' "$tmp/err.txt"

# Zero is accepted.  Here .text is logical line 0 and the bad opcode is line 1.
cat >"$tmp/zero.s" <<'SRC'
# 0 "zero.s"
.text
BAD_OPCODE
SRC
if "$CPU_AS" -o "$tmp/zero.o" "$tmp/zero.s" >"$tmp/out.txt" 2>"$tmp/err.txt"; then
  echo "mcpu-as unexpectedly accepted invalid zero-line test" >&2
  exit 1
fi
grep -Fq 'zero.s:1: error:' "$tmp/err.txt"

# The accepted decimal marker range remains 0..2147483647.
cat >"$tmp/range.s" <<'SRC'
# 2147483648 "too-large.s"
.text
SRC
if "$CPU_AS" -o "$tmp/range.o" "$tmp/range.s" >"$tmp/out.txt" 2>"$tmp/err.txt"; then
  echo "mcpu-as unexpectedly accepted an out-of-range line marker" >&2
  exit 1
fi
grep -Fq 'range.s:1: error: preprocessor line marker value is outside 0..2147483647' "$tmp/err.txt"

# #pragma and #lang are passed through by preprocessing stages for other
# consumers.  mcpu-as ignores them and they do not advance the logical counter.
cat >"$tmp/meta.s" <<'SRC'
# 100 "meta.s"
#pragma once
#lang "mcpu"
BAD_OPCODE
SRC
if "$CPU_AS" -o "$tmp/meta.o" "$tmp/meta.s" >"$tmp/out.txt" 2>"$tmp/err.txt"; then
  echo "mcpu-as unexpectedly accepted invalid pragma/lang source" >&2
  exit 1
fi
grep -Fq 'meta.s:100: error:' "$tmp/err.txt"

# Other '#' lines are discarded as assembler syntax but still count as normal
# logical source lines.
cat >"$tmp/hash.s" <<'SRC'
# 50 "hash.s"
#define SOMETHING 1
#unknown whatever
BAD_OPCODE
SRC
if "$CPU_AS" -o "$tmp/hash.o" "$tmp/hash.s" >"$tmp/out.txt" 2>"$tmp/err.txt"; then
  echo "mcpu-as unexpectedly accepted invalid generic-hash source" >&2
  exit 1
fi
grep -Fq 'hash.s:52: error:' "$tmp/err.txt"

# Historical #line is no longer a source-location directive.  It is simply an
# unrecognized '#' line: ignored as syntax, counted as a normal source line,
# and it must not replace the diagnostic filename or line counter.
cat >"$tmp/obsolete-line.s" <<'SRC'
#line 700 "obsolete.s"
.text
BAD_OPCODE
SRC
if "$CPU_AS" -o "$tmp/obsolete-line.o" "$tmp/obsolete-line.s" >"$tmp/out.txt" 2>"$tmp/err.txt"; then
  echo "mcpu-as unexpectedly accepted invalid obsolete-#line source" >&2
  exit 1
fi
grep -Fq "$tmp/obsolete-line.s:3: error:" "$tmp/err.txt"
if grep -Fq 'obsolete.s:' "$tmp/err.txt"; then
  echo "obsolete #line unexpectedly changed source coordinates" >&2
  exit 1
fi

# A normal preprocessor marker is accepted independently of the input suffix.
cat >"$tmp/good.s" <<'SRC'
# 42 "generated/original source.s" 1
.text
.global helper
helper:
        RET
SRC
"$CPU_AS" -o "$tmp/good.o" "$tmp/good.s" >/dev/null
test -s "$tmp/good.o"

echo "libmcpu mcpu-as preprocessor source-control regression: PASS"