101 lines
3.9 KiB
Python
101 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""Standalone CMOC preprocessing regression checks (set VCC_TOOLCHAIN to test another runtime)."""
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
RUNTIME = Path(os.environ.get('VCC_TOOLCHAIN', ROOT / 'platforms/macos/LocalResources/ToolchainBundle'))
|
|
|
|
|
|
@unittest.skipUnless((RUNTIME / 'bin/cpp').is_file(), 'Locally staged compiler runtime unavailable')
|
|
class InlineAssemblyCompatibility(unittest.TestCase):
|
|
def preprocess(self, source, **files):
|
|
with tempfile.TemporaryDirectory(prefix='vcc-cpp-regression-') as folder:
|
|
work = Path(folder)
|
|
(work / 'main.c').write_text(source)
|
|
for name, content in files.items(): (work / name).write_text(content)
|
|
return subprocess.run([str(RUNTIME / 'bin/cpp'), '-xc++', '-undef', '-nostdinc', 'main.c'],
|
|
cwd=work, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
|
|
|
def test_assembly_macro_operands_preserve_c_token_boundaries(self):
|
|
result = self.preprocess('''#define COUNT 32
|
|
#define MINUS -
|
|
#define TEXT(x) #x
|
|
const char *literal = "asm { ldb #COUNT }";
|
|
const char *words = TEXT(hello world);
|
|
int expression(int a, int b) { return a-MINUS b; }
|
|
void f(void) {
|
|
asm {
|
|
ldb #COUNT
|
|
leax -COUNT,x
|
|
fcc "hello world"
|
|
}
|
|
}
|
|
''')
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertRegex(result.stdout, r'ldb #32\b')
|
|
self.assertRegex(result.stdout, r'leax -32,x\b')
|
|
self.assertRegex(result.stdout, r'a-\s+-\s+b')
|
|
self.assertIn('"asm { ldb #COUNT }"', result.stdout)
|
|
self.assertIn('"hello world"', result.stdout)
|
|
|
|
def test_multiple_blocks_headers_and_source_markers(self):
|
|
result = self.preprocess('''#include "values.h"
|
|
#line 91 "game/main.c"
|
|
void a(void) { asm {
|
|
ldb #VALUE
|
|
} }
|
|
void b(void) { asm {
|
|
leax -VALUE,x
|
|
} }
|
|
''', **{'values.h': '#define VALUE 17\n'})
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertIn('# 91 "game/main.c"', result.stdout)
|
|
self.assertIn('ldb #17', result.stdout)
|
|
self.assertIn('leax -17,x', result.stdout)
|
|
|
|
def test_c_invalid_token_paste_still_fails(self):
|
|
result = self.preprocess('''#define BAD(a,b) a##b
|
|
int value = BAD(+,*);
|
|
void f(void) { asm { rts } }
|
|
''')
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertIn('error', result.stderr)
|
|
|
|
def test_preprocessor_failure_preserves_source_line(self):
|
|
result = self.preprocess('#line 72 "broken.c"\n#error EXPECTED_FAILURE\n')
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertIn(':72: error', result.stderr)
|
|
self.assertIn('EXPECTED_FAILURE', result.stderr)
|
|
|
|
def test_real_machine_code_matches_macro_immediate(self):
|
|
with tempfile.TemporaryDirectory(prefix='vcc-cpp-code-') as folder:
|
|
work = Path(folder)
|
|
(work / 'main.c').write_text('''#include <cmoc.h>
|
|
#define VALUE 42
|
|
asm unsigned char number(void) {
|
|
asm {
|
|
ldb #VALUE
|
|
rts
|
|
}
|
|
}
|
|
int main(void) { return number() != 42; }
|
|
''')
|
|
env = dict(os.environ, PATH=str(RUNTIME / 'bin') + ':/usr/bin:/bin',
|
|
DEVELOPER_DIR='/VCC-no-developer-tools', SDKROOT='/VCC-no-sdk')
|
|
result = subprocess.run([str(RUNTIME / 'bin/cmoc'), '--coco', '--intermediate', '-O0',
|
|
'-o', 'main.bin', 'main.c'], cwd=work, env=env,
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
|
self.assertEqual(result.returncode, 0, result.stdout)
|
|
listing = (work / 'main.lst').read_text()
|
|
self.assertRegex(listing, r'C62A\s+.*ldb\s+#42')
|
|
self.assertIn(bytes.fromhex('c62a39'), (work / 'main.bin').read_bytes())
|
|
|
|
|
|
if __name__ == '__main__': unittest.main()
|