mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Compare commits
212 Commits
v0.4.2-pre
...
v0.5.0-rc1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b61a187b19 | ||
|
|
6382f13647 | ||
|
|
714d39c86f | ||
|
|
f11241c2ae | ||
|
|
cb47ac8b97 | ||
|
|
028e7af7d1 | ||
|
|
e141da1d94 | ||
|
|
35367282e4 | ||
|
|
4c0fa6732e | ||
|
|
e9d9a44687 | ||
|
|
611bd46e0b | ||
|
|
e67254093e | ||
|
|
3ce3d2f662 | ||
|
|
3da9d81071 | ||
|
|
88646093ca | ||
|
|
0a0427afbb | ||
|
|
88048cdcd7 | ||
|
|
c878ff8775 | ||
|
|
a4049a3f1b | ||
|
|
8a75cc5051 | ||
|
|
b674c5fcaa | ||
|
|
a6d3df7ac9 | ||
|
|
365484ef18 | ||
|
|
5e2bd35239 | ||
|
|
6655e04ef0 | ||
|
|
40c6b840f8 | ||
|
|
5d6e0677d9 | ||
|
|
56071599e7 | ||
|
|
c637447d5d | ||
|
|
ac2cefdd87 | ||
|
|
0774f5eb9d | ||
|
|
76d8862900 | ||
|
|
1dafc1c762 | ||
|
|
63d15ac8c9 | ||
|
|
1f579deaff | ||
|
|
fad48326f8 | ||
|
|
e7d6ddf593 | ||
|
|
953f79c0d9 | ||
|
|
3c5e1caa7c | ||
|
|
d4028fff10 | ||
|
|
dd892d61d8 | ||
|
|
a09f2d4115 | ||
|
|
dafef5a953 | ||
|
|
929e2a4490 | ||
|
|
cc1129093d | ||
|
|
72911ada2d | ||
|
|
037bc7abb3 | ||
|
|
5fd636ac4b | ||
|
|
a6d844a9a5 | ||
|
|
bee62076c6 | ||
|
|
cd072d5e6a | ||
|
|
6ef3ee1391 | ||
|
|
c29b616f93 | ||
|
|
39c179aa32 | ||
|
|
5c1ae4ce22 | ||
|
|
efbfeca292 | ||
|
|
7dd34f1572 | ||
|
|
748e7dd4c7 | ||
|
|
d049ffc0f0 | ||
|
|
8c0275480c | ||
|
|
76d6ef8695 | ||
|
|
c67a696a87 | ||
|
|
ee20d9010e | ||
|
|
2bc12447e2 | ||
|
|
cb61da8842 | ||
|
|
122ba6eba5 | ||
|
|
dddff0f450 | ||
|
|
a919f922a1 | ||
|
|
8f20620c16 | ||
|
|
96bce05be2 | ||
|
|
fc2bf3d11d | ||
|
|
8415ce3ed0 | ||
|
|
ebb5aab6db | ||
|
|
464a3a4892 | ||
|
|
88e1cc7302 | ||
|
|
b3c0db218d | ||
|
|
76446e6d00 | ||
|
|
6623b1dc45 | ||
|
|
70bbb098d3 | ||
|
|
1926065377 | ||
|
|
e3d355d976 | ||
|
|
a679e02246 | ||
|
|
592e9b3725 | ||
|
|
effc58241d | ||
|
|
3697065afc | ||
|
|
fa0fa4d5ac | ||
|
|
5acc48fa54 | ||
|
|
1487eebe79 | ||
|
|
993c034039 | ||
|
|
09f16bda4a | ||
|
|
41d544a4eb | ||
|
|
f28b4abafc | ||
|
|
ca1c934629 | ||
|
|
d5a00cf634 | ||
|
|
fb39c3a70e | ||
|
|
15ec6efc28 | ||
|
|
93d83e17dc | ||
|
|
df16e64fc6 | ||
|
|
a4ebb87858 | ||
|
|
5ef8e0a1f6 | ||
|
|
eb4952c188 | ||
|
|
57b734a7df | ||
|
|
5be1c0da62 | ||
|
|
b598911e96 | ||
|
|
cab9cb06a3 | ||
|
|
62bea23c49 | ||
|
|
7ce5cf1595 | ||
|
|
7e3fc1db03 | ||
|
|
77279984a5 | ||
|
|
669a392fcd | ||
|
|
51ce0b038a | ||
|
|
bd244e6865 | ||
|
|
a70ecba06f | ||
|
|
9d2d5cfcfe | ||
|
|
18f3c8ff9a | ||
|
|
895ec5564d | ||
|
|
7bb6f71f0b | ||
|
|
10e3f1a02b | ||
|
|
2a9d52871b | ||
|
|
c4fb5591ee | ||
|
|
c0ce1da4c3 | ||
|
|
aa27e714d4 | ||
|
|
6874f694e5 | ||
|
|
3690546795 | ||
|
|
7bc42d468b | ||
|
|
097999cad3 | ||
|
|
f82edaa7ec | ||
|
|
d8e8b796e7 | ||
|
|
900fd8cabc | ||
|
|
c20ac35074 | ||
|
|
255b8bf9ba | ||
|
|
ad6f17cd93 | ||
|
|
063a22ddf9 | ||
|
|
1d9cc01ae1 | ||
|
|
f31deb5010 | ||
|
|
0956d300c4 | ||
|
|
4ef490f3cb | ||
|
|
8f2a894b88 | ||
|
|
0e40543757 | ||
|
|
ce58f6d6be | ||
|
|
5aabb915ec | ||
|
|
0d9de01f9d | ||
|
|
f5b0eae9cd | ||
|
|
e6552064bf | ||
|
|
861cb552c4 | ||
|
|
417cceb0de | ||
|
|
165bd8cb71 | ||
|
|
e54e02dc77 | ||
|
|
2bf3b08d76 | ||
|
|
21b58b08b8 | ||
|
|
af530859f0 | ||
|
|
58739b0bf2 | ||
|
|
3e4c2fe712 | ||
|
|
bdfce25db0 | ||
|
|
2b6d9cd1e0 | ||
|
|
bf789dd7b3 | ||
|
|
9b6f01047c | ||
|
|
3fe2fa43bb | ||
|
|
ede982b50a | ||
|
|
319d775c13 | ||
|
|
44124319a6 | ||
|
|
462fd7539c | ||
|
|
f16e34b804 | ||
|
|
c3ccdc548e | ||
|
|
fd721ca480 | ||
|
|
eac365aef0 | ||
|
|
4de6266442 | ||
|
|
213d985e17 | ||
|
|
de76dcb8fb | ||
|
|
30fb6bde5e | ||
|
|
4f842a1248 | ||
|
|
cc4d455b8a | ||
|
|
0bb5efebfd | ||
|
|
b6bf7ae620 | ||
|
|
dc96cc6d1e | ||
|
|
642daf1a76 | ||
|
|
84edfb3d88 | ||
|
|
7e620bff81 | ||
|
|
0c55703438 | ||
|
|
6c57ad2226 | ||
|
|
9028fb5391 | ||
|
|
12dc49b60a | ||
|
|
7e1d20acdf | ||
|
|
5230104852 | ||
|
|
55be77be69 | ||
|
|
42b3a17356 | ||
|
|
4e1d79081c | ||
|
|
4ce4fdec71 | ||
|
|
05256946ac | ||
|
|
73396166aa | ||
|
|
4c5d5c7085 | ||
|
|
045a9e8b93 | ||
|
|
4419f0d54f | ||
|
|
b07aa00d5c | ||
|
|
e4f5df1306 | ||
|
|
dc62d60e9b | ||
|
|
71d8aeb4c2 | ||
|
|
0836f67d42 | ||
|
|
176a57a1e9 | ||
|
|
0d02355dbf | ||
|
|
6767d11c23 | ||
|
|
2dd9015dc6 | ||
|
|
217c10ddac | ||
|
|
822e4e7c44 | ||
|
|
0b1d01792d | ||
|
|
01637768cf | ||
|
|
6a8ae643d5 | ||
|
|
fd83d46ba0 | ||
|
|
914856342c | ||
|
|
91889fc14a | ||
|
|
7c8ec5a5ed | ||
|
|
effc6788eb |
3
.github/FUNDING.yml
vendored
Normal file
3
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
github: avivace
|
||||
patreon: gbdev01
|
||||
open_collective: gbdev
|
||||
8
.github/actions/doc_postproc.awk
vendored
8
.github/actions/doc_postproc.awk
vendored
@@ -7,6 +7,14 @@
|
||||
sub(/b><\/td/, "th");
|
||||
}
|
||||
|
||||
# The whole page is being generated, so it's not meant to contain any Liquid
|
||||
BEGIN {
|
||||
print "{% raw %}"
|
||||
}
|
||||
END {
|
||||
print "{% endraw %}"
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
in_synopsis = 0
|
||||
}
|
||||
|
||||
35
.github/actions/get-pages.sh
vendored
35
.github/actions/get-pages.sh
vendored
@@ -5,20 +5,21 @@ usage() {
|
||||
Usage: $0 [-h] [-r] <rgbds-www> <version>
|
||||
Copy renders from RGBDS repository to rgbds-www documentation
|
||||
Execute from the root folder of the RGBDS repo, checked out at the desired tag
|
||||
<rgbds-www> : Path to the '_documentation' folder in the rgbds-www repository
|
||||
<rgbds-www> : Path to the rgbds-www repository
|
||||
<version> : Version to be copied, such as 'v0.4.1' or 'master'
|
||||
|
||||
-h Display this help message
|
||||
-r Update "latest stable" redirection pages (use for releases, not master)
|
||||
-r Update "latest stable" redirection pages and add a new entry to the index
|
||||
(use for releases, not master)
|
||||
EOF
|
||||
}
|
||||
|
||||
update_redirects=0
|
||||
is_release=0
|
||||
bad_usage=0
|
||||
while getopts ":hr" opt; do
|
||||
case $opt in
|
||||
r)
|
||||
update_redirects=1
|
||||
is_release=1
|
||||
;;
|
||||
h)
|
||||
usage
|
||||
@@ -26,14 +27,12 @@ while getopts ":hr" opt; do
|
||||
;;
|
||||
\?)
|
||||
echo "Unknown option '$OPTARG'"
|
||||
if [ $bad_usage -eq 0 ]; then
|
||||
usage
|
||||
bad_usage=1
|
||||
fi
|
||||
bad_usage=1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
if [ $bad_usage -ne 0 ]; then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
shift $(($OPTIND - 1))
|
||||
@@ -52,7 +51,7 @@ PAGES=(
|
||||
[gbz80.7.html]=src/gbz80.7
|
||||
)
|
||||
WWWPATH="/docs"
|
||||
mkdir -p "$1/$2"
|
||||
mkdir -p "$1/_documentation/$2"
|
||||
|
||||
# `mandoc` uses a different format for referring to man pages present in the **current** directory
|
||||
# We want that format for RGBDS man pages, and the other one for the t=rest;
|
||||
@@ -66,7 +65,7 @@ stem="${page%.html}"
|
||||
manpage="${stem%.?}(${stem#*.})"
|
||||
descr="$(awk -v 'FS=.Nd ' '/.Nd/ { print $2; }' "${PAGES[$page]}")"
|
||||
|
||||
cat - >"$1/$2/$page" <<EOF
|
||||
cat >"$1/_documentation/$2/$page" <<EOF
|
||||
---
|
||||
layout: doc
|
||||
title: $manpage [$2]
|
||||
@@ -77,9 +76,9 @@ EOF
|
||||
if [ $stem = rgbasm.5 ]; then
|
||||
options+=,toc
|
||||
fi
|
||||
mandoc -Thtml -I os=Linux -O$options "${PAGES[$page]##*/}" | .github/actions/doc_postproc.awk >> "$1/$2/$page"
|
||||
if [ $update_redirects -ne 0 ]; then
|
||||
cat - >"$1/$page" <<EOF
|
||||
mandoc -Thtml -I os=Linux -O$options "${PAGES[$page]##*/}" | .github/actions/doc_postproc.awk >> "$1/_documentation/$2/$page"
|
||||
if [ $is_release -ne 0 ]; then
|
||||
cat - >"$1/_documentation/$page" <<EOF
|
||||
---
|
||||
redirect_to: $WWWPATH/$2/${page%.html}
|
||||
permalink: $WWWPATH/${page%.html}/
|
||||
@@ -89,7 +88,7 @@ description: RGBDS latest stable — $descr
|
||||
EOF
|
||||
fi
|
||||
done
|
||||
cat - >"$1/$2/index.html" <<EOF
|
||||
cat - >"$1/_documentation/$2/index.html" <<EOF
|
||||
---
|
||||
layout: doc_index
|
||||
permalink: /docs/$2/
|
||||
@@ -98,5 +97,13 @@ description: RGBDS $2 - Online manual
|
||||
---
|
||||
EOF
|
||||
|
||||
# If making a release, add a new entry right after `master`
|
||||
if [ $is_release -ne 0 ]; then
|
||||
awk '{ print }
|
||||
/"name": "master"/ { print "\t\t{\"name\": \"'$2'\", \"text\": \"'$2'\" }," }
|
||||
' "$1/_data/doc.json" >"$1/_data/doc.json.tmp"
|
||||
mv "$1/_data/doc.json"{.tmp,}
|
||||
fi
|
||||
|
||||
# Clean up
|
||||
rm "${PAGES[@]##*/}"
|
||||
|
||||
8
.github/actions/install_deps.sh
vendored
8
.github/actions/install_deps.sh
vendored
@@ -4,11 +4,15 @@ case `echo $1 | cut -d '-' -f 1` in
|
||||
sudo apt-get install -yq bison libpng-dev pkg-config
|
||||
;;
|
||||
macos)
|
||||
brew install libpng pkg-config md5sha1sum
|
||||
brew install bison libpng pkg-config md5sha1sum
|
||||
# For the version check below exclusively, re-do this before building
|
||||
export PATH="/usr/local/opt/bison/bin:$PATH"
|
||||
;;
|
||||
*)
|
||||
echo "WARNING: Cannot install deps for OS '$1'"
|
||||
;;
|
||||
esac
|
||||
|
||||
yacc --version
|
||||
bison --version
|
||||
make --version
|
||||
cmake --version
|
||||
|
||||
96
.github/workflows/create-release-artifacts.yaml
vendored
Normal file
96
.github/workflows/create-release-artifacts.yaml
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
name: "Create release artifacts"
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- v[0-9]*
|
||||
|
||||
jobs:
|
||||
windows:
|
||||
runs-on: windows-2019
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Get version from tag
|
||||
shell: bash
|
||||
run: | # Turn "refs/tags/vX.Y.Z" into "X.Y.Z"
|
||||
VERSION="${{ github.ref }}"
|
||||
echo "version=${VERSION##*/v}" >> $GITHUB_ENV
|
||||
- name: Get zlib, libpng and bison
|
||||
run: | # TODO: use an array
|
||||
$wc = New-Object System.Net.WebClient
|
||||
$wc.DownloadFile('https://www.zlib.net/zlib1211.zip', 'zlib.zip')
|
||||
$hash = (Get-FileHash "zlib.zip" -Algorithm SHA256).Hash
|
||||
if ($hash -ne 'd7510a8ee1918b7d0cad197a089c0a2cd4d6df05fee22389f67f115e738b178d') {
|
||||
Write-Host "zlib SHA256 mismatch! ($hash)"
|
||||
exit 1
|
||||
}
|
||||
$wc.DownloadFile('https://download.sourceforge.net/libpng/lpng1637.zip', 'libpng.zip')
|
||||
$hash = (Get-FileHash "libpng.zip" -Algorithm SHA256).Hash
|
||||
if ($hash -ne '3b4b1cbd0bae6822f749d39b1ccadd6297f05e2b85a83dd2ce6ecd7d09eabdf2') {
|
||||
Write-Host "libpng SHA256 mismatch! ($hash)"
|
||||
exit 1
|
||||
}
|
||||
$wc.DownloadFile('https://github.com/lexxmark/winflexbison/releases/download/v2.5.23/win_flex_bison-2.5.23.zip', 'winflexbison.zip')
|
||||
$hash = (Get-FileHash "winflexbison.zip" -Algorithm SHA256).Hash
|
||||
if ($hash -ne '6AA5C8EA662DA1550020A5804C28BE63FFAA53486DA9F6842E24C379EC422DFC') {
|
||||
Write-Host "bison SHA256 mismatch! ($hash)"
|
||||
}
|
||||
Expand-Archive -DestinationPath . "zlib.zip"
|
||||
Expand-Archive -DestinationPath . "libpng.zip"
|
||||
Expand-Archive -DestinationPath install_dir "winflexbison.zip"
|
||||
Move-Item zlib-1.2.11 zlib
|
||||
Move-Item lpng1637 libpng
|
||||
- name: Build 32-bit zlib
|
||||
run: | # BUILD_SHARED_LIBS causes the output DLL to be correctly called `zlib1.dll`
|
||||
cmake -S zlib -B zbuild32 -A Win32 -DCMAKE_INSTALL_PREFIX=install_dir -DBUILD_SHARED_LIBS=ON
|
||||
cmake --build zbuild32 --config Release
|
||||
cmake --install zbuild32
|
||||
- name: Build 32-bit libpng
|
||||
run: |
|
||||
cmake -S libpng -B pngbuild32 -A Win32 -DCMAKE_INSTALL_PREFIX=install_dir -DPNG_SHARED=ON -DPNG_STATIC=ON -DPNG_TESTS=OFF
|
||||
cmake --build pngbuild32 --config Release
|
||||
cmake --install pngbuild32
|
||||
- name: Build 32-bit Windows binaries
|
||||
run: |
|
||||
cmake -S . -B build32 -A Win32 -DCMAKE_INSTALL_PREFIX=install_dir -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build32 --config Release
|
||||
cmake --install build32
|
||||
- name: Package 32-bit binaries
|
||||
run: |
|
||||
Compress-Archive -LiteralPath @("install_dir/bin/rgbasm.exe", "install_dir/bin/rgblink.exe", "install_dir/bin/rgbfix.exe", "install_dir/bin/rgbgfx.exe", "install_dir/bin/zlib1.dll", "install_dir/bin/libpng16.dll") "rgbds-${{ env.version }}-win32.zip"
|
||||
- name: Build 64-bit zlib
|
||||
run: | # BUILD_SHARED_LIBS causes the output DLL to be correctly called `zlib1.dll`
|
||||
cmake -S zlib -B zbuild64 -A x64 -DCMAKE_INSTALL_PREFIX=install_dir -DBUILD_SHARED_LIBS=ON
|
||||
cmake --build zbuild64 --config Release
|
||||
cmake --install zbuild64
|
||||
- name: Build 64-bit libpng
|
||||
run: |
|
||||
cmake -S libpng -B pngbuild64 -A x64 -DCMAKE_INSTALL_PREFIX=install_dir -DPNG_SHARED=ON -DPNG_STATIC=ON -DPNG_TESTS=OFF
|
||||
cmake --build pngbuild64 --config Release
|
||||
cmake --install pngbuild64
|
||||
- name: Build 64-bit Windows binaries
|
||||
run: |
|
||||
cmake -S . -B build64 -A x64 -DCMAKE_INSTALL_PREFIX=install_dir -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build64 --config Release
|
||||
cmake --install build64
|
||||
- name: Package 64-bit binaries
|
||||
run: |
|
||||
Compress-Archive -LiteralPath @("install_dir/bin/rgbasm.exe", "install_dir/bin/rgblink.exe", "install_dir/bin/rgbfix.exe", "install_dir/bin/rgbgfx.exe", "install_dir/bin/zlib1.dll", "install_dir/bin/libpng16.dll") "rgbds-${{ env.version }}-win64.zip"
|
||||
- name: Package sources
|
||||
run: |
|
||||
make dist
|
||||
- name: Release
|
||||
uses: softprops/action-gh-release@v1
|
||||
with:
|
||||
body: |
|
||||
Please ensure that the three assets below work properly.
|
||||
Once that's done, replace this text with the changelog, un-draft the release, and update the `release` branch.
|
||||
By the way, if you forgot to update `include/version.h`, RGBASM's version test is gonna fail in the tag's regression testing! (Use `git push --delete origin <tag>` to delete it)
|
||||
draft: true # Don't publish the release quite yet...
|
||||
prerelease: ${{ contains(github.ref, '-rc') }}
|
||||
files: |
|
||||
rgbds-${{ env.version }}-win32.zip
|
||||
rgbds-${{ env.version }}-win64.zip
|
||||
rgbds-${{ env.version }}.tar.gz
|
||||
fail_on_unmatched_files: true
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
52
.github/workflows/create-release-docs.yml
vendored
Normal file
52
.github/workflows/create-release-docs.yml
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
name: "Create release docs"
|
||||
on:
|
||||
release:
|
||||
types:
|
||||
- released # This avoids triggering on pre-releases
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-18.04
|
||||
steps:
|
||||
- name: Checkout rgbds@release
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
path: rgbds
|
||||
- name: Checkout rgbds-www@master
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
repository: ${{ github.repository_owner }}/rgbds-www
|
||||
path: rgbds-www
|
||||
# `-O toc` was added in 1.14.5, but the repos only have 1.14.4
|
||||
- name: Build and install mandoc
|
||||
run: |
|
||||
sudo apt-get -qq update
|
||||
sudo apt-get install -yq zlib1g-dev
|
||||
wget 'http://mandoc.bsd.lv/snapshots/mandoc-1.14.5.tar.gz'
|
||||
tar xf mandoc-1.14.5.tar.gz
|
||||
cd mandoc-1.14.5
|
||||
./configure
|
||||
make
|
||||
sudo make install
|
||||
- name: Update pages
|
||||
working-directory: rgbds
|
||||
run: | # The ref appears to be in the format "refs/tags/<version>", so strip that
|
||||
./.github/actions/get-pages.sh -r ../rgbds-www ${GITHUB_REF##*/}
|
||||
- name: Push new pages
|
||||
working-directory: rgbds-www
|
||||
run: |
|
||||
mkdir -p -m 700 ~/.ssh
|
||||
cat > ~/.ssh/id_ed25519 <<<"${{ secrets.SSH_KEY_SECRET }}"
|
||||
chmod 0600 ~/.ssh/id_ed25519
|
||||
eval $(ssh-agent -s)
|
||||
ssh-add ~/.ssh/id_ed25519
|
||||
git config --global user.name "GitHub Action"
|
||||
git config --global user.email "community@gbdev.io"
|
||||
git add -A
|
||||
git commit -m "Create RGBDS ${GITHUB_REF##*/} documentation"
|
||||
if git remote | grep -q origin; then
|
||||
git remote set-url origin git@github.com:${{ github.repository_owner }}/rgbds-www.git
|
||||
else
|
||||
git remote add origin git@github.com:${{ github.repository_owner }}/rgbds-www.git
|
||||
fi
|
||||
git push origin master
|
||||
9
.github/workflows/testing.yml
vendored
9
.github/workflows/testing.yml
vendored
@@ -27,14 +27,19 @@ jobs:
|
||||
shell: bash
|
||||
run: |
|
||||
./.github/actions/install_deps.sh ${{ matrix.os }}
|
||||
# The `export` lines are to allow working on macOS...
|
||||
# Apple's base version is severely outdated, not even supporting -Wall,
|
||||
# but it overrides Homebrew's version nonetheless...
|
||||
- name: Build & install using Make
|
||||
run: |
|
||||
export PATH="/usr/local/opt/bison/bin:$PATH"
|
||||
make ${{ matrix.target }} -j Q= CC=${{ matrix.cc }}
|
||||
sudo make install -j Q=
|
||||
if: matrix.buildsys == 'make'
|
||||
- name: Build & install using CMake
|
||||
run: |
|
||||
cmake -S . -B build -DCMAKE_VERBOSE_MAKEFILE=ON ${{ matrix.cmakevars }}
|
||||
export PATH="/usr/local/opt/bison/bin:$PATH"
|
||||
cmake -S . -B build -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=${{ matrix.cc }} ${{ matrix.cmakevars }}
|
||||
cmake --build build
|
||||
cp build/src/rgb{asm,link,fix,gfx} .
|
||||
sudo cmake --install build
|
||||
@@ -128,7 +133,7 @@ jobs:
|
||||
strategy:
|
||||
matrix:
|
||||
bits: [32, 64]
|
||||
os: [ubuntu-latest]
|
||||
os: [ubuntu-18.04]
|
||||
include:
|
||||
- bits: 32
|
||||
arch: i686
|
||||
|
||||
1
.github/workflows/update-master-docs.yml
vendored
1
.github/workflows/update-master-docs.yml
vendored
@@ -17,7 +17,6 @@ on:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
if: secrets.SSH_KEY_SECRET != ''
|
||||
runs-on: ubuntu-18.04
|
||||
steps:
|
||||
- name: Checkout rgbds@master
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,6 +5,7 @@ rgbgfx
|
||||
rgbshim.sh
|
||||
*.o
|
||||
*.exe
|
||||
*.dll
|
||||
.checkpatch-camelcase.*
|
||||
CMakeCache.txt
|
||||
CMakeFiles
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
# SPDX-License-Identifier: MIT
|
||||
#
|
||||
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
cmake_policy(VERSION 3.0)
|
||||
# 3.9 required for LTO checks
|
||||
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
|
||||
|
||||
project(rgbds
|
||||
LANGUAGES C)
|
||||
@@ -23,13 +23,15 @@ if(srcdir STREQUAL bindir)
|
||||
message(FATAL_ERROR "Terminating configuration")
|
||||
endif()
|
||||
|
||||
include_directories("${PROJECT_SOURCE_DIR}/include")
|
||||
|
||||
option(SANITIZERS "Build with sanitizers enabled" OFF)
|
||||
option(MORE_WARNINGS "Turn on more warnings" OFF)
|
||||
option(SANITIZERS "Build with sanitizers enabled" OFF) # Ignored on MSVC
|
||||
option(MORE_WARNINGS "Turn on more warnings" OFF) # Ignored on MSVC
|
||||
option(TRACE_PARSER "Trace parser execution" OFF)
|
||||
option(TRACE_LEXER "Trace lexer execution" OFF)
|
||||
|
||||
if(MSVC)
|
||||
add_compile_options(/W1 /MP)
|
||||
# MSVC's standard library triggers warning C5105,
|
||||
# "macro expansion producing 'defined' has undefined behavior"
|
||||
add_compile_options(/std:c11 /W1 /MP /wd5105)
|
||||
add_definitions(/D_CRT_SECURE_NO_WARNINGS)
|
||||
else()
|
||||
add_compile_options(-Wall -pedantic)
|
||||
@@ -63,9 +65,33 @@ execute_process(COMMAND git describe --tags --dirty --always
|
||||
ERROR_QUIET)
|
||||
string(STRIP "${GIT_REV}" GIT_REV)
|
||||
|
||||
include_directories("${PROJECT_SOURCE_DIR}/include")
|
||||
|
||||
add_definitions(-DBUILD_VERSION_STRING="${GIT_REV}")
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED True)
|
||||
|
||||
add_subdirectory(src)
|
||||
|
||||
# By default, build in Release mode; Debug mode must be explicitly requested
|
||||
# (You may want to augment it with the options above)
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
message(CHECK_START "Checking if LTO is supported")
|
||||
include(CheckIPOSupported)
|
||||
check_ipo_supported(RESULT enable_lto)
|
||||
if(enable_lto)
|
||||
message(CHECK_PASS "yes")
|
||||
set_property(TARGET rgbasm rgblink rgbfix rgbgfx PROPERTY INTERPROCEDURAL_OPTIMIZATION ON)
|
||||
else()
|
||||
message(CHECK_FAIL "no")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(TRACE_PARSER)
|
||||
target_compile_definitions(rgbasm PRIVATE -DYYDEBUG)
|
||||
endif()
|
||||
|
||||
if(TRACE_LEXER)
|
||||
target_compile_definitions(rgbasm PRIVATE -DLEXER_DEBUG)
|
||||
endif()
|
||||
|
||||
@@ -40,6 +40,8 @@ Other contributors
|
||||
|
||||
- Quint Guvernator <quint@guvernator.net>
|
||||
|
||||
- Rangi <http://github.com/Rangi42>
|
||||
|
||||
- Sanqui <gsanky@gmail.com>
|
||||
|
||||
- YamaArashi <shadow962@live.com>
|
||||
|
||||
@@ -7,8 +7,7 @@
|
||||
FROM alpine:latest
|
||||
RUN apk add --update \
|
||||
build-base \
|
||||
byacc \
|
||||
flex \
|
||||
bison \
|
||||
libpng-dev
|
||||
COPY . /rgbds
|
||||
WORKDIR /rgbds
|
||||
|
||||
51
Makefile
51
Makefile
@@ -33,7 +33,7 @@ VERSION_STRING := `git describe --tags --dirty --always 2>/dev/null`
|
||||
WARNFLAGS := -Wall
|
||||
|
||||
# Overridable CFLAGS
|
||||
CFLAGS ?= -O3 -DNDEBUG
|
||||
CFLAGS ?= -O3 -flto -DNDEBUG
|
||||
# Non-overridable CFLAGS
|
||||
REALCFLAGS := ${CFLAGS} ${WARNFLAGS} -std=gnu11 -D_POSIX_C_SOURCE=200809L \
|
||||
-Iinclude
|
||||
@@ -43,9 +43,9 @@ LDFLAGS ?=
|
||||
REALLDFLAGS := ${LDFLAGS} ${WARNFLAGS} \
|
||||
-DBUILD_VERSION_STRING=\"${VERSION_STRING}\"
|
||||
|
||||
YFLAGS ?=
|
||||
YFLAGS ?= -Wall
|
||||
|
||||
YACC := yacc
|
||||
BISON := bison
|
||||
RM := rm -rf
|
||||
|
||||
# Rules to build the RGBDS binaries
|
||||
@@ -53,13 +53,15 @@ RM := rm -rf
|
||||
all: rgbasm rgblink rgbfix rgbgfx
|
||||
|
||||
rgbasm_obj := \
|
||||
src/asm/asmy.o \
|
||||
src/asm/charmap.o \
|
||||
src/asm/fixpoint.o \
|
||||
src/asm/format.o \
|
||||
src/asm/fstack.o \
|
||||
src/asm/lexer.o \
|
||||
src/asm/macro.o \
|
||||
src/asm/main.o \
|
||||
src/asm/math.o \
|
||||
src/asm/parser.o \
|
||||
src/asm/opt.o \
|
||||
src/asm/output.o \
|
||||
src/asm/rpn.o \
|
||||
src/asm/section.o \
|
||||
@@ -70,9 +72,10 @@ rgbasm_obj := \
|
||||
src/extern/getopt.o \
|
||||
src/extern/utf8decoder.o \
|
||||
src/hashmap.o \
|
||||
src/linkdefs.o
|
||||
src/linkdefs.o \
|
||||
src/opmath.o
|
||||
|
||||
src/asm/lexer.o src/asm/main.o: src/asm/asmy.h
|
||||
src/asm/lexer.o src/asm/main.o: src/asm/parser.h
|
||||
|
||||
rgblink_obj := \
|
||||
src/link/assign.o \
|
||||
@@ -86,7 +89,8 @@ rgblink_obj := \
|
||||
src/extern/err.o \
|
||||
src/extern/getopt.o \
|
||||
src/hashmap.o \
|
||||
src/linkdefs.o
|
||||
src/linkdefs.o \
|
||||
src/opmath.o
|
||||
|
||||
rgbfix_obj := \
|
||||
src/fix/main.o \
|
||||
@@ -114,15 +118,27 @@ rgbgfx: ${rgbgfx_obj}
|
||||
|
||||
# Rules to process files
|
||||
|
||||
# We want the yacc invocations to pass through our rules, not default ones
|
||||
# We want the Bison invocation to pass through our rules, not default ones
|
||||
.y.o:
|
||||
|
||||
# yacc-generated C files have an accompanying header
|
||||
.c.h:
|
||||
# Bison-generated C files have an accompanying header
|
||||
src/asm/parser.h: src/asm/parser.c
|
||||
$Qtouch $@
|
||||
|
||||
.y.c:
|
||||
$Q${YACC} -d ${YFLAGS} -o $@ $<
|
||||
src/asm/parser.c: src/asm/parser.y
|
||||
$QDEFS=; \
|
||||
add_flag(){ \
|
||||
if src/check_bison_ver.sh $$1 $$2; then \
|
||||
DEFS="-D$$3 $$DEFS"; \
|
||||
fi \
|
||||
}; \
|
||||
add_flag 3 5 api.token.raw=true; \
|
||||
add_flag 3 6 parse.error=detailed; \
|
||||
add_flag 3 0 parse.error=verbose; \
|
||||
add_flag 3 0 parse.lac=full; \
|
||||
add_flag 3 0 lr.type=ielr; \
|
||||
echo "DEFS=$$DEFS"; \
|
||||
${BISON} $$DEFS -d ${YFLAGS} -o $@ $<
|
||||
|
||||
.c.o:
|
||||
$Q${CC} ${REALCFLAGS} ${PNGCFLAGS} -c -o $@ $<
|
||||
@@ -136,7 +152,7 @@ clean:
|
||||
$Q${RM} rgbgfx rgbgfx.exe
|
||||
$Qfind src/ -name "*.o" -exec rm {} \;
|
||||
$Q${RM} rgbshim.sh
|
||||
$Q${RM} src/asm/asmy.c src/asm/asmy.h
|
||||
$Q${RM} src/asm/parser.c src/asm/parser.h
|
||||
|
||||
# Target used to install the binaries and man pages.
|
||||
|
||||
@@ -178,6 +194,7 @@ checkpatch:
|
||||
for commit in `git rev-list $$COMMON_COMMIT..HEAD`; do \
|
||||
echo "[*] Analyzing commit '$$commit'"; \
|
||||
git format-patch --stdout "$$commit~..$$commit" \
|
||||
-- src include '!src/extern' '!include/extern' \
|
||||
| ${CHECKPATCH} - || true; \
|
||||
done
|
||||
|
||||
@@ -189,7 +206,7 @@ develop:
|
||||
$Qenv $(MAKE) -j WARNFLAGS="-Werror -Wall -Wextra -Wpedantic -Wno-type-limits \
|
||||
-Wno-sign-compare -Wvla -Wformat -Wformat-security -Wformat-overflow=2 \
|
||||
-Wformat-truncation=1 -Wformat-y2k -Wswitch-enum -Wunused \
|
||||
-Wuninitialized -Wunknown-pragmas -Wstrict-overflow=5 \
|
||||
-Wuninitialized -Wunknown-pragmas -Wstrict-overflow=4 \
|
||||
-Wstringop-overflow=4 -Walloc-zero -Wduplicated-cond \
|
||||
-Wfloat-equal -Wshadow -Wcast-qual -Wcast-align -Wlogical-op \
|
||||
-Wnested-externs -Wno-aggressive-loop-optimizations -Winline \
|
||||
@@ -206,11 +223,11 @@ develop:
|
||||
# install instructions instead.
|
||||
|
||||
mingw32:
|
||||
$Qmake CC=i686-w64-mingw32-gcc YACC=bison \
|
||||
$Qmake CC=i686-w64-mingw32-gcc BISON=bison \
|
||||
PKG_CONFIG=i686-w64-mingw32-pkg-config -j
|
||||
|
||||
mingw64:
|
||||
$Qmake CC=x86_64-w64-mingw32-gcc YACC=bison \
|
||||
$Qmake CC=x86_64-w64-mingw32-gcc BISON=bison \
|
||||
PKG_CONFIG=x86_64-w64-mingw32-pkg-config -j
|
||||
|
||||
wine-shim:
|
||||
|
||||
@@ -32,7 +32,7 @@ is possible using ``make`` or ``cmake``; follow the link for more detailed instr
|
||||
|
||||
.. code:: sh
|
||||
|
||||
cmake -S . -B build
|
||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build
|
||||
cmake --install build
|
||||
|
||||
@@ -50,6 +50,8 @@ The RGBDS source code file structure somewhat resembles the following:
|
||||
│ └── workflows/
|
||||
│ └── ...
|
||||
├── contrib/
|
||||
│ ├── zsh_compl/
|
||||
│ │ └── ...
|
||||
│ └── ...
|
||||
├── include/
|
||||
│ └── ...
|
||||
@@ -79,6 +81,8 @@ The RGBDS source code file structure somewhat resembles the following:
|
||||
- ``contrib/`` - scripts and other resources which may be useful to users and developers of
|
||||
RGBDS.
|
||||
|
||||
* ``zsh_compl`` contains tab completion scripts for use with zsh. Put them somewhere in your ``fpath``, and they should auto-load.
|
||||
|
||||
- ``include/`` - header files for each respective C files in `src`.
|
||||
|
||||
- ``src/`` - source code and manual pages for RGBDS.
|
||||
|
||||
@@ -31,31 +31,33 @@ diff <(xxd $1) <(xxd $2) | while read -r LINE; do
|
||||
# Separator between files switches states
|
||||
echo $LINE
|
||||
STATE=3
|
||||
elif echo $LINE | grep -Eq '^[0-9]+(,[0-9]+)?c[0-9]+(,[0-9]+)?'; then
|
||||
elif grep -Eq '^[0-9]+(,[0-9]+)?[cd][0-9]+(,[0-9]+)?' <<< "$LINE"; then
|
||||
# Line info resets the whole thing
|
||||
STATE=1
|
||||
elif [ $STATE -eq 1 -o $STATE -eq 3 ]; then
|
||||
# Compute the GB address from the ROM offset
|
||||
OFS=$(echo $LINE | cut -d ' ' -f 2 | tr -d ':')
|
||||
OFS=$(cut -d ' ' -f 2 <<< "$LINE" | tr -d ':')
|
||||
BANK=$((0x$OFS / 0x4000))
|
||||
ADDR=$((0x$OFS % 0x4000 + ($BANK != 0) * 0x4000))
|
||||
# Try finding the preceding symbol closest to the diff
|
||||
if [ $STATE -eq 1 ]; then
|
||||
STATE=2
|
||||
SYMFILE=$(echo $1 | cut -d '.' -f 1).sym
|
||||
SYMFILE=${1%.*}.sym
|
||||
else
|
||||
STATE=4
|
||||
SYMFILE=$(echo $2 | cut -d '.' -f 1).sym
|
||||
SYMFILE=${2%.*}.sym
|
||||
fi
|
||||
EXTRA=$(if [ -f "$SYMFILE" ]; then
|
||||
# Read the sym file for such a symbol
|
||||
# Ignore comment lines, only pick matching bank
|
||||
grep -Fv ';' "$SYMFILE" |
|
||||
grep -Ei $(printf "^%02x:" $BANK) |
|
||||
# (The bank regex ignores comments already, make `cut` and `tr` process less lines)
|
||||
grep -Ei $(printf "^%02x:" $BANK) "$SYMFILE" |
|
||||
cut -d ';' -f 1 |
|
||||
tr -d "\r" |
|
||||
while read -r SYMADDR SYM; do
|
||||
SYMADDR=$((0x$(echo $SYMADDR | cut -d ':' -f 2)))
|
||||
SYMADDR=$((0x${SYMADDR#*:}))
|
||||
if [ $SYMADDR -le $ADDR ]; then
|
||||
printf " (%s+%#x)\n" $SYM $(($ADDR - $SYMADDR))
|
||||
printf " (%s+%#x)\n" "$SYM" $(($ADDR - $SYMADDR))
|
||||
fi
|
||||
# TODO: assumes sorted sym files
|
||||
done | tail -n 1
|
||||
@@ -63,9 +65,9 @@ diff <(xxd $1) <(xxd $2) | while read -r LINE; do
|
||||
printf "%02x:%04x %s\n" $BANK $ADDR $EXTRA
|
||||
fi
|
||||
if [ $STATE -eq 2 -o $STATE -eq 4 ]; then
|
||||
OFS=$(echo $LINE | cut -d ' ' -f 2 | tr -d ':')
|
||||
OFS=$(cut -d ' ' -f 2 <<< "$LINE" | tr -d ':')
|
||||
BANK=$((0x$OFS / 0x4000))
|
||||
ADDR=$((0x$OFS % 0x4000 + ($BANK != 0) * 0x4000))
|
||||
printf "%s %02x:%04x: %s\n" "$(echo $LINE | cut -d ' ' -f 1)" $BANK $ADDR "$(echo $LINE | cut -d ' ' -f 3-)"
|
||||
printf "%s %02x:%04x: %s\n" "${LINE:0:1}" $BANK $ADDR "${LINE#*: }"
|
||||
fi
|
||||
done
|
||||
|
||||
49
contrib/zsh_compl/_rgbasm
Normal file
49
contrib/zsh_compl/_rgbasm
Normal file
@@ -0,0 +1,49 @@
|
||||
#compdef rgbasm
|
||||
|
||||
_rgbasm_warnings() {
|
||||
local warnings=(
|
||||
'error:Turn all warnings into errors'
|
||||
|
||||
'all:Enable most warning messages'
|
||||
'extra:Enable extra, possibly unwanted, messages'
|
||||
'everything:Enable literally everything'
|
||||
|
||||
'assert:Warn when WARN-type asserts fail'
|
||||
'builtin-args:Report incorrect args to built-in funcs'
|
||||
'div:Warn when dividing the smallest int by -1'
|
||||
'empty-entry:Warn on empty entries in db, dw, dl args'
|
||||
'large-constant:Warn on constants too large for a signed 32-bit int'
|
||||
'long-string:Warn on strings too long'
|
||||
'obsolete:Warn when using deprecated features'
|
||||
'shift:Warn when shifting negative values'
|
||||
'shift-amount:Warn when a shift'\''s operand it negative or \> 32'
|
||||
'truncation:Warn when implicit truncations lose bits'
|
||||
'user:Warn when executing the WARN built-in'
|
||||
)
|
||||
# TODO: handle `no-` and `error=` somehow?
|
||||
_describe warning warnings
|
||||
}
|
||||
|
||||
local args=(
|
||||
# Arguments are listed here in the same order as in the manual, except for the version
|
||||
'(- : * options)'{-V,--version}'[Print version number]'
|
||||
|
||||
'(-E --export-all)'{-E,--export-all}'[Export all symbols]'
|
||||
'(-h --halt-without-nop)'{-h,--halt-without-nop}'[Avoid outputting a `nop` after `halt`]'
|
||||
'(-L ---preserve-ld)'{-L,--preserve-ld}'[Prevent auto-optimizing `ld` into `ldh`]'
|
||||
'(-v --verbose)'{-v,--verbose}'[Print additional messages regarding progression]'
|
||||
-w'[Disable all warnings]'
|
||||
|
||||
'(-b --binary-digits)'{-b,--binary-digits}'+[Change chars for binary constants]:digit spec:'
|
||||
'(-D --define)'{-D,--define}'+[Define a string symbol]:name + value (default 1):'
|
||||
'(-g --gfx-chars)'{-g,--gfx-chars}'+[Change chars for gfx constants]:chars spec:'
|
||||
'(-i --include)'{-i,--include}'+[Add an include directory]:include path:_files -/'
|
||||
'(-M --dependfile)'{-M,--dependfile}"+[List deps in make format]:output file:_files -g '*.{d,mk}"
|
||||
'(-o --output)'{-o,--output}'+[Output file]:output file:_files'
|
||||
'(-p --pad-value)'{-p,--pad-value}'+[Set padding byte]:padding byte:'
|
||||
'(-r --recursion-depth)'{-r,--recursion-depth}'+[Set maximum recursion depth]:depth:'
|
||||
'(-W --warning)'{-W,--warning}'+[Toggle warning flags]:warning flag:_rgbasm_warnings'
|
||||
|
||||
'*'":assembly sources:_files -g '*.asm'"
|
||||
)
|
||||
_arguments -s -S : $args
|
||||
25
contrib/zsh_compl/_rgbfix
Normal file
25
contrib/zsh_compl/_rgbfix
Normal file
@@ -0,0 +1,25 @@
|
||||
#compdef rgbfix
|
||||
|
||||
local args=(
|
||||
# Arguments are listed here in the same order as in the manual, except for the version
|
||||
'(- : * options)'{-V,--version}'[Print version number]'
|
||||
|
||||
'(-C --color-only)'{-C,--color-only}'[Mark ROM as GBC-only]'
|
||||
'(-c --color-compatible)'{-c,--color-compatible}'[Mark ROM as GBC-compatible]'
|
||||
'(-j --non-japanese)'{-j,--non-japanese}'[Set the non-Japanese region flag]'
|
||||
'(-s --sgb-compatible)'{-s,--sgb-compatible}'[Set the SGB flag]'
|
||||
'(-f --fix-spec -v --validate)'{-v,--validate}'[Shorthand for -f lhg]'
|
||||
|
||||
'(-f --fix-spec -v --validate)'{-f,--fix-spec}'+[Fix or trash some header values]:fix spec:'
|
||||
'(-i --game-id)'{-i,--game-id}'+[Set game ID string]:4-char game ID:'
|
||||
'(-k --new-licensee)'{-k,--new-licensee}'+[Set new licensee string]:2-char licensee ID:'
|
||||
'(-l --old-licensee)'{-l,--old-licensee}'+[Set old licensee ID]:licensee number:'
|
||||
'(-m --mbc-type)'{-m,--mbc-type}'+[Set MBC flags]:mbc flags byte:'
|
||||
'(-n --rom-version)'{-n,--rom-version}'+[Set ROM version]:rom version byte:'
|
||||
'(-p --pad-value)'{-p,--pad-value}'+[Pad to next valid size using this byte as padding]:padding byte:'
|
||||
'(-r --ram-size)'{-r,--ram-size}'+[Set RAM size]:ram size byte:'
|
||||
'(-t --title)'{-t,--title}'+[Set title string]:11-char title string:'
|
||||
|
||||
'*'":ROM files:_files -g '*.{gb,sgb,gbc}'"
|
||||
)
|
||||
_arguments -s -S : $args
|
||||
37
contrib/zsh_compl/_rgbgfx
Normal file
37
contrib/zsh_compl/_rgbgfx
Normal file
@@ -0,0 +1,37 @@
|
||||
#compdef rgbgfx
|
||||
|
||||
_depths() {
|
||||
local depths=(
|
||||
'1:1bpp'
|
||||
'2:2bpp (native)'
|
||||
)
|
||||
|
||||
_describe 'bit depth' depths
|
||||
}
|
||||
|
||||
local args=(
|
||||
# Arguments are listed here in the same order as in the manual, except for the version
|
||||
'(- : * options)'{-V,--version}'[Print version number]'
|
||||
|
||||
'(-a --attr-map -A --output-attr-map)'{-A,--output-attr-map}'[Shortcut for -a <file>.attrmap]'
|
||||
'(-C --color-curve)'{-C,--color-curve}'[Generate palettes using GBC color curve]'
|
||||
'(-D --debug)'{-D,--debug}'[Enable debug features]'
|
||||
'(-f --fix -F --fix-and-save)'{-f,--fix}'[Fix input PNG into an indexed image]'
|
||||
'(-f --fix -F --fix-and-save)'{-F,--fix-and-save}'[Like -f but also save CLI params within the PNG]'
|
||||
'(-h --horizontal)'{-h,--horizontal}'[Lay out tiles horizontally instead of vertically]'
|
||||
'(-m --mirror-tiles)'{-m,--mirror-tiles}'[Eliminate mirrored tiles from output]'
|
||||
'(-p --palette -P --output-palette)'{-P,--output-palette}'[Shortcut for -p <file>.pal]'
|
||||
'(-t --tilemap -T --output-tilemap)'{-T,--output-tilemap}'[Shortcut for -t <file>.tilemap]'
|
||||
'(-u --unique-tiles)'{-u,--unique-tiles}'[Eliminate redundant tiles]'
|
||||
'(-v --verbose)'{-v,--verbose}'[Enable verbose output]'
|
||||
|
||||
'(-a --attr-map -A --output-attr-map)'{-a,--attr-map}'+[Generate a map of tile attributes (mirroring)]:attrmap file:_files'
|
||||
'(-d --depth)'{-d,--depth}'+[Set bit depth]:bit depth:_depths'
|
||||
'(-o --output)'{-o,--output}'+[Set output file]:output file:_files'
|
||||
'(-p --palette -P --output-palette)'{-p,--palette}"+[Output the image's palette in little-endian native RGB555 format]:palette file:_files"
|
||||
'(-t --tilemap -T --output-tilemap)'{-t,--tilemap}'+[Generate a map of tile indices]:tilemap file:_files'
|
||||
'(-x --trim-end)'{-x,--trim-end}'+[Trim end of output by this many tiles]:tile count:'
|
||||
|
||||
'*'":input png files:_files -g '*.png'"
|
||||
)
|
||||
_arguments -s -S : $args
|
||||
22
contrib/zsh_compl/_rgblink
Normal file
22
contrib/zsh_compl/_rgblink
Normal file
@@ -0,0 +1,22 @@
|
||||
#compdef rgblink
|
||||
|
||||
local args=(
|
||||
# Arguments are listed here in the same order as in the manual, except for the version
|
||||
'(- : * options)'{-V,--version}'[Print version number]'
|
||||
|
||||
'(-d --dmg)'{-d,--dmg}'[Enable DMG mode (-w + no VRAM banking)]'
|
||||
'(-t --tiny)'{-t,--tiny}'[Enable tiny mode, disabling ROM banking]'
|
||||
'(-v --verbose)'{-v,--verbose}'[Enable verbose output]'
|
||||
'(-w --wramx)'{-w,--wramx}'[Disable WRAM banking]'
|
||||
|
||||
'(-l --linkerscript)'{-l,--linkerscript}"+[Use a linker script]:linker script:_files -g '*.link'"
|
||||
'(-m --map)'{-m,--map}"+[Produce a map file]:map file:_files -g '*.map'"
|
||||
'(-n --sym)'(-n,--sym)"+[Produce a symbol file]:sym file:_files -g '*.sym'"
|
||||
'(-O --overlay)'{-O,--overlay}'+[Overlay sections over on top of bin file]:base overlay:_files'
|
||||
'(-o --output)'{-o,--output}"+[Write ROM image to this file]:rom file:_files -g '*.{gb,sgb,gbc}'"
|
||||
'(-p --pad-value)'{-p,--pad-value}'+[Set padding byte]:padding byte:'
|
||||
'(-s --smart)'{-s,--smart}'+[!BROKEN! Perform smart linking from this symbol]:symbol name:'
|
||||
|
||||
'*'":object files:_files -g '*.o'"
|
||||
)
|
||||
_arguments -s -S : $args
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* This file is part of RGBDS.
|
||||
*
|
||||
* Copyright (c) 1997-2018, Carsten Sorensen and RGBDS contributors.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
/*
|
||||
* Contains some assembler-wide defines and externs
|
||||
*/
|
||||
|
||||
#ifndef RGBDS_ASM_ASM_H
|
||||
#define RGBDS_ASM_ASM_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "asm/localasm.h"
|
||||
#include "asm/symbol.h"
|
||||
|
||||
#define MAXMACROARGS 99999
|
||||
#define MAXINCPATHS 128
|
||||
|
||||
extern uint32_t nTotalLines;
|
||||
extern uint32_t nIFDepth;
|
||||
extern struct Section *pCurrentSection;
|
||||
|
||||
#endif /* RGBDS_ASM_ASM_H */
|
||||
31
include/asm/fixpoint.h
Normal file
31
include/asm/fixpoint.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* This file is part of RGBDS.
|
||||
*
|
||||
* Copyright (c) 1997-2021, Carsten Sorensen and RGBDS contributors.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#ifndef RGBDS_ASM_FIXPOINT_H
|
||||
#define RGBDS_ASM_FIXPOINT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
int32_t fix_Callback_PI(void);
|
||||
void fix_Print(int32_t i);
|
||||
int32_t fix_Sin(int32_t i);
|
||||
int32_t fix_Cos(int32_t i);
|
||||
int32_t fix_Tan(int32_t i);
|
||||
int32_t fix_ASin(int32_t i);
|
||||
int32_t fix_ACos(int32_t i);
|
||||
int32_t fix_ATan(int32_t i);
|
||||
int32_t fix_ATan2(int32_t i, int32_t j);
|
||||
int32_t fix_Mul(int32_t i, int32_t j);
|
||||
int32_t fix_Div(int32_t i, int32_t j);
|
||||
int32_t fix_Pow(int32_t i, int32_t j);
|
||||
int32_t fix_Log(int32_t i, int32_t j);
|
||||
int32_t fix_Round(int32_t i);
|
||||
int32_t fix_Ceil(int32_t i);
|
||||
int32_t fix_Floor(int32_t i);
|
||||
|
||||
#endif /* RGBDS_ASM_FIXPOINT_H */
|
||||
63
include/asm/format.h
Normal file
63
include/asm/format.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* This file is part of RGBDS.
|
||||
*
|
||||
* Copyright (c) 2020, RGBDS contributors.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#ifndef RGBDS_FORMAT_SPEC_H
|
||||
#define RGBDS_FORMAT_SPEC_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
enum FormatState {
|
||||
FORMAT_SIGN, // expects '+' or ' ' (optional)
|
||||
FORMAT_PREFIX, // expects '#' (optional)
|
||||
FORMAT_ALIGN, // expects '-' (optional)
|
||||
FORMAT_WIDTH, // expects '0'-'9', max 255 (optional) (leading '0' indicates pad)
|
||||
FORMAT_FRAC, // got '.', expects '0'-'9', max 255 (optional)
|
||||
FORMAT_DONE, // got [duXxbofs] (required)
|
||||
FORMAT_INVALID, // got unexpected character
|
||||
};
|
||||
|
||||
struct FormatSpec {
|
||||
enum FormatState state;
|
||||
int sign;
|
||||
bool prefix;
|
||||
bool alignLeft;
|
||||
bool padZero;
|
||||
uint8_t width;
|
||||
bool hasFrac;
|
||||
uint8_t fracWidth;
|
||||
int type;
|
||||
bool valid;
|
||||
};
|
||||
|
||||
struct StrFmtArg {
|
||||
union {
|
||||
uint32_t number;
|
||||
char *string;
|
||||
};
|
||||
bool isNumeric;
|
||||
};
|
||||
|
||||
#define INITIAL_STRFMT_ARG_SIZE 4
|
||||
struct StrFmtArgList {
|
||||
char *format;
|
||||
size_t nbArgs;
|
||||
size_t capacity;
|
||||
struct StrFmtArg *args;
|
||||
};
|
||||
|
||||
struct FormatSpec fmt_NewSpec(void);
|
||||
bool fmt_IsEmpty(struct FormatSpec const *fmt);
|
||||
bool fmt_IsValid(struct FormatSpec const *fmt);
|
||||
bool fmt_IsFinished(struct FormatSpec const *fmt);
|
||||
void fmt_UseCharacter(struct FormatSpec *fmt, int c);
|
||||
void fmt_FinishCharacters(struct FormatSpec *fmt);
|
||||
void fmt_PrintString(char *buf, size_t bufLen, struct FormatSpec const *fmt, char const *value);
|
||||
void fmt_PrintNumber(char *buf, size_t bufLen, struct FormatSpec const *fmt, uint32_t value);
|
||||
|
||||
#endif /* RGBDS_FORMAT_SPEC_H */
|
||||
@@ -13,10 +13,10 @@
|
||||
#ifndef RGBDS_ASM_FSTACK_H
|
||||
#define RGBDS_ASM_FSTACK_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "asm/asm.h"
|
||||
#include "asm/lexer.h"
|
||||
|
||||
#include "types.h"
|
||||
@@ -73,6 +73,10 @@ bool yywrap(void);
|
||||
void fstk_RunInclude(char const *path);
|
||||
void fstk_RunMacro(char const *macroName, struct MacroArgs *args);
|
||||
void fstk_RunRept(uint32_t count, int32_t nReptLineNo, char *body, size_t size);
|
||||
void fstk_RunFor(char const *symName, int32_t start, int32_t stop, int32_t step,
|
||||
int32_t reptLineNo, char *body, size_t size);
|
||||
void fstk_StopRept(void);
|
||||
bool fstk_Break(void);
|
||||
|
||||
void fstk_Init(char const *mainPath, size_t maxRecursionDepth);
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#ifndef RGBDS_ASM_LEXER_H
|
||||
#define RGBDS_ASM_LEXER_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#define MAXSTRLEN 255
|
||||
|
||||
struct LexerState;
|
||||
@@ -30,17 +32,21 @@ static inline void lexer_SetStateAtEOL(struct LexerState *state)
|
||||
lexerStateEOL = state;
|
||||
}
|
||||
|
||||
extern char const *binDigits;
|
||||
extern char const *gfxDigits;
|
||||
extern char binDigits[2];
|
||||
extern char gfxDigits[4];
|
||||
|
||||
static inline void lexer_SetBinDigits(char const *digits)
|
||||
static inline void lexer_SetBinDigits(char const digits[2])
|
||||
{
|
||||
binDigits = digits;
|
||||
binDigits[0] = digits[0];
|
||||
binDigits[1] = digits[1];
|
||||
}
|
||||
|
||||
static inline void lexer_SetGfxDigits(char const *digits)
|
||||
static inline void lexer_SetGfxDigits(char const digits[4])
|
||||
{
|
||||
gfxDigits = digits;
|
||||
gfxDigits[0] = digits[0];
|
||||
gfxDigits[1] = digits[1];
|
||||
gfxDigits[2] = digits[2];
|
||||
gfxDigits[3] = digits[3];
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -56,18 +62,40 @@ enum LexerMode {
|
||||
LEXER_NORMAL,
|
||||
LEXER_RAW,
|
||||
LEXER_SKIP_TO_ELIF,
|
||||
LEXER_SKIP_TO_ENDC
|
||||
LEXER_SKIP_TO_ENDC,
|
||||
LEXER_SKIP_TO_ENDR
|
||||
};
|
||||
|
||||
void lexer_SetMode(enum LexerMode mode);
|
||||
void lexer_ToggleStringExpansion(bool enable);
|
||||
|
||||
uint32_t lexer_GetIFDepth(void);
|
||||
void lexer_IncIFDepth(void);
|
||||
void lexer_DecIFDepth(void);
|
||||
bool lexer_RanIFBlock(void);
|
||||
bool lexer_ReachedELSEBlock(void);
|
||||
void lexer_RunIFBlock(void);
|
||||
void lexer_ReachELSEBlock(void);
|
||||
|
||||
struct CaptureBody {
|
||||
uint32_t lineNo;
|
||||
char *body;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
char const *lexer_GetFileName(void);
|
||||
uint32_t lexer_GetLineNo(void);
|
||||
uint32_t lexer_GetColNo(void);
|
||||
void lexer_DumpStringExpansions(void);
|
||||
int yylex(void);
|
||||
void lexer_CaptureRept(char **capture, size_t *size);
|
||||
void lexer_CaptureMacroBody(char **capture, size_t *size);
|
||||
void lexer_CaptureRept(struct CaptureBody *capture);
|
||||
void lexer_CaptureMacroBody(struct CaptureBody *capture);
|
||||
|
||||
#define INITIAL_DS_ARG_SIZE 2
|
||||
struct DsArgList {
|
||||
size_t nbArgs;
|
||||
size_t capacity;
|
||||
struct Expression *args;
|
||||
};
|
||||
|
||||
#endif /* RGBDS_ASM_LEXER_H */
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
/*
|
||||
* This file is part of RGBDS.
|
||||
*
|
||||
* Copyright (c) 1997-2018, Carsten Sorensen and RGBDS contributors.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#ifndef RGBDS_ASM_LOCALASM_H
|
||||
#define RGBDS_ASM_LOCALASM_H
|
||||
|
||||
/*
|
||||
* GB Z80 instruction groups
|
||||
*
|
||||
* n3 = 3-bit
|
||||
* n = 8-bit
|
||||
* nn = 16-bit
|
||||
*
|
||||
* ADC A,n : 0xCE
|
||||
* ADC A,r : 0x88|r
|
||||
* ADD A,n : 0xC6
|
||||
* ADD A,r : 0x80|r
|
||||
* ADD HL,ss : 0x09|(ss<<4)
|
||||
* ADD SP,n : 0xE8
|
||||
* AND A,n : 0xE6
|
||||
* AND A,r : 0xA0|r
|
||||
* BIT n3,r : 0xCB 0x40|(n3<<3)|r
|
||||
* CALL cc,nn : 0xC4|(cc<<3)
|
||||
* CALL nn : 0xCD
|
||||
* CCF : 0x3F
|
||||
* CP A,n : 0xFE
|
||||
* CP A,r : 0xB8|r
|
||||
* CPL : 0x2F
|
||||
* DAA : 0x27
|
||||
* DEC r : 0x05|(r<<3)
|
||||
* DEC ss : 0x0B|(ss<<4)
|
||||
* DI : 0xF3
|
||||
* EI : 0xFB
|
||||
* HALT : 0x76
|
||||
* INC r : 0x04|(r<<3)
|
||||
* INC ss : 0x03|(ss<<4)
|
||||
* JP HL : 0xE9
|
||||
* JP cc,nn : 0xC2|(cc<<3)
|
||||
* JP nn : 0xC3|(cc<<3)
|
||||
* JR n : 0x18
|
||||
* JR cc,n : 0x20|(cc<<3)
|
||||
* LD (nn),SP : 0x08
|
||||
* LD ($FF00+C),A : 0xE2
|
||||
* LD ($FF00+n),A : 0xE0
|
||||
* LD (nn),A : 0xEA
|
||||
* LD (rr),A : 0x02|(rr<<4) // HL+ and HL- included
|
||||
* LD A,($FF00+C) : 0xF2
|
||||
* LD A,($FF00+n) : 0xF0
|
||||
* LD A,(nn) : 0xFA
|
||||
* LD A,(rr) : 0x0A|(rr<<4) // HL+ and HL- included
|
||||
* LD HL,SP+n : 0xF8
|
||||
* LD SP,HL : 0xF9
|
||||
* LD r,n : 0x06|(r<<3)
|
||||
* LD r,r' : 0x40|(r<<3)|r' // NOTE: LD (HL),(HL) not allowed
|
||||
* LD ss,nn : 0x01|(ss<<4)
|
||||
* NOP : 0x00
|
||||
* OR A,n : 0xF6
|
||||
* OR A,r : 0xB0|r
|
||||
* POP tt : 0xC1|(tt<<4)
|
||||
* PUSH tt : 0xC5|(tt<<4)
|
||||
* RES n3,r : 0xCB 0x80|(n3<<3)|r
|
||||
* RET : 0xC9
|
||||
* RET cc : 0xC0|(cc<<3)
|
||||
* RETI : 0xD9
|
||||
* RL r : 0xCB 0x10|r
|
||||
* RLA : 0x17
|
||||
* RLC r : 0xCB 0x00|r
|
||||
* RLCA : 0x07
|
||||
* RR r : 0xCB 0x18|r
|
||||
* RRA : 0x1F
|
||||
* RRC r : 0xCB 0x08|r
|
||||
* RRCA : 0x0F
|
||||
* RST n : 0xC7|n
|
||||
* SBC A,n : 0xDE
|
||||
* SBC A,r : 0x98|r
|
||||
* SCF : 0x37
|
||||
* SET n3,r : 0xCB 0xC0|(n8<<3)|r
|
||||
* SLA r : 0xCB 0x20|r
|
||||
* SRA r : 0xCB 0x28|r
|
||||
* SRL r : 0xCB 0x38|r
|
||||
* STOP : 0x10 0x00
|
||||
* SUB A,n : 0xD6
|
||||
* SUB A,r : 0x90|r
|
||||
* SWAP r : 0xCB 0x30|r
|
||||
* XOR A,n : 0xEE
|
||||
* XOR A,r : 0xA8|r
|
||||
*/
|
||||
|
||||
/* "r" defs */
|
||||
enum {
|
||||
REG_B = 0,
|
||||
REG_C,
|
||||
REG_D,
|
||||
REG_E,
|
||||
REG_H,
|
||||
REG_L,
|
||||
REG_HL_IND,
|
||||
REG_A
|
||||
};
|
||||
|
||||
/* "rr" defs */
|
||||
enum {
|
||||
REG_BC_IND = 0,
|
||||
REG_DE_IND,
|
||||
REG_HL_INDINC,
|
||||
REG_HL_INDDEC,
|
||||
};
|
||||
|
||||
/* "ss" defs (SP) and "tt" defs (AF) */
|
||||
enum {
|
||||
REG_BC = 0,
|
||||
REG_DE = 1,
|
||||
REG_HL = 2,
|
||||
REG_SP = 3,
|
||||
REG_AF = 3
|
||||
};
|
||||
|
||||
/* "cc" defs */
|
||||
enum {
|
||||
CC_NZ = 0,
|
||||
CC_Z,
|
||||
CC_NC,
|
||||
CC_C
|
||||
};
|
||||
|
||||
#endif /* RGBDS_ASM_LOCALASM_H */
|
||||
@@ -10,6 +10,7 @@
|
||||
#define RGBDS_MACRO_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "asm/warning.h"
|
||||
@@ -24,12 +25,13 @@ void macro_AppendArg(struct MacroArgs **args, char *s);
|
||||
void macro_UseNewArgs(struct MacroArgs *args);
|
||||
void macro_FreeArgs(struct MacroArgs *args);
|
||||
char const *macro_GetArg(uint32_t i);
|
||||
char *macro_GetAllArgs(void);
|
||||
|
||||
uint32_t macro_GetUniqueID(void);
|
||||
char const *macro_GetUniqueIDStr(void);
|
||||
void macro_SetUniqueID(uint32_t id);
|
||||
uint32_t macro_UseNewUniqueID(void);
|
||||
void macro_ShiftCurrentArgs(void);
|
||||
void macro_ShiftCurrentArgs(int32_t count);
|
||||
uint32_t macro_NbArgs(void);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* This file is part of RGBDS.
|
||||
*
|
||||
* Copyright (c) 1997-2018, Carsten Sorensen and RGBDS contributors.
|
||||
* Copyright (c) 1997-2021, Carsten Sorensen and RGBDS contributors.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
@@ -15,19 +15,6 @@
|
||||
|
||||
#include "helpers.h"
|
||||
|
||||
struct sOptions {
|
||||
char binary[2];
|
||||
char gbgfx[4];
|
||||
int32_t fillchar;
|
||||
};
|
||||
|
||||
extern char *tzNewMacro;
|
||||
extern uint32_t ulNewMacroSize;
|
||||
extern int32_t nGBGfxID;
|
||||
extern int32_t nBinaryID;
|
||||
|
||||
extern struct sOptions DefaultOptions;
|
||||
extern struct sOptions CurrentOptions;
|
||||
extern bool haltnop;
|
||||
extern bool optimizeloads;
|
||||
extern bool verbose;
|
||||
@@ -39,13 +26,6 @@ extern bool oGeneratedMissingIncludes;
|
||||
extern bool oFailedOnMissingInclude;
|
||||
extern bool oGeneratePhonyDeps;
|
||||
|
||||
void opt_Push(void);
|
||||
void opt_Pop(void);
|
||||
void opt_Parse(char *s);
|
||||
|
||||
void upperstring(char *s);
|
||||
void lowerstring(char *s);
|
||||
|
||||
/* TODO: are these really needed? */
|
||||
#define YY_FATAL_ERROR fatalerror
|
||||
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* This file is part of RGBDS.
|
||||
*
|
||||
* Copyright (c) 1997-2018, Carsten Sorensen and RGBDS contributors.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#ifndef RGBDS_ASM_MATH_H
|
||||
#define RGBDS_ASM_MATH_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void math_DefinePI(void);
|
||||
void math_Print(int32_t i);
|
||||
int32_t math_Sin(int32_t i);
|
||||
int32_t math_Cos(int32_t i);
|
||||
int32_t math_Tan(int32_t i);
|
||||
int32_t math_ASin(int32_t i);
|
||||
int32_t math_ACos(int32_t i);
|
||||
int32_t math_ATan(int32_t i);
|
||||
int32_t math_ATan2(int32_t i, int32_t j);
|
||||
int32_t math_Mul(int32_t i, int32_t j);
|
||||
int32_t math_Div(int32_t i, int32_t j);
|
||||
int32_t math_Round(int32_t i);
|
||||
int32_t math_Ceil(int32_t i);
|
||||
int32_t math_Floor(int32_t i);
|
||||
|
||||
#endif /* RGBDS_ASM_MATH_H */
|
||||
22
include/asm/opt.h
Normal file
22
include/asm/opt.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* This file is part of RGBDS.
|
||||
*
|
||||
* Copyright (c) 2021, Eldred Habert and RGBDS contributors.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#ifndef RGBDS_OPT_H
|
||||
#define RGBDS_OPT_H
|
||||
|
||||
|
||||
void opt_B(char chars[2]);
|
||||
void opt_G(char chars[4]);
|
||||
void opt_P(uint8_t fill);
|
||||
void opt_Parse(char const *option);
|
||||
|
||||
void opt_Push(void);
|
||||
void opt_Pop(void);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "linkdefs.h"
|
||||
|
||||
struct Expression;
|
||||
struct FileStackNode;
|
||||
|
||||
extern char *tzObjectname;
|
||||
extern struct Section *pSectionList, *pCurrentSection;
|
||||
@@ -21,8 +22,7 @@ extern struct Section *pSectionList, *pCurrentSection;
|
||||
void out_RegisterNode(struct FileStackNode *node);
|
||||
void out_ReplaceNode(struct FileStackNode *node);
|
||||
void out_SetFileName(char *s);
|
||||
void out_CreatePatch(uint32_t type, struct Expression const *expr,
|
||||
uint32_t ofs);
|
||||
void out_CreatePatch(uint32_t type, struct Expression const *expr, uint32_t ofs, uint32_t pcShift);
|
||||
bool out_CreateAssert(enum AssertionType type, struct Expression const *expr,
|
||||
char const *message, uint32_t ofs);
|
||||
void out_WriteObject(void);
|
||||
|
||||
@@ -27,9 +27,6 @@ struct Expression {
|
||||
uint32_t nRPNPatchSize; // Size the expression will take in the obj file
|
||||
};
|
||||
|
||||
/* FIXME: Should be defined in `asmy.h`, but impossible with POSIX Yacc */
|
||||
extern int32_t nPCOffset;
|
||||
|
||||
/*
|
||||
* Determines if an expression is known at assembly time
|
||||
*/
|
||||
@@ -46,7 +43,7 @@ static inline bool rpn_isSymbol(const struct Expression *expr)
|
||||
return expr->isSymbol;
|
||||
}
|
||||
|
||||
void rpn_Symbol(struct Expression *expr, char *tzSym);
|
||||
void rpn_Symbol(struct Expression *expr, char const *tzSym);
|
||||
void rpn_Number(struct Expression *expr, uint32_t i);
|
||||
void rpn_LOGNOT(struct Expression *expr, const struct Expression *src);
|
||||
struct Symbol const *rpn_SymbolOf(struct Expression const *expr);
|
||||
|
||||
@@ -14,12 +14,16 @@
|
||||
|
||||
#include "linkdefs.h"
|
||||
|
||||
extern uint8_t fillByte;
|
||||
|
||||
struct Expression;
|
||||
|
||||
struct Section {
|
||||
char *name;
|
||||
enum SectionType type;
|
||||
enum SectionModifier modifier;
|
||||
struct FileStackNode *src; /* Where the section was defined */
|
||||
uint32_t fileLine; /* Line where the section was defined */
|
||||
uint32_t size;
|
||||
uint32_t org;
|
||||
uint32_t bank;
|
||||
@@ -41,7 +45,8 @@ void out_NewSection(char const *name, uint32_t secttype, uint32_t org,
|
||||
struct SectionSpec const *attributes,
|
||||
enum SectionModifier mod);
|
||||
void out_SetLoadSection(char const *name, uint32_t secttype, uint32_t org,
|
||||
struct SectionSpec const *attributes);
|
||||
struct SectionSpec const *attributes,
|
||||
enum SectionModifier mod);
|
||||
void out_EndLoadSection(void);
|
||||
|
||||
struct Section *sect_GetSymbolSection(void);
|
||||
@@ -56,13 +61,15 @@ void sect_CheckUnionClosed(void);
|
||||
|
||||
void out_AbsByte(uint8_t b);
|
||||
void out_AbsByteGroup(uint8_t const *s, int32_t length);
|
||||
void out_AbsWordGroup(uint8_t const *s, int32_t length);
|
||||
void out_AbsLongGroup(uint8_t const *s, int32_t length);
|
||||
void out_Skip(int32_t skip, bool ds);
|
||||
void out_String(char const *s);
|
||||
void out_RelByte(struct Expression *expr);
|
||||
void out_RelBytes(struct Expression *expr, uint32_t n);
|
||||
void out_RelWord(struct Expression *expr);
|
||||
void out_RelLong(struct Expression *expr);
|
||||
void out_PCRelByte(struct Expression *expr);
|
||||
void out_RelByte(struct Expression *expr, uint32_t pcShift);
|
||||
void out_RelBytes(uint32_t n, struct Expression *exprs, size_t size);
|
||||
void out_RelWord(struct Expression *expr, uint32_t pcShift);
|
||||
void out_RelLong(struct Expression *expr, uint32_t pcShift);
|
||||
void out_PCRelByte(struct Expression *expr, uint32_t pcShift);
|
||||
void out_BinaryFile(char const *s, int32_t startPos);
|
||||
void out_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length);
|
||||
|
||||
|
||||
@@ -12,9 +12,11 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "asm/section.h"
|
||||
|
||||
#include "platform.h" // MIN_NB_ELMS
|
||||
#include "types.h"
|
||||
|
||||
#define HASHSIZE (1 << 16)
|
||||
@@ -115,19 +117,33 @@ int32_t sym_GetValue(struct Symbol const *sym);
|
||||
void sym_SetExportAll(bool set);
|
||||
struct Symbol *sym_AddLocalLabel(char const *symName);
|
||||
struct Symbol *sym_AddLabel(char const *symName);
|
||||
struct Symbol *sym_AddAnonLabel(void);
|
||||
void sym_WriteAnonLabelName(char buf[MIN_NB_ELMS(MAXSYMLEN + 1)], uint32_t ofs, bool neg);
|
||||
void sym_Export(char const *symName);
|
||||
struct Symbol *sym_AddEqu(char const *symName, int32_t value);
|
||||
struct Symbol *sym_AddSet(char const *symName, int32_t value);
|
||||
uint32_t sym_GetPCValue(void);
|
||||
uint32_t sym_GetConstantSymValue(struct Symbol const *sym);
|
||||
uint32_t sym_GetConstantValue(char const *s);
|
||||
struct Symbol *sym_FindSymbol(char const *symName);
|
||||
/*
|
||||
* Find a symbol by exact name, bypassing expansion checks
|
||||
*/
|
||||
struct Symbol *sym_FindExactSymbol(char const *name);
|
||||
/*
|
||||
* Find a symbol by exact name; may not be scoped, produces an error if it is
|
||||
*/
|
||||
struct Symbol *sym_FindUnscopedSymbol(char const *name);
|
||||
/*
|
||||
* Find a symbol, possibly scoped, by name
|
||||
*/
|
||||
struct Symbol *sym_FindScopedSymbol(char const *name);
|
||||
struct Symbol const *sym_GetPC(void);
|
||||
struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char *body, size_t size);
|
||||
struct Symbol *sym_Ref(char const *symName);
|
||||
struct Symbol *sym_AddString(char const *symName, char const *value);
|
||||
uint32_t sym_GetDefinedValue(char const *s);
|
||||
struct Symbol *sym_RedefString(char const *symName, char const *value);
|
||||
void sym_Purge(char const *symName);
|
||||
void sym_Init(void);
|
||||
void sym_Init(time_t now);
|
||||
|
||||
/* Functions to save and restore the current symbol scope. */
|
||||
char const *sym_GetCurrentSymbolScope(void);
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
|
||||
uint32_t calchash(const char *s);
|
||||
char const *print(int c);
|
||||
/*
|
||||
* @return The number of bytes read, or 0 if invalid data was found
|
||||
*/
|
||||
size_t readUTF8Char(uint8_t *dest, char const *src);
|
||||
|
||||
#endif /* RGBDS_UTIL_H */
|
||||
|
||||
@@ -18,10 +18,13 @@ enum WarningID {
|
||||
WARNING_BUILTIN_ARG, /* Invalid args to builtins */
|
||||
WARNING_CHARMAP_REDEF, /* Charmap entry re-definition */
|
||||
WARNING_DIV, /* Division undefined behavior */
|
||||
WARNING_EMPTY_DATA_DIRECTIVE, /* `db`, `dw` or `dl` with no directive in ROM */
|
||||
WARNING_EMPTY_ENTRY, /* Empty entry in `db`, `dw` or `dl` */
|
||||
WARNING_EMPTY_DATA_DIRECTIVE, /* `db`, `dw` or `dl` directive without data in ROM */
|
||||
WARNING_EMPTY_MACRO_ARG, /* Empty macro argument */
|
||||
WARNING_EMPTY_STRRPL, /* Empty second argument in `STRRPL` */
|
||||
WARNING_LARGE_CONSTANT, /* Constants too large */
|
||||
WARNING_LONG_STR, /* String too long for internal buffers */
|
||||
WARNING_MACRO_SHIFT, /* Shift past available arguments in macro */
|
||||
WARNING_NESTED_COMMENT, /* Comment-start delimiter in a block comment */
|
||||
WARNING_OBSOLETE, /* Obsolete things */
|
||||
WARNING_SHIFT, /* Shifting undefined behavior */
|
||||
WARNING_SHIFT_AMOUNT, /* Strange shift amount */
|
||||
@@ -45,7 +48,7 @@ void processWarningFlag(char const *flag);
|
||||
* Used to warn the user about problems that don't prevent the generation of
|
||||
* valid code.
|
||||
*/
|
||||
void warning(enum WarningID id, const char *fmt, ...);
|
||||
void warning(enum WarningID id, const char *fmt, ...) format_(printf, 2, 3);
|
||||
|
||||
/*
|
||||
* Used for errors that compromise the whole assembly process by affecting the
|
||||
@@ -54,7 +57,7 @@ void warning(enum WarningID id, const char *fmt, ...);
|
||||
* It is also used when the assembler goes into an invalid state (for example,
|
||||
* when it fails to allocate memory).
|
||||
*/
|
||||
noreturn_ void fatalerror(const char *fmt, ...);
|
||||
_Noreturn void fatalerror(const char *fmt, ...) format_(printf, 1, 2);
|
||||
|
||||
/*
|
||||
* Used for errors that make it impossible to assemble correctly, but don't
|
||||
@@ -62,6 +65,6 @@ noreturn_ void fatalerror(const char *fmt, ...);
|
||||
* get a list of all errors at the end, making it easier to fix all of them at
|
||||
* once.
|
||||
*/
|
||||
void error(const char *fmt, ...);
|
||||
void error(const char *fmt, ...) format_(printf, 1, 2);
|
||||
|
||||
#endif
|
||||
|
||||
8
include/extern/err.h
vendored
8
include/extern/err.h
vendored
@@ -34,10 +34,10 @@ void vwarn(const char *fmt, va_list ap) format_(printf, 1, 0);
|
||||
void warnx(const char *fmt, ...) format_(printf, 1, 2);
|
||||
void vwarnx(const char *fmt, va_list ap) format_(printf, 1, 0);
|
||||
|
||||
noreturn_ void err(int status, const char *fmt, ...) format_(printf, 2, 3);
|
||||
noreturn_ void verr(int status, const char *fmt, va_list ap) format_(printf, 2, 0);
|
||||
noreturn_ void errx(int status, const char *fmt, ...) format_(printf, 2, 3);
|
||||
noreturn_ void verrx(int status, const char *fmt, va_list ap) format_(printf, 2, 0);
|
||||
_Noreturn void err(int status, const char *fmt, ...) format_(printf, 2, 3);
|
||||
_Noreturn void verr(int status, const char *fmt, va_list ap) format_(printf, 2, 0);
|
||||
_Noreturn void errx(int status, const char *fmt, ...) format_(printf, 2, 3);
|
||||
_Noreturn void verrx(int status, const char *fmt, va_list ap) format_(printf, 2, 0);
|
||||
|
||||
#endif /* ERR_IN_LIBC */
|
||||
|
||||
|
||||
4
include/extern/getopt.h
vendored
4
include/extern/getopt.h
vendored
@@ -26,8 +26,8 @@
|
||||
#ifndef RGBDS_EXTERN_GETOPT_H
|
||||
#define RGBDS_EXTERN_GETOPT_H
|
||||
|
||||
extern char *optarg;
|
||||
extern int optind, opterr, optopt, optreset;
|
||||
extern char *musl_optarg;
|
||||
extern int musl_optind, musl_opterr, musl_optopt, musl_optreset;
|
||||
|
||||
struct option {
|
||||
const char *name;
|
||||
|
||||
2
include/extern/utf8decoder.h
vendored
2
include/extern/utf8decoder.h
vendored
@@ -9,6 +9,6 @@
|
||||
#ifndef EXTERN_UTF8DECODER_H
|
||||
#define EXTERN_UTF8DECODER_H
|
||||
|
||||
uint32_t decode(uint32_t *state, uint32_t *codep, uint32_t byte);
|
||||
uint32_t decode(uint32_t *state, uint32_t *codep, uint8_t byte);
|
||||
|
||||
#endif /* EXTERN_UTF8DECODER_H */
|
||||
|
||||
@@ -33,6 +33,16 @@ typedef struct HashMapEntry *HashMap[HASHMAP_NB_BUCKETS];
|
||||
*/
|
||||
bool hash_AddElement(HashMap map, char const *key, void *element);
|
||||
|
||||
/**
|
||||
* Replaces an element with an already-present key in a hashmap.
|
||||
* @warning Inserting a NULL will make `hash_GetElement`'s return ambiguous!
|
||||
* @param map The HashMap to replace the element in
|
||||
* @param key The key with which the element will be stored and retrieved
|
||||
* @param element The element to replace
|
||||
* @return True if the element was found and replaced
|
||||
*/
|
||||
bool hash_ReplaceElement(HashMap const map, char const *key, void *element);
|
||||
|
||||
/**
|
||||
* Removes an element from a hashmap.
|
||||
* @param map The HashMap to remove the element from
|
||||
|
||||
@@ -9,21 +9,82 @@
|
||||
#ifndef HELPERS_H
|
||||
#define HELPERS_H
|
||||
|
||||
#ifdef __GNUC__
|
||||
/* GCC or compatible */
|
||||
#define format_(archetype, str_index, first_arg) \
|
||||
__attribute__ ((format (archetype, str_index, first_arg)))
|
||||
#define noreturn_ __attribute__ ((noreturn))
|
||||
#define trap_ __builtin_trap()
|
||||
#else
|
||||
/* Unsupported, but no need to throw a fit */
|
||||
#define format_(archetype, str_index, first_arg)
|
||||
#define noreturn_
|
||||
#define unused_
|
||||
#define trap_
|
||||
// Of course, MSVC does not support C11, so no _Noreturn there...
|
||||
#ifdef _MSC_VER
|
||||
#define _Noreturn __declspec(noreturn)
|
||||
#endif
|
||||
|
||||
/* Macros for stringification */
|
||||
// Ideally, we'd use `__has_attribute` and `__has_builtin`, but these were only introduced in GCC 9
|
||||
#ifdef __GNUC__ // GCC or compatible
|
||||
#define format_(archetype, str_index, first_arg) \
|
||||
__attribute__ ((format (archetype, str_index, first_arg)))
|
||||
// In release builds, define "unreachable" as such, but trap in debug builds
|
||||
#ifdef NDEBUG
|
||||
#define unreachable_ __builtin_unreachable
|
||||
#else
|
||||
#define unreachable_ __builtin_trap
|
||||
#endif
|
||||
#else
|
||||
// Unsupported, but no need to throw a fit
|
||||
#define format_(archetype, str_index, first_arg)
|
||||
// This seems to generate similar code to __builtin_unreachable, despite different semantics
|
||||
// Note that executing this is undefined behavior (declared _Noreturn, but does return)
|
||||
static inline _Noreturn unreachable_(void) {}
|
||||
#endif
|
||||
|
||||
// Use builtins whenever possible, and shim them otherwise
|
||||
#ifdef __GNUC__ // GCC or compatible
|
||||
#define ctz __builtin_ctz
|
||||
#define clz __builtin_clz
|
||||
|
||||
#elif defined(_MSC_VER)
|
||||
#include <assert.h>
|
||||
#include <intrin.h>
|
||||
#pragma intrinsic(_BitScanReverse, _BitScanForward)
|
||||
static inline int ctz(unsigned int x)
|
||||
{
|
||||
unsigned long cnt;
|
||||
|
||||
assert(x != 0);
|
||||
_BitScanForward(&cnt, x);
|
||||
return cnt;
|
||||
}
|
||||
static inline int clz(unsigned int x)
|
||||
{
|
||||
unsigned long cnt;
|
||||
|
||||
assert(x != 0);
|
||||
_BitScanReverse(&cnt, x);
|
||||
return 31 - cnt;
|
||||
}
|
||||
|
||||
#else
|
||||
// FIXME: these are rarely used, and need testing...
|
||||
#include <limits.h>
|
||||
static inline int ctz(unsigned int x)
|
||||
{
|
||||
int cnt = 0;
|
||||
|
||||
while (!(x & 1)) {
|
||||
x >>= 1;
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
static inline int clz(unsigned int x)
|
||||
{
|
||||
int cnt = 0;
|
||||
|
||||
while (x <= UINT_MAX / 2) {
|
||||
x <<= 1;
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Macros for stringification
|
||||
#define STR(x) #x
|
||||
#define EXPAND_AND_STR(x) STR(x)
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ void warning(struct FileStackNode const *where, uint32_t lineNo,
|
||||
void error(struct FileStackNode const *where, uint32_t lineNo,
|
||||
char const *fmt, ...) format_(printf, 3, 4);
|
||||
|
||||
noreturn_ void fatal(struct FileStackNode const *where, uint32_t lineNo,
|
||||
_Noreturn void fatal(struct FileStackNode const *where, uint32_t lineNo,
|
||||
char const *fmt, ...) format_(printf, 3, 4);
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
#define RGBDS_OBJECT_VERSION_STRING "RGB%1u"
|
||||
#define RGBDS_OBJECT_VERSION_NUMBER 9U
|
||||
#define RGBDS_OBJECT_REV 6U
|
||||
#define RGBDS_OBJECT_REV 7U
|
||||
|
||||
enum AssertionType {
|
||||
ASSERT_WARN,
|
||||
@@ -29,6 +29,7 @@ enum RPNCommand {
|
||||
RPN_DIV = 0x03,
|
||||
RPN_MOD = 0x04,
|
||||
RPN_UNSUB = 0x05,
|
||||
RPN_EXP = 0x06,
|
||||
|
||||
RPN_OR = 0x10,
|
||||
RPN_AND = 0x11,
|
||||
|
||||
20
include/opmath.h
Normal file
20
include/opmath.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* This file is part of RGBDS.
|
||||
*
|
||||
* Copyright (c) 1997-2021, RGBDS contributors.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#ifndef RGBDS_OP_MATH_H
|
||||
#define RGBDS_OP_MATH_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
int32_t op_divide(int32_t dividend, int32_t divisor);
|
||||
int32_t op_modulo(int32_t dividend, int32_t divisor);
|
||||
int32_t op_exponent(int32_t base, uint32_t power);
|
||||
int32_t op_shift_left(int32_t value, int32_t amount);
|
||||
int32_t op_shift_right(int32_t value, int32_t amount);
|
||||
|
||||
#endif /* RGBDS_OP_MATH_H */
|
||||
@@ -34,9 +34,40 @@
|
||||
|
||||
/* MSVC doesn't use POSIX types or defines for `read` */
|
||||
#ifdef _MSC_VER
|
||||
# include <io.h>
|
||||
# define STDIN_FILENO 0
|
||||
# define STDOUT_FILENO 1
|
||||
# define STDERR_FILENO 2
|
||||
# define ssize_t int
|
||||
# define SSIZE_MAX INT_MAX
|
||||
#else
|
||||
# include <fcntl.h>
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
/* MSVC doesn't support `[static N]` for array arguments from C99 */
|
||||
#ifdef _MSC_VER
|
||||
# define MIN_NB_ELMS(N)
|
||||
#else
|
||||
# define MIN_NB_ELMS(N) static (N)
|
||||
#endif
|
||||
|
||||
// MSVC uses a different name for O_RDWR, and needs an additional _O_BINARY flag
|
||||
#ifdef _MSC_VER
|
||||
# include <fcntl.h>
|
||||
# define O_RDWR _O_RDWR
|
||||
# define S_ISREG(field) ((field) & _S_IFREG)
|
||||
# define O_BINARY _O_BINARY
|
||||
#elif !defined(O_BINARY) // Cross-compilers define O_BINARY
|
||||
# define O_BINARY 0 // POSIX says we shouldn't care!
|
||||
#endif // _MSC_VER
|
||||
|
||||
// Windows has stdin and stdout open as text by default, which we may not want
|
||||
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||
# include <io.h>
|
||||
# define setmode(fd, mode) _setmode(fd, mode)
|
||||
#else
|
||||
# define setmode(fd, mode) ((void)0)
|
||||
#endif
|
||||
|
||||
#endif /* RGBDS_PLATFORM_H */
|
||||
|
||||
@@ -9,9 +9,10 @@
|
||||
#ifndef EXTERN_VERSION_H
|
||||
#define EXTERN_VERSION_H
|
||||
|
||||
#define PACKAGE_VERSION_MAJOR (0)
|
||||
#define PACKAGE_VERSION_MINOR (4)
|
||||
#define PACKAGE_VERSION_PATCH (1)
|
||||
#define PACKAGE_VERSION_MAJOR 0
|
||||
#define PACKAGE_VERSION_MINOR 5
|
||||
#define PACKAGE_VERSION_PATCH 0
|
||||
#define PACKAGE_VERSION_RC 1
|
||||
|
||||
const char *get_package_version_string(void);
|
||||
|
||||
|
||||
@@ -12,29 +12,45 @@ set(common_src
|
||||
"version.c"
|
||||
)
|
||||
|
||||
find_package(BISON REQUIRED)
|
||||
find_package(PkgConfig)
|
||||
|
||||
if(NOT PKG_CONFIG_FOUND)
|
||||
if(MSVC OR NOT PKG_CONFIG_FOUND)
|
||||
# fallback to find_package
|
||||
find_package(PNG REQUIRED)
|
||||
else()
|
||||
pkg_check_modules(LIBPNG REQUIRED libpng)
|
||||
endif()
|
||||
|
||||
BISON_TARGET(ASMy "asm/asmy.y"
|
||||
"${PROJECT_SOURCE_DIR}/src/asm/asmy.c"
|
||||
DEFINES_FILE "${PROJECT_SOURCE_DIR}/src/asm/asmy.h"
|
||||
find_package(BISON REQUIRED)
|
||||
set(BISON_FLAGS "-Wall")
|
||||
# Set sompe optimization flags on versions that support them
|
||||
if(BISON_VERSION VERSION_GREATER_EQUAL "3.5")
|
||||
set(BISON_FLAGS "${BISON_FLAGS} -Dapi.token.raw=true")
|
||||
endif()
|
||||
if(BISON_VERSION VERSION_GREATER_EQUAL "3.6")
|
||||
set(BISON_FLAGS "${BISON_FLAGS} -Dparse.error=detailed")
|
||||
elseif(BISON_VERSION VERSION_GREATER_EQUAL "3.0")
|
||||
set(BISON_FLAGS "${BISON_FLAGS} -Dparse.error=verbose")
|
||||
endif()
|
||||
if(BISON_VERSION VERSION_GREATER_EQUAL "3.0")
|
||||
set(BISON_FLAGS "${BISON_FLAGS} -Dparse.lac=full")
|
||||
set(BISON_FLAGS "${BISON_FLAGS} -Dlr.type=ielr")
|
||||
endif()
|
||||
BISON_TARGET(PARSER "asm/parser.y"
|
||||
"${PROJECT_SOURCE_DIR}/src/asm/parser.c"
|
||||
COMPILE_FLAGS "${BISON_FLAGS}"
|
||||
DEFINES_FILE "${PROJECT_SOURCE_DIR}/src/asm/parser.h"
|
||||
)
|
||||
|
||||
set(rgbasm_src
|
||||
"${BISON_ASMy_OUTPUT_SOURCE}"
|
||||
"${BISON_PARSER_OUTPUT_SOURCE}"
|
||||
"asm/charmap.c"
|
||||
"asm/fixpoint.c"
|
||||
"asm/format.c"
|
||||
"asm/fstack.c"
|
||||
"asm/lexer.c"
|
||||
"asm/macro.c"
|
||||
"asm/main.c"
|
||||
"asm/math.c"
|
||||
"asm/opt.c"
|
||||
"asm/output.c"
|
||||
"asm/rpn.c"
|
||||
"asm/section.c"
|
||||
@@ -44,6 +60,7 @@ set(rgbasm_src
|
||||
"extern/utf8decoder.c"
|
||||
"hashmap.c"
|
||||
"linkdefs.c"
|
||||
"opmath.c"
|
||||
)
|
||||
|
||||
set(rgbfix_src
|
||||
@@ -67,6 +84,7 @@ set(rgblink_src
|
||||
"link/symbol.c"
|
||||
"hashmap.c"
|
||||
"linkdefs.c"
|
||||
"opmath.c"
|
||||
)
|
||||
|
||||
foreach(PROG "asm" "fix" "gfx" "link")
|
||||
|
||||
4
src/asm/.gitignore
vendored
4
src/asm/.gitignore
vendored
@@ -1,2 +1,2 @@
|
||||
asmy.c
|
||||
asmy.h
|
||||
/parser.c
|
||||
/parser.h
|
||||
|
||||
1631
src/asm/asmy.y
1631
src/asm/asmy.y
File diff suppressed because it is too large
Load Diff
@@ -13,7 +13,6 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "asm/asm.h"
|
||||
#include "asm/charmap.h"
|
||||
#include "asm/main.h"
|
||||
#include "asm/output.h"
|
||||
@@ -173,6 +172,7 @@ void charmap_Add(char *mapping, uint8_t value)
|
||||
if (currentCharmap->usedNodes == currentCharmap->capacity) {
|
||||
currentCharmap->capacity *= 2;
|
||||
currentCharmap = resizeCharmap(currentCharmap, currentCharmap->capacity);
|
||||
hash_ReplaceElement(charmaps, currentCharmap->name, currentCharmap);
|
||||
}
|
||||
|
||||
/* Switch to and init new node */
|
||||
@@ -182,7 +182,7 @@ void charmap_Add(char *mapping, uint8_t value)
|
||||
}
|
||||
|
||||
if (node->isTerminal)
|
||||
warning(WARNING_CHARMAP_REDEF, "Overriding charmap mapping");
|
||||
warning(WARNING_CHARMAP_REDEF, "Overriding charmap mapping\n");
|
||||
|
||||
node->isTerminal = true;
|
||||
node->value = value;
|
||||
@@ -228,6 +228,10 @@ size_t charmap_Convert(char const *input, uint8_t *output)
|
||||
} else if (*input) { /* No match found */
|
||||
size_t codepointLen = readUTF8Char(output, input);
|
||||
|
||||
if (codepointLen == 0) {
|
||||
error("Input string is not valid UTF-8!\n");
|
||||
break;
|
||||
}
|
||||
input += codepointLen; /* OK because UTF-8 has no NUL in multi-byte chars */
|
||||
output += codepointLen;
|
||||
outputLen += codepointLen;
|
||||
|
||||
170
src/asm/fixpoint.c
Normal file
170
src/asm/fixpoint.c
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* This file is part of RGBDS.
|
||||
*
|
||||
* Copyright (c) 1997-2021, Carsten Sorensen and RGBDS contributors.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
/*
|
||||
* Fixed-point math routines
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "asm/fixpoint.h"
|
||||
#include "asm/symbol.h"
|
||||
#include "asm/warning.h"
|
||||
|
||||
#define fix2double(i) ((double)((i) / 65536.0))
|
||||
#define double2fix(d) ((int32_t)round((d) * 65536.0))
|
||||
|
||||
// pi radians == 32768 fixed-point "degrees"
|
||||
#define fdeg2rad(f) ((f) * (M_PI / 32768.0))
|
||||
#define rad2fdeg(r) ((r) * (32768.0 / M_PI))
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Return the _PI symbol value
|
||||
*/
|
||||
int32_t fix_Callback_PI(void)
|
||||
{
|
||||
warning(WARNING_OBSOLETE, "`_PI` is deprecated; use 3.14159\n");
|
||||
|
||||
return double2fix(M_PI);
|
||||
}
|
||||
|
||||
/*
|
||||
* Print a fixed point value
|
||||
*/
|
||||
void fix_Print(int32_t i)
|
||||
{
|
||||
uint32_t u = i;
|
||||
const char *sign = "";
|
||||
|
||||
if (i < 0) {
|
||||
u = -u;
|
||||
sign = "-";
|
||||
}
|
||||
|
||||
printf("%s%" PRIu32 ".%05" PRIu32, sign, u >> 16,
|
||||
((uint32_t)(fix2double(u) * 100000 + 0.5)) % 100000);
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate sine
|
||||
*/
|
||||
int32_t fix_Sin(int32_t i)
|
||||
{
|
||||
return double2fix(sin(fdeg2rad(fix2double(i))));
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate cosine
|
||||
*/
|
||||
int32_t fix_Cos(int32_t i)
|
||||
{
|
||||
return double2fix(cos(fdeg2rad(fix2double(i))));
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate tangent
|
||||
*/
|
||||
int32_t fix_Tan(int32_t i)
|
||||
{
|
||||
return double2fix(tan(fdeg2rad(fix2double(i))));
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate arcsine
|
||||
*/
|
||||
int32_t fix_ASin(int32_t i)
|
||||
{
|
||||
return double2fix(rad2fdeg(asin(fix2double(i))));
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate arccosine
|
||||
*/
|
||||
int32_t fix_ACos(int32_t i)
|
||||
{
|
||||
return double2fix(rad2fdeg(acos(fix2double(i))));
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate arctangent
|
||||
*/
|
||||
int32_t fix_ATan(int32_t i)
|
||||
{
|
||||
return double2fix(rad2fdeg(atan(fix2double(i))));
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate atan2
|
||||
*/
|
||||
int32_t fix_ATan2(int32_t i, int32_t j)
|
||||
{
|
||||
return double2fix(rad2fdeg(atan2(fix2double(i), fix2double(j))));
|
||||
}
|
||||
|
||||
/*
|
||||
* Multiplication
|
||||
*/
|
||||
int32_t fix_Mul(int32_t i, int32_t j)
|
||||
{
|
||||
return double2fix(fix2double(i) * fix2double(j));
|
||||
}
|
||||
|
||||
/*
|
||||
* Division
|
||||
*/
|
||||
int32_t fix_Div(int32_t i, int32_t j)
|
||||
{
|
||||
return double2fix(fix2double(i) / fix2double(j));
|
||||
}
|
||||
|
||||
/*
|
||||
* Power
|
||||
*/
|
||||
int32_t fix_Pow(int32_t i, int32_t j)
|
||||
{
|
||||
return double2fix(pow(fix2double(i), fix2double(j)));
|
||||
}
|
||||
|
||||
/*
|
||||
* Logarithm
|
||||
*/
|
||||
int32_t fix_Log(int32_t i, int32_t j)
|
||||
{
|
||||
return double2fix(log(fix2double(i)) / log(fix2double(j)));
|
||||
}
|
||||
|
||||
/*
|
||||
* Round
|
||||
*/
|
||||
int32_t fix_Round(int32_t i)
|
||||
{
|
||||
return double2fix(round(fix2double(i)));
|
||||
}
|
||||
|
||||
/*
|
||||
* Ceil
|
||||
*/
|
||||
int32_t fix_Ceil(int32_t i)
|
||||
{
|
||||
return double2fix(ceil(fix2double(i)));
|
||||
}
|
||||
|
||||
/*
|
||||
* Floor
|
||||
*/
|
||||
int32_t fix_Floor(int32_t i)
|
||||
{
|
||||
return double2fix(floor(fix2double(i)));
|
||||
}
|
||||
299
src/asm/format.c
Normal file
299
src/asm/format.c
Normal file
@@ -0,0 +1,299 @@
|
||||
/*
|
||||
* This file is part of RGBDS.
|
||||
*
|
||||
* Copyright (c) 2020, RGBDS contributors.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "asm/format.h"
|
||||
#include "asm/warning.h"
|
||||
|
||||
struct FormatSpec fmt_NewSpec(void)
|
||||
{
|
||||
struct FormatSpec fmt = {0};
|
||||
|
||||
return fmt;
|
||||
}
|
||||
|
||||
bool fmt_IsEmpty(struct FormatSpec const *fmt)
|
||||
{
|
||||
return !fmt->state;
|
||||
}
|
||||
|
||||
bool fmt_IsValid(struct FormatSpec const *fmt)
|
||||
{
|
||||
return fmt->valid || fmt->state == FORMAT_DONE;
|
||||
}
|
||||
|
||||
bool fmt_IsFinished(struct FormatSpec const *fmt)
|
||||
{
|
||||
return fmt->state >= FORMAT_DONE;
|
||||
}
|
||||
|
||||
void fmt_UseCharacter(struct FormatSpec *fmt, int c)
|
||||
{
|
||||
if (fmt->state == FORMAT_INVALID)
|
||||
return;
|
||||
|
||||
switch (c) {
|
||||
/* sign */
|
||||
case ' ':
|
||||
case '+':
|
||||
if (fmt->state > FORMAT_SIGN)
|
||||
goto invalid;
|
||||
fmt->state = FORMAT_PREFIX;
|
||||
fmt->sign = c;
|
||||
break;
|
||||
|
||||
/* prefix */
|
||||
case '#':
|
||||
if (fmt->state > FORMAT_PREFIX)
|
||||
goto invalid;
|
||||
fmt->state = FORMAT_ALIGN;
|
||||
fmt->prefix = true;
|
||||
break;
|
||||
|
||||
/* align */
|
||||
case '-':
|
||||
if (fmt->state > FORMAT_ALIGN)
|
||||
goto invalid;
|
||||
fmt->state = FORMAT_WIDTH;
|
||||
fmt->alignLeft = true;
|
||||
break;
|
||||
|
||||
/* pad and width */
|
||||
case '0':
|
||||
if (fmt->state < FORMAT_WIDTH)
|
||||
fmt->padZero = true;
|
||||
/* fallthrough */
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
if (fmt->state < FORMAT_WIDTH) {
|
||||
fmt->state = FORMAT_WIDTH;
|
||||
fmt->width = c - '0';
|
||||
} else if (fmt->state == FORMAT_WIDTH) {
|
||||
fmt->width = fmt->width * 10 + (c - '0');
|
||||
} else if (fmt->state == FORMAT_FRAC) {
|
||||
fmt->fracWidth = fmt->fracWidth * 10 + (c - '0');
|
||||
} else {
|
||||
goto invalid;
|
||||
}
|
||||
break;
|
||||
|
||||
case '.':
|
||||
if (fmt->state > FORMAT_WIDTH)
|
||||
goto invalid;
|
||||
fmt->state = FORMAT_FRAC;
|
||||
fmt->hasFrac = true;
|
||||
break;
|
||||
|
||||
/* type */
|
||||
case 'd':
|
||||
case 'u':
|
||||
case 'X':
|
||||
case 'x':
|
||||
case 'b':
|
||||
case 'o':
|
||||
case 'f':
|
||||
case 's':
|
||||
if (fmt->state >= FORMAT_DONE)
|
||||
goto invalid;
|
||||
fmt->state = FORMAT_DONE;
|
||||
fmt->valid = true;
|
||||
fmt->type = c;
|
||||
break;
|
||||
|
||||
default:
|
||||
invalid:
|
||||
fmt->state = FORMAT_INVALID;
|
||||
fmt->valid = false;
|
||||
}
|
||||
}
|
||||
|
||||
void fmt_FinishCharacters(struct FormatSpec *fmt)
|
||||
{
|
||||
if (!fmt_IsValid(fmt))
|
||||
fmt->state = FORMAT_INVALID;
|
||||
}
|
||||
|
||||
void fmt_PrintString(char *buf, size_t bufLen, struct FormatSpec const *fmt, char const *value)
|
||||
{
|
||||
if (fmt->sign)
|
||||
error("Formatting string with sign flag '%c'\n", fmt->sign);
|
||||
if (fmt->prefix)
|
||||
error("Formatting string with prefix flag '#'\n");
|
||||
if (fmt->padZero)
|
||||
error("Formatting string with padding flag '0'\n");
|
||||
if (fmt->hasFrac)
|
||||
error("Formatting string with fractional width\n");
|
||||
if (fmt->type != 's')
|
||||
error("Formatting string as type '%c'\n", fmt->type);
|
||||
|
||||
size_t len = strlen(value);
|
||||
size_t totalLen = fmt->width > len ? fmt->width : len;
|
||||
|
||||
if (totalLen + 1 > bufLen) /* bufLen includes terminator */
|
||||
error("Formatted string value too long\n");
|
||||
|
||||
size_t padLen = fmt->width > len ? fmt->width - len : 0;
|
||||
|
||||
if (fmt->alignLeft) {
|
||||
strncpy(buf, value, len < bufLen ? len : bufLen);
|
||||
for (size_t i = 0; i < totalLen && len + i < bufLen; i++)
|
||||
buf[len + i] = ' ';
|
||||
} else {
|
||||
for (size_t i = 0; i < padLen && i < bufLen; i++)
|
||||
buf[i] = ' ';
|
||||
if (bufLen > padLen)
|
||||
strncpy(buf + padLen, value, bufLen - padLen - 1);
|
||||
}
|
||||
|
||||
buf[totalLen] = '\0';
|
||||
}
|
||||
|
||||
void fmt_PrintNumber(char *buf, size_t bufLen, struct FormatSpec const *fmt, uint32_t value)
|
||||
{
|
||||
if (fmt->type != 'X' && fmt->type != 'x' && fmt->type != 'b' && fmt->type != 'o'
|
||||
&& fmt->prefix)
|
||||
error("Formatting type '%c' with prefix flag '#'\n", fmt->type);
|
||||
if (fmt->type != 'f' && fmt->hasFrac)
|
||||
error("Formatting type '%c' with fractional width\n", fmt->type);
|
||||
if (fmt->type == 's')
|
||||
error("Formatting number as type 's'\n");
|
||||
|
||||
char sign = fmt->sign; /* 0 or ' ' or '+' */
|
||||
|
||||
if (fmt->type == 'd' || fmt->type == 'f') {
|
||||
int32_t v = value;
|
||||
|
||||
if (v < 0 && v != INT32_MIN) {
|
||||
sign = '-';
|
||||
value = -v;
|
||||
}
|
||||
}
|
||||
|
||||
char prefix = !fmt->prefix ? 0
|
||||
: fmt->type == 'X' ? '$'
|
||||
: fmt->type == 'x' ? '$'
|
||||
: fmt->type == 'b' ? '%'
|
||||
: fmt->type == 'o' ? '&'
|
||||
: 0;
|
||||
|
||||
char valueBuf[262]; /* Max 5 digits + decimal + 255 fraction digits + terminator */
|
||||
|
||||
if (fmt->type == 'b') {
|
||||
/* Special case for binary */
|
||||
char *ptr = valueBuf;
|
||||
|
||||
do {
|
||||
*ptr++ = (value & 1) + '0';
|
||||
value >>= 1;
|
||||
} while (value);
|
||||
|
||||
*ptr = '\0';
|
||||
|
||||
/* Reverse the digits */
|
||||
size_t valueLen = ptr - valueBuf;
|
||||
|
||||
for (size_t i = 0, j = valueLen - 1; i < j; i++, j--) {
|
||||
char c = valueBuf[i];
|
||||
|
||||
valueBuf[i] = valueBuf[j];
|
||||
valueBuf[j] = c;
|
||||
}
|
||||
} else if (fmt->type == 'f') {
|
||||
/* Special case for fixed-point */
|
||||
|
||||
/* Default fractional width (C's is 6 for "%f"; here 5 is enough) */
|
||||
uint8_t fracWidth = fmt->hasFrac ? fmt->fracWidth : 5;
|
||||
|
||||
if (fracWidth) {
|
||||
char spec[16]; /* Max "%" + 5-char PRIu32 + ".%0255.f" + terminator */
|
||||
|
||||
snprintf(spec, sizeof(spec), "%%" PRIu32 ".%%0%d.f", fracWidth);
|
||||
snprintf(valueBuf, sizeof(valueBuf), spec, value >> 16,
|
||||
(value % 65536) / 65536.0 * pow(10, fracWidth) + 0.5);
|
||||
} else {
|
||||
snprintf(valueBuf, sizeof(valueBuf), "%" PRIu32, value >> 16);
|
||||
}
|
||||
} else {
|
||||
char const *spec = fmt->type == 'd' ? "%" PRId32
|
||||
: fmt->type == 'u' ? "%" PRIu32
|
||||
: fmt->type == 'X' ? "%" PRIX32
|
||||
: fmt->type == 'x' ? "%" PRIx32
|
||||
: fmt->type == 'o' ? "%" PRIo32
|
||||
: "%" PRId32;
|
||||
|
||||
snprintf(valueBuf, sizeof(valueBuf), spec, value);
|
||||
}
|
||||
|
||||
size_t len = strlen(valueBuf);
|
||||
size_t numLen = len;
|
||||
|
||||
if (sign)
|
||||
numLen++;
|
||||
if (prefix)
|
||||
numLen++;
|
||||
|
||||
size_t totalLen = fmt->width > numLen ? fmt->width : numLen;
|
||||
|
||||
if (totalLen + 1 > bufLen) /* bufLen includes terminator */
|
||||
error("Formatted numeric value too long\n");
|
||||
|
||||
size_t padLen = fmt->width > numLen ? fmt->width - numLen : 0;
|
||||
|
||||
if (fmt->alignLeft) {
|
||||
size_t pos = 0;
|
||||
|
||||
if (sign && pos < bufLen)
|
||||
buf[pos++] = sign;
|
||||
if (prefix && pos < bufLen)
|
||||
buf[pos++] = prefix;
|
||||
|
||||
strcpy(buf + pos, valueBuf);
|
||||
pos += len;
|
||||
|
||||
for (size_t i = 0; i < totalLen && pos + i < bufLen; i++)
|
||||
buf[pos + i] = ' ';
|
||||
} else {
|
||||
size_t pos = 0;
|
||||
|
||||
if (fmt->padZero) {
|
||||
/* sign, then prefix, then zero padding */
|
||||
if (sign && pos < bufLen)
|
||||
buf[pos++] = sign;
|
||||
if (prefix && pos < bufLen)
|
||||
buf[pos++] = prefix;
|
||||
for (size_t i = 0; i < padLen && pos < bufLen; i++)
|
||||
buf[pos++] = '0';
|
||||
} else {
|
||||
/* space padding, then sign, then prefix */
|
||||
for (size_t i = 0; i < padLen && pos < bufLen; i++)
|
||||
buf[pos++] = ' ';
|
||||
if (sign && pos < bufLen)
|
||||
buf[pos++] = sign;
|
||||
if (prefix && pos < bufLen)
|
||||
buf[pos++] = prefix;
|
||||
}
|
||||
if (bufLen > pos)
|
||||
strcpy(buf + pos, valueBuf);
|
||||
}
|
||||
|
||||
buf[totalLen] = '\0';
|
||||
}
|
||||
121
src/asm/fstack.c
121
src/asm/fstack.c
@@ -21,8 +21,10 @@
|
||||
#include "asm/warning.h"
|
||||
#include "platform.h" /* S_ISDIR (stat macro) */
|
||||
|
||||
#define MAXINCPATHS 128
|
||||
|
||||
#ifdef LEXER_DEBUG
|
||||
#define dbgPrint(...) fprintf(stderr, "[lexer] " __VA_ARGS__)
|
||||
#define dbgPrint(...) fprintf(stderr, "[fstack] " __VA_ARGS__)
|
||||
#else
|
||||
#define dbgPrint(...)
|
||||
#endif
|
||||
@@ -34,6 +36,9 @@ struct Context {
|
||||
uint32_t uniqueID;
|
||||
struct MacroArgs *macroArgs; /* Macro args are *saved* here */
|
||||
uint32_t nbReptIters;
|
||||
int32_t forValue;
|
||||
int32_t forStep;
|
||||
char *forName;
|
||||
};
|
||||
|
||||
static struct Context *contextStack;
|
||||
@@ -44,7 +49,7 @@ size_t nMaxRecursionDepth;
|
||||
static unsigned int nbIncPaths = 0;
|
||||
static char const *includePaths[MAXINCPATHS];
|
||||
|
||||
char const *dumpNodeAndParents(struct FileStackNode const *node)
|
||||
static const char *dumpNodeAndParents(struct FileStackNode const *node)
|
||||
{
|
||||
char const *name;
|
||||
|
||||
@@ -85,6 +90,9 @@ void fstk_DumpCurrent(void)
|
||||
|
||||
struct FileStackNode *fstk_GetFileStack(void)
|
||||
{
|
||||
if (!contextStack)
|
||||
return NULL;
|
||||
|
||||
struct FileStackNode *node = contextStack->fileInfo;
|
||||
|
||||
/* Mark node and all of its parents as referenced if not already so they don't get freed */
|
||||
@@ -197,6 +205,12 @@ bool fstk_FindFile(char const *path, char **fullPath, size_t *size)
|
||||
|
||||
bool yywrap(void)
|
||||
{
|
||||
uint32_t nIFDepth = lexer_GetIFDepth();
|
||||
|
||||
if (nIFDepth != 0)
|
||||
fatalerror("Ended block with %" PRIu32 " unterminated IF construct%s\n",
|
||||
nIFDepth, nIFDepth == 1 ? "" : "s");
|
||||
|
||||
if (contextStack->fileInfo->type == NODE_REPT) { /* The context is a REPT block, which may loop */
|
||||
struct FileStackReptNode *fileInfo = (struct FileStackReptNode *)contextStack->fileInfo;
|
||||
|
||||
@@ -216,6 +230,17 @@ bool yywrap(void)
|
||||
contextStack->fileInfo = (struct FileStackNode *)fileInfo;
|
||||
}
|
||||
|
||||
/* If this is a FOR, update the symbol value */
|
||||
if (contextStack->forName && fileInfo->iters[0] <= contextStack->nbReptIters) {
|
||||
contextStack->forValue += contextStack->forStep;
|
||||
struct Symbol *sym = sym_AddSet(contextStack->forName,
|
||||
contextStack->forValue);
|
||||
|
||||
/* This error message will refer to the current iteration */
|
||||
if (sym->type != SYM_SET)
|
||||
fatalerror("Failed to update FOR symbol value\n");
|
||||
}
|
||||
/* Advance to the next iteration */
|
||||
fileInfo->iters[0]++;
|
||||
/* If this wasn't the last iteration, wrap instead of popping */
|
||||
if (fileInfo->iters[0] <= contextStack->nbReptIters) {
|
||||
@@ -231,17 +256,20 @@ bool yywrap(void)
|
||||
struct Context *context = contextStack;
|
||||
|
||||
contextStack = contextStack->parent;
|
||||
assert(contextDepth != 0); // This is never supposed to underflow
|
||||
contextDepth--;
|
||||
|
||||
lexer_DeleteState(context->lexerState);
|
||||
/* Restore args if a macro (not REPT) saved them */
|
||||
if (context->fileInfo->type == NODE_MACRO) {
|
||||
dbgPrint("Restoring macro args %p\n", contextStack->macroArgs);
|
||||
dbgPrint("Restoring macro args %p\n", (void *)contextStack->macroArgs);
|
||||
macro_UseNewArgs(contextStack->macroArgs);
|
||||
}
|
||||
/* Free the file stack node */
|
||||
if (!context->fileInfo->referenced)
|
||||
free(context->fileInfo);
|
||||
/* Free the FOR symbol name */
|
||||
free(context->forName);
|
||||
/* Free the entry and make its parent the current entry */
|
||||
free(context);
|
||||
|
||||
@@ -253,6 +281,7 @@ bool yywrap(void)
|
||||
/*
|
||||
* Make sure not to switch the lexer state before calling this, so the saved line no is correct
|
||||
* BE CAREFUL!! This modifies the file stack directly, you should have set up the file info first
|
||||
* Callers should set contextStack->lexerState after this so it is not NULL
|
||||
*/
|
||||
static void newContext(struct FileStackNode *fileInfo)
|
||||
{
|
||||
@@ -267,13 +296,13 @@ static void newContext(struct FileStackNode *fileInfo)
|
||||
fileInfo->referenced = false;
|
||||
fileInfo->lineNo = lexer_GetLineNo();
|
||||
context->fileInfo = fileInfo;
|
||||
context->forName = NULL;
|
||||
/*
|
||||
* Link new entry to its parent so it's reachable later
|
||||
* ERRORS SHOULD NOT OCCUR AFTER THIS!!
|
||||
*/
|
||||
context->parent = contextStack;
|
||||
contextStack = context;
|
||||
|
||||
}
|
||||
|
||||
void fstk_RunInclude(char const *path)
|
||||
@@ -317,7 +346,7 @@ void fstk_RunMacro(char const *macroName, struct MacroArgs *args)
|
||||
{
|
||||
dbgPrint("Running macro \"%s\"\n", macroName);
|
||||
|
||||
struct Symbol *macro = sym_FindSymbol(macroName);
|
||||
struct Symbol *macro = sym_FindExactSymbol(macroName);
|
||||
|
||||
if (!macro) {
|
||||
error("Macro \"%s\" not defined\n", macroName);
|
||||
@@ -376,9 +405,8 @@ void fstk_RunMacro(char const *macroName, struct MacroArgs *args)
|
||||
memcpy(dest, macro->name, macroNameLen + 1);
|
||||
|
||||
newContext((struct FileStackNode *)fileInfo);
|
||||
/* Line minus 1 because buffer begins with a newline */
|
||||
contextStack->lexerState = lexer_OpenFileView(macro->macro, macro->macroSize,
|
||||
macro->fileLine - 1);
|
||||
macro->fileLine);
|
||||
if (!contextStack->lexerState)
|
||||
fatalerror("Failed to set up lexer for macro invocation\n");
|
||||
lexer_SetStateAtEOL(contextStack->lexerState);
|
||||
@@ -386,12 +414,8 @@ void fstk_RunMacro(char const *macroName, struct MacroArgs *args)
|
||||
macro_UseNewArgs(args);
|
||||
}
|
||||
|
||||
void fstk_RunRept(uint32_t count, int32_t reptLineNo, char *body, size_t size)
|
||||
static bool newReptContext(int32_t reptLineNo, char *body, size_t size)
|
||||
{
|
||||
dbgPrint("Running REPT(%" PRIu32 ")\n", count);
|
||||
if (count == 0)
|
||||
return;
|
||||
|
||||
uint32_t reptDepth = contextStack->fileInfo->type == NODE_REPT
|
||||
? ((struct FileStackReptNode *)contextStack->fileInfo)->reptDepth
|
||||
: 0;
|
||||
@@ -400,7 +424,7 @@ void fstk_RunRept(uint32_t count, int32_t reptLineNo, char *body, size_t size)
|
||||
|
||||
if (!fileInfo) {
|
||||
error("Failed to alloc file info for REPT: %s\n", strerror(errno));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
fileInfo->node.type = NODE_REPT;
|
||||
fileInfo->reptDepth = reptDepth + 1;
|
||||
@@ -417,11 +441,75 @@ void fstk_RunRept(uint32_t count, int32_t reptLineNo, char *body, size_t size)
|
||||
|
||||
contextStack->lexerState = lexer_OpenFileView(body, size, reptLineNo);
|
||||
if (!contextStack->lexerState)
|
||||
fatalerror("Failed to set up lexer for rept block\n");
|
||||
fatalerror("Failed to set up lexer for REPT block\n");
|
||||
lexer_SetStateAtEOL(contextStack->lexerState);
|
||||
contextStack->uniqueID = macro_UseNewUniqueID();
|
||||
contextStack->nbReptIters = count;
|
||||
return true;
|
||||
}
|
||||
|
||||
void fstk_RunRept(uint32_t count, int32_t reptLineNo, char *body, size_t size)
|
||||
{
|
||||
dbgPrint("Running REPT(%" PRIu32 ")\n", count);
|
||||
|
||||
if (count == 0)
|
||||
return;
|
||||
if (!newReptContext(reptLineNo, body, size))
|
||||
return;
|
||||
|
||||
contextStack->nbReptIters = count;
|
||||
contextStack->forName = NULL;
|
||||
}
|
||||
|
||||
void fstk_RunFor(char const *symName, int32_t start, int32_t stop, int32_t step,
|
||||
int32_t reptLineNo, char *body, size_t size)
|
||||
{
|
||||
dbgPrint("Running FOR(\"%s\", %" PRId32 ", %" PRId32 ", %" PRId32 ")\n",
|
||||
symName, start, stop, step);
|
||||
|
||||
struct Symbol *sym = sym_AddSet(symName, start);
|
||||
|
||||
if (sym->type != SYM_SET)
|
||||
return;
|
||||
|
||||
uint32_t count = 0;
|
||||
|
||||
if (step > 0 && start < stop)
|
||||
count = (stop - start - 1) / step + 1;
|
||||
else if (step < 0 && stop < start)
|
||||
count = (start - stop - 1) / -step + 1;
|
||||
else if (step == 0)
|
||||
error("FOR cannot have a step value of 0\n");
|
||||
|
||||
if (count == 0)
|
||||
return;
|
||||
if (!newReptContext(reptLineNo, body, size))
|
||||
return;
|
||||
|
||||
contextStack->nbReptIters = count;
|
||||
contextStack->forValue = start;
|
||||
contextStack->forStep = step;
|
||||
contextStack->forName = strdup(symName);
|
||||
if (!contextStack->forName)
|
||||
fatalerror("Not enough memory for FOR symbol name: %s\n", strerror(errno));
|
||||
}
|
||||
|
||||
void fstk_StopRept(void)
|
||||
{
|
||||
/* Prevent more iterations */
|
||||
contextStack->nbReptIters = 0;
|
||||
}
|
||||
|
||||
bool fstk_Break(void)
|
||||
{
|
||||
dbgPrint("Breaking out of REPT/FOR\n");
|
||||
|
||||
if (contextStack->fileInfo->type != NODE_REPT) {
|
||||
error("BREAK can only be used inside a REPT/FOR block\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
fstk_StopRept();
|
||||
return true;
|
||||
}
|
||||
|
||||
void fstk_Init(char const *mainPath, size_t maxRecursionDepth)
|
||||
@@ -453,6 +541,9 @@ void fstk_Init(char const *mainPath, size_t maxRecursionDepth)
|
||||
context->uniqueID = 0;
|
||||
macro_SetUniqueID(0);
|
||||
context->nbReptIters = 0;
|
||||
context->forValue = 0;
|
||||
context->forStep = 0;
|
||||
context->forName = NULL;
|
||||
|
||||
/* Now that it's set up properly, register the context */
|
||||
contextStack = context;
|
||||
|
||||
1271
src/asm/lexer.c
1271
src/asm/lexer.c
File diff suppressed because it is too large
Load Diff
@@ -2,14 +2,16 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "asm/asm.h"
|
||||
#include "asm/macro.h"
|
||||
#include "asm/warning.h"
|
||||
|
||||
#define MAXMACROARGS 99999
|
||||
|
||||
/*
|
||||
* Your average macro invocation does not go past the tens, but some go further
|
||||
* This ensures that sane and slightly insane invocations suffer no penalties,
|
||||
@@ -36,7 +38,7 @@ static uint32_t maxUniqueID = 0;
|
||||
* guarantees the size of the buffer will be correct. I was unable to find a
|
||||
* better solution, but if you have one, please feel free!
|
||||
*/
|
||||
static char uniqueIDBuf[] = "_" EXPAND_AND_STR(UINT32_MAX);
|
||||
static char uniqueIDBuf[] = "_u4294967295"; // UINT32_MAX
|
||||
static char *uniqueIDPtr = NULL;
|
||||
|
||||
struct MacroArgs *macro_GetCurrentArgs(void)
|
||||
@@ -60,14 +62,15 @@ struct MacroArgs *macro_NewArgs(void)
|
||||
void macro_AppendArg(struct MacroArgs **argPtr, char *s)
|
||||
{
|
||||
#define macArgs (*argPtr)
|
||||
if (s[0] == '\0')
|
||||
warning(WARNING_EMPTY_MACRO_ARG, "Empty macro argument\n");
|
||||
if (macArgs->nbArgs == MAXMACROARGS)
|
||||
error("A maximum of " EXPAND_AND_STR(MAXMACROARGS)
|
||||
" arguments is allowed\n");
|
||||
error("A maximum of " EXPAND_AND_STR(MAXMACROARGS) " arguments is allowed\n");
|
||||
if (macArgs->nbArgs >= macArgs->capacity) {
|
||||
macArgs->capacity *= 2;
|
||||
/* Check that overflow didn't roll us back */
|
||||
if (macArgs->capacity <= macArgs->nbArgs)
|
||||
fatalerror("Failed to add new macro argument: possible capacity overflow\n");
|
||||
fatalerror("Failed to add new macro argument: capacity overflow\n");
|
||||
macArgs = realloc(macArgs, SIZEOF_ARGS(macArgs->capacity));
|
||||
if (!macArgs)
|
||||
fatalerror("Error adding new macro argument: %s\n", strerror(errno));
|
||||
@@ -98,6 +101,40 @@ char const *macro_GetArg(uint32_t i)
|
||||
: macroArgs->args[realIndex];
|
||||
}
|
||||
|
||||
char *macro_GetAllArgs(void)
|
||||
{
|
||||
if (!macroArgs)
|
||||
return NULL;
|
||||
|
||||
if (macroArgs->shift >= macroArgs->nbArgs)
|
||||
return "";
|
||||
|
||||
size_t len = 0;
|
||||
|
||||
for (uint32_t i = macroArgs->shift; i < macroArgs->nbArgs; i++)
|
||||
len += strlen(macroArgs->args[i]) + 1; /* 1 for comma */
|
||||
|
||||
char *str = malloc(len + 1); /* 1 for '\0' */
|
||||
char *ptr = str;
|
||||
|
||||
if (!str)
|
||||
fatalerror("Failed to allocate memory for expanding '\\#': %s\n", strerror(errno));
|
||||
|
||||
for (uint32_t i = macroArgs->shift; i < macroArgs->nbArgs; i++) {
|
||||
size_t n = strlen(macroArgs->args[i]);
|
||||
|
||||
memcpy(ptr, macroArgs->args[i], n);
|
||||
ptr += n;
|
||||
|
||||
/* Commas go between args and after a last empty arg */
|
||||
if (i < macroArgs->nbArgs - 1 || n == 0)
|
||||
*ptr++ = ','; /* no space after comma */
|
||||
}
|
||||
*ptr = '\0';
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
uint32_t macro_GetUniqueID(void)
|
||||
{
|
||||
return uniqueID;
|
||||
@@ -117,7 +154,8 @@ void macro_SetUniqueID(uint32_t id)
|
||||
if (uniqueID > maxUniqueID)
|
||||
maxUniqueID = uniqueID;
|
||||
/* The buffer is guaranteed to be the correct size */
|
||||
sprintf(uniqueIDBuf, "_%" PRIu32, id);
|
||||
/* This is a valid label fragment, but not a valid numeric */
|
||||
sprintf(uniqueIDBuf, "_u%" PRIu32, id);
|
||||
uniqueIDPtr = uniqueIDBuf;
|
||||
}
|
||||
}
|
||||
@@ -128,10 +166,22 @@ uint32_t macro_UseNewUniqueID(void)
|
||||
return maxUniqueID;
|
||||
}
|
||||
|
||||
void macro_ShiftCurrentArgs(void)
|
||||
void macro_ShiftCurrentArgs(int32_t count)
|
||||
{
|
||||
if (macroArgs->shift != macroArgs->nbArgs)
|
||||
macroArgs->shift++;
|
||||
if (!macroArgs) {
|
||||
error("Cannot shift macro arguments outside of a macro\n");
|
||||
} else if (count > 0 && (count > macroArgs->nbArgs
|
||||
|| macroArgs->shift > macroArgs->nbArgs - count)) {
|
||||
warning(WARNING_MACRO_SHIFT,
|
||||
"Cannot shift macro arguments past their end\n");
|
||||
macroArgs->shift = macroArgs->nbArgs;
|
||||
} else if (count < 0 && macroArgs->shift < -count) {
|
||||
warning(WARNING_MACRO_SHIFT,
|
||||
"Cannot shift macro arguments past their beginning\n");
|
||||
macroArgs->shift = 0;
|
||||
} else {
|
||||
macroArgs->shift += count;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t macro_NbArgs(void)
|
||||
|
||||
385
src/asm/main.c
385
src/asm/main.c
@@ -19,14 +19,16 @@
|
||||
#include <time.h>
|
||||
|
||||
#include "asm/charmap.h"
|
||||
#include "asm/format.h"
|
||||
#include "asm/fstack.h"
|
||||
#include "asm/lexer.h"
|
||||
#include "asm/main.h"
|
||||
#include "asm/opt.h"
|
||||
#include "asm/output.h"
|
||||
#include "asm/rpn.h"
|
||||
#include "asm/symbol.h"
|
||||
#include "asm/warning.h"
|
||||
#include "asmy.h"
|
||||
#include "parser.h"
|
||||
|
||||
#include "extern/err.h"
|
||||
#include "extern/getopt.h"
|
||||
@@ -34,14 +36,9 @@
|
||||
#include "helpers.h"
|
||||
#include "version.h"
|
||||
|
||||
size_t cldefines_index;
|
||||
size_t cldefines_numindices;
|
||||
size_t cldefines_bufsize;
|
||||
const size_t cldefine_entrysize = 2 * sizeof(void *);
|
||||
char **cldefines;
|
||||
|
||||
clock_t nStartClock, nEndClock;
|
||||
uint32_t nTotalLines, nIFDepth;
|
||||
// Old Bison versions (confirmed for 2.3) do not forward-declare `yyparse` in the generated header
|
||||
// Unfortunately, macOS still ships 2.3, which is from 2008...
|
||||
int yyparse(void);
|
||||
|
||||
#if defined(YYDEBUG) && YYDEBUG
|
||||
extern int yydebug;
|
||||
@@ -53,162 +50,11 @@ bool oFailedOnMissingInclude;
|
||||
bool oGeneratePhonyDeps;
|
||||
char *tzTargetFileName;
|
||||
|
||||
/*
|
||||
* Option stack
|
||||
*/
|
||||
|
||||
struct sOptions DefaultOptions;
|
||||
struct sOptions CurrentOptions;
|
||||
bool haltnop;
|
||||
bool optimizeloads;
|
||||
bool verbose;
|
||||
bool warnings; /* True to enable warnings, false to disable them. */
|
||||
|
||||
struct sOptionStackEntry {
|
||||
struct sOptions Options;
|
||||
struct sOptionStackEntry *next;
|
||||
};
|
||||
|
||||
struct sOptionStackEntry *pOptionStack;
|
||||
|
||||
void opt_SetCurrentOptions(struct sOptions *opt)
|
||||
{
|
||||
CurrentOptions = *opt;
|
||||
lexer_SetGfxDigits(CurrentOptions.gbgfx);
|
||||
lexer_SetBinDigits(CurrentOptions.binary);
|
||||
}
|
||||
|
||||
void opt_Parse(char *s)
|
||||
{
|
||||
struct sOptions newopt;
|
||||
|
||||
newopt = CurrentOptions;
|
||||
|
||||
switch (s[0]) {
|
||||
case 'g':
|
||||
if (strlen(&s[1]) == 4) {
|
||||
newopt.gbgfx[0] = s[1];
|
||||
newopt.gbgfx[1] = s[2];
|
||||
newopt.gbgfx[2] = s[3];
|
||||
newopt.gbgfx[3] = s[4];
|
||||
} else {
|
||||
error("Must specify exactly 4 characters for option 'g'\n");
|
||||
}
|
||||
break;
|
||||
case 'b':
|
||||
if (strlen(&s[1]) == 2) {
|
||||
newopt.binary[0] = s[1];
|
||||
newopt.binary[1] = s[2];
|
||||
} else {
|
||||
error("Must specify exactly 2 characters for option 'b'\n");
|
||||
}
|
||||
break;
|
||||
case 'z':
|
||||
warning(WARNING_OBSOLETE, "Option 'z' is a deprecated alias for 'p'\n");
|
||||
/* fallthrough */
|
||||
case 'p':
|
||||
if (strlen(&s[1]) <= 2) {
|
||||
int result;
|
||||
unsigned int fillchar;
|
||||
|
||||
result = sscanf(&s[1], "%x", &fillchar);
|
||||
if (result != EOF && result != 1)
|
||||
error("Invalid argument for option 'z'\n");
|
||||
else
|
||||
newopt.fillchar = fillchar;
|
||||
} else {
|
||||
error("Invalid argument for option 'z'\n");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
error("Unknown option\n");
|
||||
break;
|
||||
}
|
||||
|
||||
opt_SetCurrentOptions(&newopt);
|
||||
}
|
||||
|
||||
void opt_Push(void)
|
||||
{
|
||||
struct sOptionStackEntry *pOpt;
|
||||
|
||||
pOpt = malloc(sizeof(struct sOptionStackEntry));
|
||||
|
||||
if (pOpt == NULL)
|
||||
fatalerror("No memory for option stack\n");
|
||||
|
||||
pOpt->Options = CurrentOptions;
|
||||
pOpt->next = pOptionStack;
|
||||
pOptionStack = pOpt;
|
||||
}
|
||||
|
||||
void opt_Pop(void)
|
||||
{
|
||||
if (pOptionStack == NULL)
|
||||
fatalerror("No entries in the option stack\n");
|
||||
|
||||
struct sOptionStackEntry *pOpt;
|
||||
|
||||
pOpt = pOptionStack;
|
||||
opt_SetCurrentOptions(&(pOpt->Options));
|
||||
pOptionStack = pOpt->next;
|
||||
free(pOpt);
|
||||
}
|
||||
|
||||
void opt_AddDefine(char *s)
|
||||
{
|
||||
char *value, *equals;
|
||||
|
||||
if (cldefines_index >= cldefines_numindices) {
|
||||
/* Check for overflows */
|
||||
if ((cldefines_numindices * 2) < cldefines_numindices)
|
||||
fatalerror("No memory for command line defines\n");
|
||||
|
||||
if ((cldefines_bufsize * 2) < cldefines_bufsize)
|
||||
fatalerror("No memory for command line defines\n");
|
||||
|
||||
cldefines_numindices *= 2;
|
||||
cldefines_bufsize *= 2;
|
||||
|
||||
cldefines = realloc(cldefines, cldefines_bufsize);
|
||||
if (!cldefines)
|
||||
fatalerror("No memory for command line defines\n");
|
||||
}
|
||||
equals = strchr(s, '=');
|
||||
if (equals) {
|
||||
*equals = '\0';
|
||||
value = equals + 1;
|
||||
} else {
|
||||
value = "1";
|
||||
}
|
||||
cldefines[cldefines_index++] = s;
|
||||
cldefines[cldefines_index++] = value;
|
||||
}
|
||||
|
||||
static void opt_ParseDefines(void)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < cldefines_index; i += 2)
|
||||
sym_AddString(cldefines[i], cldefines[i + 1]);
|
||||
}
|
||||
|
||||
void upperstring(char *s)
|
||||
{
|
||||
while (*s) {
|
||||
*s = toupper(*s);
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
void lowerstring(char *s)
|
||||
{
|
||||
while (*s) {
|
||||
*s = tolower(*s);
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Escapes Make-special chars from a string */
|
||||
static char *make_escape(const char *str)
|
||||
{
|
||||
@@ -230,7 +76,7 @@ static char *make_escape(const char *str)
|
||||
}
|
||||
|
||||
/* Short options */
|
||||
static char const *optstring = "b:D:Eg:hi:LM:o:p:r:VvW:w";
|
||||
static const char *optstring = "b:D:Eg:hi:LM:o:p:r:VvW:w";
|
||||
|
||||
/* Variables for the long-only options */
|
||||
static int depType; /* Variants of `-M` */
|
||||
@@ -272,7 +118,7 @@ static void print_usage(void)
|
||||
fputs(
|
||||
"Usage: rgbasm [-EhLVvw] [-b chars] [-D name[=value]] [-g chars] [-i path]\n"
|
||||
" [-M depend_file] [-MG] [-MP] [-MT target_file] [-MQ target_file]\n"
|
||||
" [-o out_file] [-p pad_value] [-r depth] [-W warning] <file> ...\n"
|
||||
" [-o out_file] [-p pad_value] [-r depth] [-W warning] <file>\n"
|
||||
"Useful options:\n"
|
||||
" -E, --export-all export all labels\n"
|
||||
" -M, --dependfile <path> set the output dependency file\n"
|
||||
@@ -291,120 +137,130 @@ int main(int argc, char *argv[])
|
||||
int ch;
|
||||
char *ep;
|
||||
|
||||
struct sOptions newopt;
|
||||
time_t now = time(NULL);
|
||||
char const *sourceDateEpoch = getenv("SOURCE_DATE_EPOCH");
|
||||
|
||||
char *tzMainfile;
|
||||
/*
|
||||
* Support SOURCE_DATE_EPOCH for reproducible builds
|
||||
* https://reproducible-builds.org/docs/source-date-epoch/
|
||||
*/
|
||||
if (sourceDateEpoch)
|
||||
now = (time_t)strtoul(sourceDateEpoch, NULL, 0);
|
||||
|
||||
dependfile = NULL;
|
||||
|
||||
/* Initial number of allocated elements in array */
|
||||
cldefines_numindices = 32;
|
||||
cldefines_bufsize = cldefines_numindices * cldefine_entrysize;
|
||||
cldefines = malloc(cldefines_bufsize);
|
||||
if (!cldefines)
|
||||
fatalerror("No memory for command line defines\n");
|
||||
|
||||
#if defined(YYDEBUG) && YYDEBUG
|
||||
yydebug = 1;
|
||||
#endif
|
||||
|
||||
// Perform some init for below
|
||||
sym_Init(now);
|
||||
|
||||
// Set defaults
|
||||
|
||||
oGeneratePhonyDeps = false;
|
||||
oGeneratedMissingIncludes = false;
|
||||
oFailedOnMissingInclude = false;
|
||||
tzTargetFileName = NULL;
|
||||
uint32_t maxRecursionDepth = 64;
|
||||
size_t nTargetFileNameLen = 0;
|
||||
|
||||
DefaultOptions.gbgfx[0] = '0';
|
||||
DefaultOptions.gbgfx[1] = '1';
|
||||
DefaultOptions.gbgfx[2] = '2';
|
||||
DefaultOptions.gbgfx[3] = '3';
|
||||
DefaultOptions.binary[0] = '0';
|
||||
DefaultOptions.binary[1] = '1';
|
||||
DefaultOptions.fillchar = 0;
|
||||
opt_B("01");
|
||||
opt_G("0123");
|
||||
opt_P(0);
|
||||
optimizeloads = true;
|
||||
haltnop = true;
|
||||
verbose = false;
|
||||
warnings = true;
|
||||
bool exportall = false;
|
||||
sym_SetExportAll(false);
|
||||
uint32_t maxRecursionDepth = 64;
|
||||
size_t nTargetFileNameLen = 0;
|
||||
|
||||
opt_SetCurrentOptions(&DefaultOptions);
|
||||
|
||||
newopt = CurrentOptions;
|
||||
|
||||
while ((ch = musl_getopt_long_only(argc, argv, optstring, longopts,
|
||||
NULL)) != -1) {
|
||||
while ((ch = musl_getopt_long_only(argc, argv, optstring, longopts, NULL)) != -1) {
|
||||
switch (ch) {
|
||||
case 'b':
|
||||
if (strlen(optarg) == 2) {
|
||||
newopt.binary[0] = optarg[1];
|
||||
newopt.binary[1] = optarg[2];
|
||||
} else {
|
||||
if (strlen(musl_optarg) == 2)
|
||||
opt_B(&musl_optarg[1]);
|
||||
else
|
||||
errx(1, "Must specify exactly 2 characters for option 'b'");
|
||||
}
|
||||
break;
|
||||
|
||||
char *equals;
|
||||
case 'D':
|
||||
opt_AddDefine(optarg);
|
||||
break;
|
||||
case 'E':
|
||||
exportall = true;
|
||||
break;
|
||||
case 'g':
|
||||
if (strlen(optarg) == 4) {
|
||||
newopt.gbgfx[0] = optarg[1];
|
||||
newopt.gbgfx[1] = optarg[2];
|
||||
newopt.gbgfx[2] = optarg[3];
|
||||
newopt.gbgfx[3] = optarg[4];
|
||||
equals = strchr(musl_optarg, '=');
|
||||
if (equals) {
|
||||
*equals = '\0';
|
||||
sym_AddString(musl_optarg, equals + 1);
|
||||
} else {
|
||||
errx(1, "Must specify exactly 4 characters for option 'g'");
|
||||
sym_AddString(musl_optarg, "1");
|
||||
}
|
||||
break;
|
||||
|
||||
case 'E':
|
||||
sym_SetExportAll(true);
|
||||
break;
|
||||
|
||||
case 'g':
|
||||
if (strlen(musl_optarg) == 4)
|
||||
opt_G(&musl_optarg[1]);
|
||||
else
|
||||
errx(1, "Must specify exactly 4 characters for option 'g'");
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
haltnop = false;
|
||||
break;
|
||||
|
||||
case 'i':
|
||||
fstk_AddIncludePath(optarg);
|
||||
fstk_AddIncludePath(musl_optarg);
|
||||
break;
|
||||
|
||||
case 'L':
|
||||
optimizeloads = false;
|
||||
break;
|
||||
|
||||
case 'M':
|
||||
if (!strcmp("-", optarg))
|
||||
if (!strcmp("-", musl_optarg))
|
||||
dependfile = stdout;
|
||||
else
|
||||
dependfile = fopen(optarg, "w");
|
||||
dependfile = fopen(musl_optarg, "w");
|
||||
if (dependfile == NULL)
|
||||
err(1, "Could not open dependfile %s",
|
||||
optarg);
|
||||
err(1, "Could not open dependfile %s", musl_optarg);
|
||||
break;
|
||||
case 'o':
|
||||
out_SetFileName(optarg);
|
||||
break;
|
||||
case 'p':
|
||||
newopt.fillchar = strtoul(optarg, &ep, 0);
|
||||
|
||||
if (optarg[0] == '\0' || *ep != '\0')
|
||||
case 'o':
|
||||
out_SetFileName(musl_optarg);
|
||||
break;
|
||||
|
||||
unsigned long fill;
|
||||
case 'p':
|
||||
fill = strtoul(musl_optarg, &ep, 0);
|
||||
|
||||
if (musl_optarg[0] == '\0' || *ep != '\0')
|
||||
errx(1, "Invalid argument for option 'p'");
|
||||
|
||||
if (newopt.fillchar < 0 || newopt.fillchar > 0xFF)
|
||||
if (fill < 0 || fill > 0xFF)
|
||||
errx(1, "Argument for option 'p' must be between 0 and 0xFF");
|
||||
|
||||
opt_P(fill);
|
||||
break;
|
||||
case 'r':
|
||||
maxRecursionDepth = strtoul(optarg, &ep, 0);
|
||||
|
||||
if (optarg[0] == '\0' || *ep != '\0')
|
||||
case 'r':
|
||||
maxRecursionDepth = strtoul(musl_optarg, &ep, 0);
|
||||
|
||||
if (musl_optarg[0] == '\0' || *ep != '\0')
|
||||
errx(1, "Invalid argument for option 'r'");
|
||||
break;
|
||||
|
||||
case 'V':
|
||||
printf("rgbasm %s\n", get_package_version_string());
|
||||
exit(0);
|
||||
case 'v':
|
||||
verbose = true;
|
||||
break;
|
||||
|
||||
case 'W':
|
||||
processWarningFlag(optarg);
|
||||
processWarningFlag(musl_optarg);
|
||||
break;
|
||||
|
||||
case 'w':
|
||||
warnings = false;
|
||||
break;
|
||||
@@ -415,36 +271,36 @@ int main(int argc, char *argv[])
|
||||
case 'G':
|
||||
oGeneratedMissingIncludes = true;
|
||||
break;
|
||||
|
||||
case 'P':
|
||||
oGeneratePhonyDeps = true;
|
||||
break;
|
||||
|
||||
case 'Q':
|
||||
case 'T':
|
||||
if (optind == argc)
|
||||
errx(1, "-M%c takes a target file name argument",
|
||||
depType);
|
||||
ep = optarg;
|
||||
if (musl_optind == argc)
|
||||
errx(1, "-M%c takes a target file name argument", depType);
|
||||
ep = musl_optarg;
|
||||
if (depType == 'Q')
|
||||
ep = make_escape(ep);
|
||||
|
||||
nTargetFileNameLen += strlen(ep) + 1;
|
||||
if (!tzTargetFileName) {
|
||||
/* On first alloc, make an empty str */
|
||||
tzTargetFileName =
|
||||
malloc(nTargetFileNameLen + 1);
|
||||
*tzTargetFileName = '\0';
|
||||
tzTargetFileName = malloc(nTargetFileNameLen + 1);
|
||||
if (tzTargetFileName)
|
||||
*tzTargetFileName = '\0';
|
||||
} else {
|
||||
tzTargetFileName =
|
||||
realloc(tzTargetFileName,
|
||||
nTargetFileNameLen + 1);
|
||||
tzTargetFileName = realloc(tzTargetFileName,
|
||||
nTargetFileNameLen + 1);
|
||||
}
|
||||
if (tzTargetFileName == NULL)
|
||||
err(1, "Cannot append new file to target file list");
|
||||
strcat(tzTargetFileName, ep);
|
||||
if (depType == 'Q')
|
||||
free(ep);
|
||||
char *ptr = tzTargetFileName +
|
||||
strlen(tzTargetFileName);
|
||||
char *ptr = tzTargetFileName + strlen(tzTargetFileName);
|
||||
|
||||
*ptr++ = ' ';
|
||||
*ptr = '\0';
|
||||
break;
|
||||
@@ -457,76 +313,49 @@ int main(int argc, char *argv[])
|
||||
/* NOTREACHED */
|
||||
}
|
||||
}
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
|
||||
if (tzTargetFileName == NULL)
|
||||
tzTargetFileName = tzObjectname;
|
||||
|
||||
opt_SetCurrentOptions(&newopt);
|
||||
|
||||
DefaultOptions = CurrentOptions;
|
||||
|
||||
if (argc == 0) {
|
||||
fputs("FATAL: no input files\n", stderr);
|
||||
if (argc == musl_optind) {
|
||||
fputs("FATAL: No input files\n", stderr);
|
||||
print_usage();
|
||||
} else if (argc != musl_optind + 1) {
|
||||
fputs("FATAL: More than one input file given\n", stderr);
|
||||
print_usage();
|
||||
}
|
||||
|
||||
tzMainfile = argv[argc - 1];
|
||||
char const *mainFileName = argv[musl_optind];
|
||||
|
||||
if (verbose)
|
||||
printf("Assembling %s\n", tzMainfile);
|
||||
printf("Assembling %s\n", mainFileName);
|
||||
|
||||
if (dependfile) {
|
||||
if (!tzTargetFileName)
|
||||
errx(1, "Dependency files can only be created if a target file is specified with either -o, -MQ or -MT.\n");
|
||||
errx(1, "Dependency files can only be created if a target file is specified with either -o, -MQ or -MT\n");
|
||||
|
||||
fprintf(dependfile, "%s: %s\n", tzTargetFileName, tzMainfile);
|
||||
fprintf(dependfile, "%s: %s\n", tzTargetFileName, mainFileName);
|
||||
}
|
||||
|
||||
/* Init file stack; important to do first, since it provides the file name, line, etc */
|
||||
lexer_Init();
|
||||
fstk_Init(tzMainfile, maxRecursionDepth);
|
||||
|
||||
nStartClock = clock();
|
||||
|
||||
nTotalLines = 0;
|
||||
nIFDepth = 0;
|
||||
sym_Init();
|
||||
sym_SetExportAll(exportall);
|
||||
|
||||
opt_ParseDefines();
|
||||
charmap_New("main", NULL);
|
||||
|
||||
opt_SetCurrentOptions(&DefaultOptions);
|
||||
// Init lexer and file stack, prodiving file info
|
||||
lexer_Init();
|
||||
fstk_Init(mainFileName, maxRecursionDepth);
|
||||
|
||||
// Perform parse (yyparse is auto-generated from `parser.y`)
|
||||
if (yyparse() != 0 && nbErrors == 0)
|
||||
nbErrors = 1;
|
||||
|
||||
if (yyparse() != 0 || nbErrors != 0)
|
||||
errx(1, "Assembly aborted (%u errors)!", nbErrors);
|
||||
if (dependfile)
|
||||
fclose(dependfile);
|
||||
|
||||
if (nIFDepth != 0)
|
||||
errx(1, "Unterminated IF construct (%" PRIu32 " levels)!",
|
||||
nIFDepth);
|
||||
|
||||
sect_CheckUnionClosed();
|
||||
|
||||
double timespent;
|
||||
|
||||
nEndClock = clock();
|
||||
timespent = ((double)(nEndClock - nStartClock))
|
||||
/ (double)CLOCKS_PER_SEC;
|
||||
if (verbose) {
|
||||
printf("Success! %" PRIu32 " lines in %d.%02d seconds ",
|
||||
nTotalLines, (int)timespent,
|
||||
((int)(timespent * 100.0)) % 100);
|
||||
if (timespent < FLT_MIN_EXP)
|
||||
printf("(INFINITY lines/minute)\n");
|
||||
else
|
||||
printf("(%d lines/minute)\n",
|
||||
(int)(60 / timespent * nTotalLines));
|
||||
}
|
||||
if (nbErrors != 0)
|
||||
errx(1, "Assembly aborted (%u errors)!", nbErrors);
|
||||
|
||||
// If parse aborted due to missing an include, and `-MG` was given, exit normally
|
||||
if (oFailedOnMissingInclude)
|
||||
return 0;
|
||||
|
||||
|
||||
147
src/asm/math.c
147
src/asm/math.c
@@ -1,147 +0,0 @@
|
||||
/*
|
||||
* This file is part of RGBDS.
|
||||
*
|
||||
* Copyright (c) 1997-2018, Carsten Sorensen and RGBDS contributors.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
/*
|
||||
* Fixedpoint math routines
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "asm/mymath.h"
|
||||
#include "asm/symbol.h"
|
||||
|
||||
#define fx2double(i) ((double)((i) / 65536.0))
|
||||
#define double2fx(d) ((int32_t)((d) * 65536.0))
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Define the _PI symbol
|
||||
*/
|
||||
void math_DefinePI(void)
|
||||
{
|
||||
sym_AddEqu("_PI", double2fx(M_PI));
|
||||
}
|
||||
|
||||
/*
|
||||
* Print a fixed point value
|
||||
*/
|
||||
void math_Print(int32_t i)
|
||||
{
|
||||
uint32_t u = i;
|
||||
const char *sign = "";
|
||||
|
||||
if (i < 0) {
|
||||
u = -u;
|
||||
sign = "-";
|
||||
}
|
||||
|
||||
printf("%s%" PRIu32 ".%05" PRIu32, sign, u >> 16,
|
||||
((uint32_t)(fx2double(u) * 100000 + 0.5)) % 100000);
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate sine
|
||||
*/
|
||||
int32_t math_Sin(int32_t i)
|
||||
{
|
||||
return double2fx(sin(fx2double(i) * 2 * M_PI / 65536));
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate cosine
|
||||
*/
|
||||
int32_t math_Cos(int32_t i)
|
||||
{
|
||||
return double2fx(cos(fx2double(i) * 2 * M_PI / 65536));
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate tangent
|
||||
*/
|
||||
int32_t math_Tan(int32_t i)
|
||||
{
|
||||
return double2fx(tan(fx2double(i) * 2 * M_PI / 65536));
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate arcsine
|
||||
*/
|
||||
int32_t math_ASin(int32_t i)
|
||||
{
|
||||
return double2fx(asin(fx2double(i)) / 2 / M_PI * 65536);
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate arccosine
|
||||
*/
|
||||
int32_t math_ACos(int32_t i)
|
||||
{
|
||||
return double2fx(acos(fx2double(i)) / 2 / M_PI * 65536);
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate arctangent
|
||||
*/
|
||||
int32_t math_ATan(int32_t i)
|
||||
{
|
||||
return double2fx(atan(fx2double(i)) / 2 / M_PI * 65536);
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate atan2
|
||||
*/
|
||||
int32_t math_ATan2(int32_t i, int32_t j)
|
||||
{
|
||||
return double2fx(atan2(fx2double(i), fx2double(j)) / 2 / M_PI * 65536);
|
||||
}
|
||||
|
||||
/*
|
||||
* Multiplication
|
||||
*/
|
||||
int32_t math_Mul(int32_t i, int32_t j)
|
||||
{
|
||||
return double2fx(fx2double(i) * fx2double(j));
|
||||
}
|
||||
|
||||
/*
|
||||
* Division
|
||||
*/
|
||||
int32_t math_Div(int32_t i, int32_t j)
|
||||
{
|
||||
return double2fx(fx2double(i) / fx2double(j));
|
||||
}
|
||||
|
||||
/*
|
||||
* Round
|
||||
*/
|
||||
int32_t math_Round(int32_t i)
|
||||
{
|
||||
return double2fx(round(fx2double(i)));
|
||||
}
|
||||
|
||||
/*
|
||||
* Ceil
|
||||
*/
|
||||
int32_t math_Ceil(int32_t i)
|
||||
{
|
||||
return double2fx(ceil(fx2double(i)));
|
||||
}
|
||||
|
||||
/*
|
||||
* Floor
|
||||
*/
|
||||
int32_t math_Floor(int32_t i)
|
||||
{
|
||||
return double2fx(floor(fx2double(i)));
|
||||
}
|
||||
111
src/asm/opt.c
Normal file
111
src/asm/opt.c
Normal file
@@ -0,0 +1,111 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "asm/lexer.h"
|
||||
#include "asm/section.h"
|
||||
#include "asm/warning.h"
|
||||
|
||||
struct OptStackEntry {
|
||||
char binary[2];
|
||||
char gbgfx[4];
|
||||
int32_t fillByte;
|
||||
struct OptStackEntry *next;
|
||||
};
|
||||
|
||||
static struct OptStackEntry *stack = NULL;
|
||||
|
||||
void opt_B(char chars[2])
|
||||
{
|
||||
lexer_SetBinDigits(chars);
|
||||
}
|
||||
|
||||
void opt_G(char chars[4])
|
||||
{
|
||||
lexer_SetGfxDigits(chars);
|
||||
}
|
||||
|
||||
void opt_P(uint8_t fill)
|
||||
{
|
||||
fillByte = fill;
|
||||
}
|
||||
|
||||
void opt_Parse(char *s)
|
||||
{
|
||||
switch (s[0]) {
|
||||
case 'b':
|
||||
if (strlen(&s[1]) == 2)
|
||||
opt_B(&s[1]);
|
||||
else
|
||||
error("Must specify exactly 2 characters for option 'b'\n");
|
||||
break;
|
||||
|
||||
case 'g':
|
||||
if (strlen(&s[1]) == 4)
|
||||
opt_G(&s[1]);
|
||||
else
|
||||
error("Must specify exactly 4 characters for option 'g'\n");
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
if (strlen(&s[1]) <= 2) {
|
||||
int result;
|
||||
unsigned int fillchar;
|
||||
|
||||
result = sscanf(&s[1], "%x", &fillchar);
|
||||
if (result != EOF && result != 1)
|
||||
error("Invalid argument for option 'p'\n");
|
||||
else
|
||||
opt_P(fillchar);
|
||||
} else {
|
||||
error("Invalid argument for option 'p'\n");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
error("Unknown option '%c'\n", s[0]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void opt_Push(void)
|
||||
{
|
||||
struct OptStackEntry *entry = malloc(sizeof(*entry));
|
||||
|
||||
if (entry == NULL)
|
||||
fatalerror("Failed to alloc option stack entry: %s\n", strerror(errno));
|
||||
|
||||
// Both of these pulled from lexer.h
|
||||
entry->binary[0] = binDigits[0];
|
||||
entry->binary[1] = binDigits[1];
|
||||
|
||||
entry->gbgfx[0] = gfxDigits[0];
|
||||
entry->gbgfx[1] = gfxDigits[1];
|
||||
entry->gbgfx[2] = gfxDigits[2];
|
||||
entry->gbgfx[3] = gfxDigits[3];
|
||||
|
||||
entry->fillByte = fillByte; // Pulled from section.h
|
||||
|
||||
entry->next = stack;
|
||||
stack = entry;
|
||||
}
|
||||
|
||||
void opt_Pop(void)
|
||||
{
|
||||
if (stack == NULL) {
|
||||
error("No entries in the option stack\n");
|
||||
return;
|
||||
}
|
||||
|
||||
struct OptStackEntry *entry = stack;
|
||||
|
||||
opt_B(entry->binary);
|
||||
opt_G(entry->gbgfx);
|
||||
opt_P(entry->fillByte);
|
||||
stack = entry->next;
|
||||
free(entry);
|
||||
}
|
||||
159
src/asm/output.c
159
src/asm/output.c
@@ -18,7 +18,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "asm/asm.h"
|
||||
#include "asm/charmap.h"
|
||||
#include "asm/fstack.h"
|
||||
#include "asm/main.h"
|
||||
@@ -69,7 +68,7 @@ static struct FileStackNode *fileStackNodes = NULL;
|
||||
/*
|
||||
* Count the number of sections used in this object
|
||||
*/
|
||||
static uint32_t countsections(void)
|
||||
static uint32_t countSections(void)
|
||||
{
|
||||
uint32_t count = 0;
|
||||
|
||||
@@ -82,7 +81,7 @@ static uint32_t countsections(void)
|
||||
/*
|
||||
* Count the number of patches used in this object
|
||||
*/
|
||||
static uint32_t countpatches(struct Section const *sect)
|
||||
static uint32_t countPatches(struct Section const *sect)
|
||||
{
|
||||
uint32_t r = 0;
|
||||
|
||||
@@ -96,7 +95,7 @@ static uint32_t countpatches(struct Section const *sect)
|
||||
/**
|
||||
* Count the number of assertions used in this object
|
||||
*/
|
||||
static uint32_t countasserts(void)
|
||||
static uint32_t countAsserts(void)
|
||||
{
|
||||
struct Assertion *assert = assertions;
|
||||
uint32_t count = 0;
|
||||
@@ -111,22 +110,22 @@ static uint32_t countasserts(void)
|
||||
/*
|
||||
* Write a long to a file (little-endian)
|
||||
*/
|
||||
static void fputlong(uint32_t i, FILE *f)
|
||||
static void putlong(uint32_t i, FILE *f)
|
||||
{
|
||||
fputc(i, f);
|
||||
fputc(i >> 8, f);
|
||||
fputc(i >> 16, f);
|
||||
fputc(i >> 24, f);
|
||||
putc(i, f);
|
||||
putc(i >> 8, f);
|
||||
putc(i >> 16, f);
|
||||
putc(i >> 24, f);
|
||||
}
|
||||
|
||||
/*
|
||||
* Write a NULL-terminated string to a file
|
||||
*/
|
||||
static void fputstring(char const *s, FILE *f)
|
||||
static void putstring(char const *s, FILE *f)
|
||||
{
|
||||
while (*s)
|
||||
fputc(*s++, f);
|
||||
fputc(0, f);
|
||||
putc(*s++, f);
|
||||
putc(0, f);
|
||||
}
|
||||
|
||||
static uint32_t getNbFileStackNodes(void)
|
||||
@@ -204,14 +203,13 @@ static uint32_t getSectIDIfAny(struct Section const *sect)
|
||||
static void writepatch(struct Patch const *patch, FILE *f)
|
||||
{
|
||||
assert(patch->src->ID != -1);
|
||||
|
||||
fputlong(patch->src->ID, f);
|
||||
fputlong(patch->lineNo, f);
|
||||
fputlong(patch->nOffset, f);
|
||||
fputlong(getSectIDIfAny(patch->pcSection), f);
|
||||
fputlong(patch->pcOffset, f);
|
||||
fputc(patch->type, f);
|
||||
fputlong(patch->nRPNSize, f);
|
||||
putlong(patch->src->ID, f);
|
||||
putlong(patch->lineNo, f);
|
||||
putlong(patch->nOffset, f);
|
||||
putlong(getSectIDIfAny(patch->pcSection), f);
|
||||
putlong(patch->pcOffset, f);
|
||||
putc(patch->type, f);
|
||||
putlong(patch->nRPNSize, f);
|
||||
fwrite(patch->pRPN, 1, patch->nRPNSize, f);
|
||||
}
|
||||
|
||||
@@ -220,23 +218,23 @@ static void writepatch(struct Patch const *patch, FILE *f)
|
||||
*/
|
||||
static void writesection(struct Section const *sect, FILE *f)
|
||||
{
|
||||
fputstring(sect->name, f);
|
||||
putstring(sect->name, f);
|
||||
|
||||
fputlong(sect->size, f);
|
||||
putlong(sect->size, f);
|
||||
|
||||
bool isUnion = sect->modifier == SECTION_UNION;
|
||||
bool isFragment = sect->modifier == SECTION_FRAGMENT;
|
||||
|
||||
fputc(sect->type | isUnion << 7 | isFragment << 6, f);
|
||||
putc(sect->type | isUnion << 7 | isFragment << 6, f);
|
||||
|
||||
fputlong(sect->org, f);
|
||||
fputlong(sect->bank, f);
|
||||
fputc(sect->align, f);
|
||||
fputlong(sect->alignOfs, f);
|
||||
putlong(sect->org, f);
|
||||
putlong(sect->bank, f);
|
||||
putc(sect->align, f);
|
||||
putlong(sect->alignOfs, f);
|
||||
|
||||
if (sect_HasData(sect->type)) {
|
||||
fwrite(sect->data, 1, sect->size, f);
|
||||
fputlong(countpatches(sect), f);
|
||||
putlong(countPatches(sect), f);
|
||||
|
||||
for (struct Patch const *patch = sect->patches; patch != NULL;
|
||||
patch = patch->next)
|
||||
@@ -249,17 +247,17 @@ static void writesection(struct Section const *sect, FILE *f)
|
||||
*/
|
||||
static void writesymbol(struct Symbol const *sym, FILE *f)
|
||||
{
|
||||
fputstring(sym->name, f);
|
||||
putstring(sym->name, f);
|
||||
if (!sym_IsDefined(sym)) {
|
||||
fputc(SYMTYPE_IMPORT, f);
|
||||
putc(SYMTYPE_IMPORT, f);
|
||||
} else {
|
||||
assert(sym->src->ID != -1);
|
||||
|
||||
fputc(sym->isExported ? SYMTYPE_EXPORT : SYMTYPE_LOCAL, f);
|
||||
fputlong(sym->src->ID, f);
|
||||
fputlong(sym->fileLine, f);
|
||||
fputlong(getSectIDIfAny(sym_GetSection(sym)), f);
|
||||
fputlong(sym->value, f);
|
||||
putc(sym->isExported ? SYMTYPE_EXPORT : SYMTYPE_LOCAL, f);
|
||||
putlong(sym->src->ID, f);
|
||||
putlong(sym->fileLine, f);
|
||||
putlong(getSectIDIfAny(sym_GetSection(sym)), f);
|
||||
putlong(sym->value, f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,6 +294,11 @@ static void writerpn(uint8_t *rpnexpr, uint32_t *rpnptr, uint8_t *rpn,
|
||||
uint8_t rpndata = popbyte();
|
||||
|
||||
switch (rpndata) {
|
||||
struct Symbol *sym;
|
||||
uint32_t value;
|
||||
uint8_t b;
|
||||
size_t i;
|
||||
|
||||
case RPN_CONST:
|
||||
writebyte(RPN_CONST);
|
||||
writebyte(popbyte());
|
||||
@@ -303,13 +306,15 @@ static void writerpn(uint8_t *rpnexpr, uint32_t *rpnptr, uint8_t *rpn,
|
||||
writebyte(popbyte());
|
||||
writebyte(popbyte());
|
||||
break;
|
||||
case RPN_SYM:
|
||||
{
|
||||
for (unsigned int i = -1; (tzSym[++i] = popbyte()); )
|
||||
;
|
||||
struct Symbol *sym = sym_FindSymbol(tzSym);
|
||||
uint32_t value;
|
||||
|
||||
case RPN_SYM:
|
||||
i = 0;
|
||||
do {
|
||||
tzSym[i] = popbyte();
|
||||
} while (tzSym[i++]);
|
||||
|
||||
// The symbol name is always written expanded
|
||||
sym = sym_FindExactSymbol(tzSym);
|
||||
if (sym_IsConstant(sym)) {
|
||||
writebyte(RPN_CONST);
|
||||
value = sym_GetConstantValue(tzSym);
|
||||
@@ -317,18 +322,22 @@ static void writerpn(uint8_t *rpnexpr, uint32_t *rpnptr, uint8_t *rpn,
|
||||
writebyte(RPN_SYM);
|
||||
value = getSymbolID(sym);
|
||||
}
|
||||
|
||||
writebyte(value & 0xFF);
|
||||
writebyte(value >> 8);
|
||||
writebyte(value >> 16);
|
||||
writebyte(value >> 24);
|
||||
break;
|
||||
}
|
||||
|
||||
case RPN_BANK_SYM:
|
||||
{
|
||||
for (unsigned int i = -1; (tzSym[++i] = popbyte()); )
|
||||
;
|
||||
struct Symbol *sym = sym_FindSymbol(tzSym);
|
||||
uint32_t value = getSymbolID(sym);
|
||||
i = 0;
|
||||
do {
|
||||
tzSym[i] = popbyte();
|
||||
} while (tzSym[i++]);
|
||||
|
||||
// The symbol name is always written expanded
|
||||
sym = sym_FindExactSymbol(tzSym);
|
||||
value = getSymbolID(sym);
|
||||
|
||||
writebyte(RPN_BANK_SYM);
|
||||
writebyte(value & 0xFF);
|
||||
@@ -336,18 +345,15 @@ static void writerpn(uint8_t *rpnexpr, uint32_t *rpnptr, uint8_t *rpn,
|
||||
writebyte(value >> 16);
|
||||
writebyte(value >> 24);
|
||||
break;
|
||||
}
|
||||
case RPN_BANK_SECT:
|
||||
{
|
||||
uint8_t b;
|
||||
|
||||
case RPN_BANK_SECT:
|
||||
writebyte(RPN_BANK_SECT);
|
||||
do {
|
||||
b = popbyte();
|
||||
writebyte(b);
|
||||
} while (b != 0);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
writebyte(rpndata);
|
||||
break;
|
||||
@@ -403,10 +409,15 @@ static struct Patch *allocpatch(uint32_t type, struct Expression const *expr, ui
|
||||
/*
|
||||
* Create a new patch (includes the rpn expr)
|
||||
*/
|
||||
void out_CreatePatch(uint32_t type, struct Expression const *expr, uint32_t ofs)
|
||||
void out_CreatePatch(uint32_t type, struct Expression const *expr, uint32_t ofs, uint32_t pcShift)
|
||||
{
|
||||
struct Patch *patch = allocpatch(type, expr, ofs);
|
||||
|
||||
// If the patch had a quantity of bytes output before it,
|
||||
// PC is not at the patch's location, but at the location
|
||||
// before those bytes.
|
||||
patch->pcOffset -= pcShift;
|
||||
|
||||
patch->next = pCurrentSection->patches;
|
||||
pCurrentSection->patches = patch;
|
||||
}
|
||||
@@ -438,30 +449,32 @@ bool out_CreateAssert(enum AssertionType type, struct Expression const *expr,
|
||||
static void writeassert(struct Assertion *assert, FILE *f)
|
||||
{
|
||||
writepatch(assert->patch, f);
|
||||
fputstring(assert->message, f);
|
||||
putstring(assert->message, f);
|
||||
}
|
||||
|
||||
static void writeFileStackNode(struct FileStackNode const *node, FILE *f)
|
||||
{
|
||||
fputlong(node->parent ? node->parent->ID : -1, f);
|
||||
fputlong(node->lineNo, f);
|
||||
fputc(node->type, f);
|
||||
putlong(node->parent ? node->parent->ID : -1, f);
|
||||
putlong(node->lineNo, f);
|
||||
putc(node->type, f);
|
||||
if (node->type != NODE_REPT) {
|
||||
fputstring(((struct FileStackNamedNode const *)node)->name, f);
|
||||
putstring(((struct FileStackNamedNode const *)node)->name, f);
|
||||
} else {
|
||||
struct FileStackReptNode const *reptNode = (struct FileStackReptNode const *)node;
|
||||
|
||||
fputlong(reptNode->reptDepth, f);
|
||||
putlong(reptNode->reptDepth, f);
|
||||
/* Iters are stored by decreasing depth, so reverse the order for output */
|
||||
for (uint32_t i = reptNode->reptDepth; i--; )
|
||||
fputlong(reptNode->iters[i], f);
|
||||
putlong(reptNode->iters[i], f);
|
||||
}
|
||||
}
|
||||
|
||||
static void registerExportedSymbol(struct Symbol *symbol, void *arg)
|
||||
static void registerUnregisteredSymbol(struct Symbol *symbol, void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
if (sym_IsExported(symbol) && symbol->ID == -1) {
|
||||
(void)arg; // sym_ForEach requires a void* parameter, but we are not using it.
|
||||
|
||||
// Check for symbol->src, to skip any built-in symbol from rgbasm
|
||||
if (symbol->src && symbol->ID == -1) {
|
||||
registerSymbol(symbol);
|
||||
}
|
||||
}
|
||||
@@ -471,21 +484,25 @@ static void registerExportedSymbol(struct Symbol *symbol, void *arg)
|
||||
*/
|
||||
void out_WriteObject(void)
|
||||
{
|
||||
FILE *f = fopen(tzObjectname, "wb");
|
||||
FILE *f;
|
||||
if (strcmp(tzObjectname, "-") != 0)
|
||||
f = fopen(tzObjectname, "wb");
|
||||
else
|
||||
f = fdopen(1, "wb");
|
||||
|
||||
if (!f)
|
||||
err(1, "Couldn't write file '%s'", tzObjectname);
|
||||
|
||||
/* Also write exported symbols that weren't written above */
|
||||
sym_ForEach(registerExportedSymbol, NULL);
|
||||
/* Also write symbols that weren't written above */
|
||||
sym_ForEach(registerUnregisteredSymbol, NULL);
|
||||
|
||||
fprintf(f, RGBDS_OBJECT_VERSION_STRING, RGBDS_OBJECT_VERSION_NUMBER);
|
||||
fputlong(RGBDS_OBJECT_REV, f);
|
||||
putlong(RGBDS_OBJECT_REV, f);
|
||||
|
||||
fputlong(nbSymbols, f);
|
||||
fputlong(countsections(), f);
|
||||
putlong(nbSymbols, f);
|
||||
putlong(countSections(), f);
|
||||
|
||||
fputlong(getNbFileStackNodes(), f);
|
||||
putlong(getNbFileStackNodes(), f);
|
||||
for (struct FileStackNode const *node = fileStackNodes; node; node = node->next) {
|
||||
writeFileStackNode(node, f);
|
||||
if (node->next && node->next->ID != node->ID - 1)
|
||||
@@ -500,7 +517,7 @@ void out_WriteObject(void)
|
||||
for (struct Section *sect = pSectionList; sect; sect = sect->next)
|
||||
writesection(sect, f);
|
||||
|
||||
fputlong(countasserts(), f);
|
||||
putlong(countAsserts(), f);
|
||||
for (struct Assertion *assert = assertions; assert;
|
||||
assert = assert->next)
|
||||
writeassert(assert, f);
|
||||
|
||||
2060
src/asm/parser.y
Normal file
2060
src/asm/parser.y
Normal file
File diff suppressed because it is too large
Load Diff
@@ -179,9 +179,9 @@ Enables literally every warning.
|
||||
.Pp
|
||||
The following warnings are actual warning flags; with each description, the corresponding warning flag is included.
|
||||
Note that each of these flag also has a negation (for example,
|
||||
.Fl Wempty-entry
|
||||
.Fl Wcharmap-redef
|
||||
enables the warning that
|
||||
.Fl Wno-empty-entry
|
||||
.Fl Wno-charmap-redef
|
||||
disables).
|
||||
Only the non-default flag is listed here.
|
||||
Ignoring the
|
||||
@@ -209,12 +209,16 @@ This warning is enabled by
|
||||
.Fl Wall .
|
||||
.It Fl Wdiv
|
||||
Warn when dividing the smallest negative integer by -1, which yields itself due to integer overflow.
|
||||
.It Fl Wempty-entry
|
||||
Warn when an empty entry is encountered in a
|
||||
.Ic db , dw , dl
|
||||
list.
|
||||
.It Fl Wempty-macro-arg
|
||||
Warn when a macro argument is empty.
|
||||
This warning is enabled by
|
||||
.Fl Wextra .
|
||||
.It Fl Wempty-strrpl
|
||||
Warn when
|
||||
.Fn STRRPL
|
||||
is called with an empty string as its second argument (the substring to replace).
|
||||
This warning is enabled by
|
||||
.Fl Wall .
|
||||
.It Fl Wlarge-constant
|
||||
Warn when a constant too large to fit in a signed 32-bit integer is encountered.
|
||||
This warning is enabled by
|
||||
@@ -223,12 +227,16 @@ This warning is enabled by
|
||||
Warn when a string too long to fit in internal buffers is encountered.
|
||||
This warning is enabled by
|
||||
.Fl Wall .
|
||||
.It Fl Wmacro-shift
|
||||
Warn when shifting macro arguments past their limits.
|
||||
This warning is enabled by
|
||||
.Fl Wextra .
|
||||
.It Fl Wno-obsolete
|
||||
Warn when obsolete constructs such as the
|
||||
.Ic jp [hl]
|
||||
instruction or
|
||||
.Ic HOME
|
||||
section type are encountered.
|
||||
.Ic _PI
|
||||
constant or
|
||||
.Ic PRINTT
|
||||
directive are encountered.
|
||||
.It Fl Wshift
|
||||
Warn when shifting right a negative value.
|
||||
Use a division by 2^N instead.
|
||||
|
||||
757
src/asm/rgbasm.5
757
src/asm/rgbasm.5
File diff suppressed because it is too large
Load Diff
138
src/asm/rpn.c
138
src/asm/rpn.c
@@ -15,15 +15,18 @@
|
||||
#include <inttypes.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "asm/asm.h"
|
||||
#include "asm/main.h"
|
||||
#include "asm/output.h"
|
||||
#include "asm/rpn.h"
|
||||
#include "asm/section.h"
|
||||
#include "asm/symbol.h"
|
||||
#include "asm/warning.h"
|
||||
|
||||
#include "opmath.h"
|
||||
|
||||
/* Makes an expression "not known", also setting its error message */
|
||||
#define makeUnknown(expr_, ...) do { \
|
||||
struct Expression *_expr = expr_; \
|
||||
@@ -46,17 +49,19 @@ static uint8_t *reserveSpace(struct Expression *expr, uint32_t size)
|
||||
/* If there isn't enough room to reserve the space, realloc */
|
||||
if (!expr->tRPN)
|
||||
expr->nRPNCapacity = 256; /* Initial size */
|
||||
else if (expr->nRPNCapacity >= MAXRPNLEN)
|
||||
/*
|
||||
* To avoid generating humongous object files, cap the
|
||||
* size of RPN expressions
|
||||
*/
|
||||
fatalerror("RPN expression cannot grow larger than "
|
||||
EXPAND_AND_STR(MAXRPNLEN) " bytes\n");
|
||||
else if (expr->nRPNCapacity > MAXRPNLEN / 2)
|
||||
expr->nRPNCapacity = MAXRPNLEN;
|
||||
else
|
||||
expr->nRPNCapacity *= 2;
|
||||
while (expr->nRPNCapacity - expr->nRPNLength < size) {
|
||||
if (expr->nRPNCapacity >= MAXRPNLEN)
|
||||
/*
|
||||
* To avoid generating humongous object files, cap the
|
||||
* size of RPN expressions
|
||||
*/
|
||||
fatalerror("RPN expression cannot grow larger than "
|
||||
EXPAND_AND_STR(MAXRPNLEN) " bytes\n");
|
||||
else if (expr->nRPNCapacity > MAXRPNLEN / 2)
|
||||
expr->nRPNCapacity = MAXRPNLEN;
|
||||
else
|
||||
expr->nRPNCapacity *= 2;
|
||||
}
|
||||
expr->tRPN = realloc(expr->tRPN, expr->nRPNCapacity);
|
||||
|
||||
if (!expr->tRPN)
|
||||
@@ -102,9 +107,9 @@ void rpn_Number(struct Expression *expr, uint32_t i)
|
||||
expr->nVal = i;
|
||||
}
|
||||
|
||||
void rpn_Symbol(struct Expression *expr, char *tzSym)
|
||||
void rpn_Symbol(struct Expression *expr, char const *tzSym)
|
||||
{
|
||||
struct Symbol *sym = sym_FindSymbol(tzSym);
|
||||
struct Symbol *sym = sym_FindScopedSymbol(tzSym);
|
||||
|
||||
if (sym_IsPC(sym) && !sect_GetSymbolSection()) {
|
||||
error("PC has no value outside a section\n");
|
||||
@@ -113,25 +118,15 @@ void rpn_Symbol(struct Expression *expr, char *tzSym)
|
||||
rpn_Init(expr);
|
||||
expr->isSymbol = true;
|
||||
|
||||
sym_Ref(tzSym);
|
||||
makeUnknown(expr, sym_IsPC(sym) ? "PC is not constant at assembly time"
|
||||
: "'%s' is not constant at assembly time", tzSym);
|
||||
sym = sym_Ref(tzSym);
|
||||
expr->nRPNPatchSize += 5; /* 1-byte opcode + 4-byte symbol ID */
|
||||
|
||||
size_t nameLen = strlen(tzSym) + 1; /* Don't forget NUL! */
|
||||
size_t nameLen = strlen(sym->name) + 1; /* Don't forget NUL! */
|
||||
uint8_t *ptr = reserveSpace(expr, nameLen + 1);
|
||||
*ptr++ = RPN_SYM;
|
||||
memcpy(ptr, tzSym, nameLen);
|
||||
|
||||
/* RGBLINK assumes PC is at the byte being computed... */
|
||||
if (sym_IsPC(sym) && nPCOffset) {
|
||||
struct Expression pc = *expr, offset;
|
||||
|
||||
rpn_Number(&offset, nPCOffset);
|
||||
rpn_BinaryOp(RPN_SUB, expr, &pc, &offset);
|
||||
if (!rpn_isKnown(expr))
|
||||
expr->isSymbol = true;
|
||||
}
|
||||
memcpy(ptr, sym->name, nameLen);
|
||||
} else {
|
||||
rpn_Number(expr, sym_GetConstantValue(tzSym));
|
||||
}
|
||||
@@ -155,7 +150,7 @@ void rpn_BankSelf(struct Expression *expr)
|
||||
|
||||
void rpn_BankSymbol(struct Expression *expr, char const *tzSym)
|
||||
{
|
||||
struct Symbol const *sym = sym_FindSymbol(tzSym);
|
||||
struct Symbol const *sym = sym_FindScopedSymbol(tzSym);
|
||||
|
||||
/* The @ symbol is treated differently. */
|
||||
if (sym_IsPC(sym)) {
|
||||
@@ -167,10 +162,8 @@ void rpn_BankSymbol(struct Expression *expr, char const *tzSym)
|
||||
if (sym && !sym_IsLabel(sym)) {
|
||||
error("BANK argument must be a label\n");
|
||||
} else {
|
||||
sym_Ref(tzSym);
|
||||
if (!sym)
|
||||
/* If the symbol didn't exist, `sym_Ref` created it */
|
||||
sym = sym_FindSymbol(tzSym);
|
||||
sym = sym_Ref(tzSym);
|
||||
assert(sym); // If the symbol didn't exist, it should have been created
|
||||
|
||||
if (sym_GetSection(sym) && sym_GetSection(sym)->bank != -1) {
|
||||
/* Symbol's section is known and bank is fixed */
|
||||
@@ -179,10 +172,10 @@ void rpn_BankSymbol(struct Expression *expr, char const *tzSym)
|
||||
makeUnknown(expr, "\"%s\"'s bank is not known", tzSym);
|
||||
expr->nRPNPatchSize += 5; /* opcode + 4-byte sect ID */
|
||||
|
||||
size_t nameLen = strlen(tzSym) + 1; /* Room for NUL! */
|
||||
size_t nameLen = strlen(sym->name) + 1; /* Room for NUL! */
|
||||
uint8_t *ptr = reserveSpace(expr, nameLen + 1);
|
||||
*ptr++ = RPN_BANK_SYM;
|
||||
memcpy(ptr, tzSym, nameLen);
|
||||
memcpy(ptr, sym->name, nameLen);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -253,50 +246,11 @@ void rpn_LOGNOT(struct Expression *expr, const struct Expression *src)
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t shift(int32_t shiftee, int32_t amount)
|
||||
{
|
||||
if (amount >= 0) {
|
||||
// Left shift
|
||||
if (amount >= 32) {
|
||||
warning(WARNING_SHIFT_AMOUNT, "Shifting left by large amount %"
|
||||
PRId32 "\n", amount);
|
||||
return 0;
|
||||
|
||||
} else {
|
||||
/*
|
||||
* Use unsigned to force a bitwise shift
|
||||
* Casting back is OK because the types implement two's
|
||||
* complement behavior
|
||||
*/
|
||||
return (uint32_t)shiftee << amount;
|
||||
}
|
||||
} else {
|
||||
// Right shift
|
||||
amount = -amount;
|
||||
if (amount >= 32) {
|
||||
warning(WARNING_SHIFT_AMOUNT,
|
||||
"Shifting right by large amount %" PRId32 "\n", amount);
|
||||
return shiftee < 0 ? -1 : 0;
|
||||
|
||||
} else if (shiftee >= 0) {
|
||||
return shiftee >> amount;
|
||||
|
||||
} else {
|
||||
/*
|
||||
* The C standard leaves shifting right negative values
|
||||
* undefined, so use a left shift manually sign-extended
|
||||
*/
|
||||
return (uint32_t)shiftee >> amount
|
||||
| -(UINT32_C(1) << (32 - amount));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Symbol const *rpn_SymbolOf(struct Expression const *expr)
|
||||
{
|
||||
if (!rpn_isSymbol(expr))
|
||||
return NULL;
|
||||
return sym_FindSymbol((char *)expr->tRPN + 1);
|
||||
return sym_FindScopedSymbol((char *)expr->tRPN + 1);
|
||||
}
|
||||
|
||||
bool rpn_IsDiffConstant(struct Expression const *src, struct Symbol const *sym)
|
||||
@@ -307,7 +261,9 @@ bool rpn_IsDiffConstant(struct Expression const *src, struct Symbol const *sym)
|
||||
if (!sym1 || !sym || sym1->type != SYM_LABEL || sym->type != SYM_LABEL)
|
||||
return false;
|
||||
|
||||
return sym_GetSection(sym1) == sym_GetSection(sym);
|
||||
struct Section const *section1 = sym_GetSection(sym1);
|
||||
struct Section const *section2 = sym_GetSection(sym);
|
||||
return section1 && (section1 == section2);
|
||||
}
|
||||
|
||||
static bool isDiffConstant(struct Expression const *src1,
|
||||
@@ -375,11 +331,17 @@ void rpn_BinaryOp(enum RPNCommand op, struct Expression *expr,
|
||||
"Shifting left by negative amount %" PRId32 "\n",
|
||||
src2->nVal);
|
||||
|
||||
expr->nVal = shift(src1->nVal, src2->nVal);
|
||||
if (src2->nVal >= 32)
|
||||
warning(WARNING_SHIFT_AMOUNT,
|
||||
"Shifting left by large amount %" PRId32 "\n",
|
||||
src2->nVal);
|
||||
|
||||
expr->nVal = op_shift_left(src1->nVal, src2->nVal);
|
||||
break;
|
||||
case RPN_SHR:
|
||||
if (src1->nVal < 0)
|
||||
warning(WARNING_SHIFT, "Shifting negative value %" PRId32 "\n",
|
||||
warning(WARNING_SHIFT, "Shifting right negative value %"
|
||||
PRId32 "\n",
|
||||
src1->nVal);
|
||||
|
||||
if (src2->nVal < 0)
|
||||
@@ -387,7 +349,12 @@ void rpn_BinaryOp(enum RPNCommand op, struct Expression *expr,
|
||||
"Shifting right by negative amount %" PRId32 "\n",
|
||||
src2->nVal);
|
||||
|
||||
expr->nVal = shift(src1->nVal, -src2->nVal);
|
||||
if (src2->nVal >= 32)
|
||||
warning(WARNING_SHIFT_AMOUNT,
|
||||
"Shifting right by large amount %" PRId32 "\n",
|
||||
src2->nVal);
|
||||
|
||||
expr->nVal = op_shift_right(src1->nVal, src2->nVal);
|
||||
break;
|
||||
case RPN_MUL:
|
||||
expr->nVal = uleft * uright;
|
||||
@@ -401,17 +368,26 @@ void rpn_BinaryOp(enum RPNCommand op, struct Expression *expr,
|
||||
PRId32 "\n", INT32_MIN, INT32_MIN);
|
||||
expr->nVal = INT32_MIN;
|
||||
} else {
|
||||
expr->nVal = src1->nVal / src2->nVal;
|
||||
expr->nVal = op_divide(src1->nVal, src2->nVal);
|
||||
}
|
||||
break;
|
||||
case RPN_MOD:
|
||||
if (src2->nVal == 0)
|
||||
fatalerror("Division by zero\n");
|
||||
fatalerror("Modulo by zero\n");
|
||||
|
||||
if (src1->nVal == INT32_MIN && src2->nVal == -1)
|
||||
expr->nVal = 0;
|
||||
else
|
||||
expr->nVal = src1->nVal % src2->nVal;
|
||||
expr->nVal = op_modulo(src1->nVal, src2->nVal);
|
||||
break;
|
||||
case RPN_EXP:
|
||||
if (src2->nVal < 0)
|
||||
fatalerror("Exponentiation by negative power\n");
|
||||
|
||||
if (src1->nVal == INT32_MIN && src2->nVal == -1)
|
||||
expr->nVal = 0;
|
||||
else
|
||||
expr->nVal = op_exponent(src1->nVal, src2->nVal);
|
||||
break;
|
||||
|
||||
case RPN_UNSUB:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
@@ -11,11 +12,14 @@
|
||||
#include "asm/output.h"
|
||||
#include "asm/rpn.h"
|
||||
#include "asm/section.h"
|
||||
#include "asm/symbol.h"
|
||||
#include "asm/warning.h"
|
||||
|
||||
#include "extern/err.h"
|
||||
#include "platform.h" // strdup
|
||||
|
||||
uint8_t fillByte;
|
||||
|
||||
struct SectionStackEntry {
|
||||
struct Section *section;
|
||||
char const *scope; /* Section's symbol scope */
|
||||
@@ -26,7 +30,7 @@ struct SectionStackEntry {
|
||||
struct SectionStackEntry *sectionStack;
|
||||
uint32_t curOffset; /* Offset into the current section (see sect_GetSymbolOffset) */
|
||||
static struct Section *currentLoadSection = NULL;
|
||||
uint32_t loadOffset; /* The offset of the LOAD section within its parent */
|
||||
int32_t loadOffset; /* Offset into the LOAD section's parent (see sect_GetOutputOffset) */
|
||||
|
||||
struct UnionStackEntry {
|
||||
uint32_t start;
|
||||
@@ -83,48 +87,247 @@ static inline void reserveSpace(uint32_t delta_size)
|
||||
|
||||
struct Section *out_FindSectionByName(const char *name)
|
||||
{
|
||||
struct Section *sect = pSectionList;
|
||||
|
||||
while (sect) {
|
||||
for (struct Section *sect = pSectionList; sect; sect = sect->next) {
|
||||
if (strcmp(name, sect->name) == 0)
|
||||
return sect;
|
||||
|
||||
sect = sect->next;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find a section by name and type. If it doesn't exist, create it
|
||||
*/
|
||||
static struct Section *getSection(char const *name, enum SectionType type,
|
||||
uint32_t org, struct SectionSpec const *attrs,
|
||||
enum SectionModifier mod)
|
||||
#define mask(align) ((1U << (align)) - 1)
|
||||
#define fail(...) \
|
||||
do { \
|
||||
error(__VA_ARGS__); \
|
||||
nbSectErrors++; \
|
||||
} while (0)
|
||||
|
||||
static unsigned int mergeSectUnion(struct Section *sect, enum SectionType type, uint32_t org,
|
||||
uint8_t alignment, uint16_t alignOffset)
|
||||
{
|
||||
assert(alignment < 16); // Should be ensured by the caller
|
||||
unsigned int nbSectErrors = 0;
|
||||
|
||||
/*
|
||||
* Unionized sections only need "compatible" constraints, and they end up with the strictest
|
||||
* combination of both.
|
||||
*/
|
||||
if (sect_HasData(type))
|
||||
fail("Cannot declare ROM sections as UNION\n");
|
||||
|
||||
if (org != -1) {
|
||||
/* If both are fixed, they must be the same */
|
||||
if (sect->org != -1 && sect->org != org)
|
||||
fail("Section already declared as fixed at different address $%04"
|
||||
PRIx32 "\n", sect->org);
|
||||
else if (sect->align != 0 && (mask(sect->align) & (org - sect->alignOfs)))
|
||||
fail("Section already declared as aligned to %u bytes (offset %"
|
||||
PRIu16 ")\n", 1U << sect->align, sect->alignOfs);
|
||||
else
|
||||
/* Otherwise, just override */
|
||||
sect->org = org;
|
||||
|
||||
} else if (alignment != 0) {
|
||||
/* Make sure any fixed address given is compatible */
|
||||
if (sect->org != -1) {
|
||||
if ((sect->org - alignOffset) & mask(alignment))
|
||||
fail("Section already declared as fixed at incompatible address $%04"
|
||||
PRIx32 "\n", sect->org);
|
||||
/* Check if alignment offsets are compatible */
|
||||
} else if ((alignOffset & mask(sect->align))
|
||||
!= (sect->alignOfs & mask(alignment))) {
|
||||
fail("Section already declared with incompatible %" PRIu8
|
||||
"-byte alignment (offset %" PRIu16 ")\n",
|
||||
sect->align, sect->alignOfs);
|
||||
} else if (alignment > sect->align) {
|
||||
// If the section is not fixed, its alignment is the largest of both
|
||||
sect->align = alignment;
|
||||
sect->alignOfs = alignOffset;
|
||||
}
|
||||
}
|
||||
|
||||
return nbSectErrors;
|
||||
}
|
||||
|
||||
static unsigned int mergeFragments(struct Section *sect, enum SectionType type, uint32_t org,
|
||||
uint8_t alignment, uint16_t alignOffset)
|
||||
{
|
||||
(void)type;
|
||||
assert(alignment < 16); // Should be ensured by the caller
|
||||
unsigned int nbSectErrors = 0;
|
||||
|
||||
/*
|
||||
* Fragments only need "compatible" constraints, and they end up with the strictest
|
||||
* combination of both.
|
||||
* The merging is however performed at the *end* of the original section!
|
||||
*/
|
||||
if (org != -1) {
|
||||
uint16_t curOrg = org - sect->size;
|
||||
|
||||
/* If both are fixed, they must be the same */
|
||||
if (sect->org != -1 && sect->org != curOrg)
|
||||
fail("Section already declared as fixed at incompatible address $%04"
|
||||
PRIx32 " (cur addr = %04" PRIx32 ")\n",
|
||||
sect->org, sect->org + sect->size);
|
||||
else if (sect->align != 0 && (mask(sect->align) & (curOrg - sect->alignOfs)))
|
||||
fail("Section already declared as aligned to %u bytes (offset %"
|
||||
PRIu16 ")\n", 1U << sect->align, sect->alignOfs);
|
||||
else
|
||||
/* Otherwise, just override */
|
||||
sect->org = curOrg;
|
||||
|
||||
} else if (alignment != 0) {
|
||||
int32_t curOfs = (alignOffset - sect->size) % (1U << alignment);
|
||||
|
||||
if (curOfs < 0)
|
||||
curOfs += 1U << alignment;
|
||||
|
||||
/* Make sure any fixed address given is compatible */
|
||||
if (sect->org != -1) {
|
||||
if ((sect->org - curOfs) & mask(alignment))
|
||||
fail("Section already declared as fixed at incompatible address $%04"
|
||||
PRIx32 "\n", sect->org);
|
||||
/* Check if alignment offsets are compatible */
|
||||
} else if ((curOfs & mask(sect->align)) != (sect->alignOfs & mask(alignment))) {
|
||||
fail("Section already declared with incompatible %" PRIu8
|
||||
"-byte alignment (offset %" PRIu16 ")\n",
|
||||
sect->align, sect->alignOfs);
|
||||
} else if (alignment > sect->align) {
|
||||
// If the section is not fixed, its alignment is the largest of both
|
||||
sect->align = alignment;
|
||||
sect->alignOfs = curOfs;
|
||||
}
|
||||
}
|
||||
|
||||
return nbSectErrors;
|
||||
}
|
||||
|
||||
static void mergeSections(struct Section *sect, enum SectionType type, uint32_t org, uint32_t bank,
|
||||
uint8_t alignment, uint16_t alignOffset, enum SectionModifier mod)
|
||||
{
|
||||
unsigned int nbSectErrors = 0;
|
||||
|
||||
if (type != sect->type)
|
||||
fail("Section already exists but with type %s\n", typeNames[sect->type]);
|
||||
|
||||
if (sect->modifier != mod) {
|
||||
fail("Section already declared as %s section\n", sectionModNames[sect->modifier]);
|
||||
} else {
|
||||
switch (mod) {
|
||||
case SECTION_UNION:
|
||||
case SECTION_FRAGMENT:
|
||||
nbSectErrors += (mod == SECTION_UNION ? mergeSectUnion : mergeFragments)
|
||||
(sect, type, org, alignment, alignOffset);
|
||||
|
||||
// Common checks
|
||||
|
||||
/* If the section's bank is unspecified, override it */
|
||||
if (sect->bank == -1)
|
||||
sect->bank = bank;
|
||||
/* If both specify a bank, it must be the same one */
|
||||
else if (bank != -1 && sect->bank != bank)
|
||||
fail("Section already declared with different bank %" PRIu32 "\n",
|
||||
sect->bank);
|
||||
break;
|
||||
|
||||
case SECTION_NORMAL:
|
||||
fail("Section already defined previously at ");
|
||||
fstk_Dump(sect->src, sect->fileLine);
|
||||
putc('\n', stderr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (nbSectErrors)
|
||||
fatalerror("Cannot create section \"%s\" (%u error%s)\n",
|
||||
sect->name, nbSectErrors, nbSectErrors == 1 ? "" : "s");
|
||||
}
|
||||
|
||||
#undef fail
|
||||
|
||||
/*
|
||||
* Create a new section, not yet in the list.
|
||||
*/
|
||||
static struct Section *createSection(char const *name, enum SectionType type,
|
||||
uint32_t org, uint32_t bank, uint8_t alignment,
|
||||
uint16_t alignOffset, enum SectionModifier mod)
|
||||
{
|
||||
struct Section *sect = malloc(sizeof(*sect));
|
||||
|
||||
if (sect == NULL)
|
||||
fatalerror("Not enough memory for section: %s\n", strerror(errno));
|
||||
|
||||
sect->name = strdup(name);
|
||||
if (sect->name == NULL)
|
||||
fatalerror("Not enough memory for section name: %s\n", strerror(errno));
|
||||
|
||||
sect->type = type;
|
||||
sect->modifier = mod;
|
||||
sect->src = fstk_GetFileStack();
|
||||
sect->fileLine = lexer_GetLineNo();
|
||||
sect->size = 0;
|
||||
sect->org = org;
|
||||
sect->bank = bank;
|
||||
sect->align = alignment;
|
||||
sect->alignOfs = alignOffset;
|
||||
sect->next = NULL;
|
||||
sect->patches = NULL;
|
||||
|
||||
/* It is only needed to allocate memory for ROM sections. */
|
||||
if (sect_HasData(type)) {
|
||||
sect->data = malloc(maxsize[type]);
|
||||
if (sect->data == NULL)
|
||||
fatalerror("Not enough memory for section: %s\n", strerror(errno));
|
||||
} else {
|
||||
sect->data = NULL;
|
||||
}
|
||||
|
||||
return sect;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find a section by name and type. If it doesn't exist, create it.
|
||||
*/
|
||||
static struct Section *getSection(char const *name, enum SectionType type, uint32_t org,
|
||||
struct SectionSpec const *attrs, enum SectionModifier mod)
|
||||
{
|
||||
#define mask(align) ((1 << (align)) - 1)
|
||||
uint32_t bank = attrs->bank;
|
||||
uint8_t alignment = attrs->alignment;
|
||||
uint16_t alignOffset = attrs->alignOfs;
|
||||
|
||||
// First, validate parameters, and normalize them if applicable
|
||||
|
||||
if (bank != -1) {
|
||||
if (type != SECTTYPE_ROMX && type != SECTTYPE_VRAM
|
||||
&& type != SECTTYPE_SRAM && type != SECTTYPE_WRAMX)
|
||||
error("BANK only allowed for ROMX, WRAMX, SRAM, or VRAM sections\n");
|
||||
else if (bank < bankranges[type][0]
|
||||
|| bank > bankranges[type][1])
|
||||
error("%s bank value $%" PRIx32 " out of range ($%" PRIx32 " to $%"
|
||||
error("%s bank value $%04" PRIx32 " out of range ($%04" PRIx32 " to $%04"
|
||||
PRIx32 ")\n", typeNames[type], bank,
|
||||
bankranges[type][0], bankranges[type][1]);
|
||||
} else if (nbbanks(type) == 1) {
|
||||
// If the section type only has a single bank, implicitly force it
|
||||
bank = bankranges[type][0];
|
||||
}
|
||||
|
||||
if (alignOffset >= 1 << alignment) {
|
||||
error("Alignment offset must not be greater than alignment (%" PRIu16 " < %u)\n",
|
||||
alignOffset, 1U << alignment);
|
||||
error("Alignment offset (%" PRIu16 ") must be smaller than alignment size (%u)\n",
|
||||
alignOffset, 1U << alignment);
|
||||
alignOffset = 0;
|
||||
}
|
||||
|
||||
if (org != -1) {
|
||||
if (org < startaddr[type] || org > endaddr(type))
|
||||
error("Section \"%s\"'s fixed address %#" PRIx32
|
||||
" is outside of range [%#" PRIx16 "; %#" PRIx16 "]\n",
|
||||
name, org, startaddr[type], endaddr(type));
|
||||
}
|
||||
|
||||
if (alignment != 0) {
|
||||
if (alignment > 16) {
|
||||
error("Alignment must be between 0 and 16, not %u\n", alignment);
|
||||
alignment = 16;
|
||||
}
|
||||
/* It doesn't make sense to have both alignment and org set */
|
||||
uint32_t mask = mask(alignment);
|
||||
|
||||
@@ -136,169 +339,28 @@ static struct Section *getSection(char const *name, enum SectionType type,
|
||||
} else if (startaddr[type] & mask) {
|
||||
error("Section \"%s\"'s alignment cannot be attained in %s\n",
|
||||
name, typeNames[type]);
|
||||
} else if (alignment == 16) {
|
||||
// Treat an alignment of 16 as being fixed at address 0
|
||||
alignment = 0;
|
||||
org = 0;
|
||||
// The address is known to be valid, since the alignment is
|
||||
}
|
||||
}
|
||||
|
||||
if (org != -1) {
|
||||
if (org < startaddr[type] || org > endaddr(type))
|
||||
error("Section \"%s\"'s fixed address %#" PRIx32
|
||||
" is outside of range [%#" PRIx16 "; %#" PRIx16 "]\n",
|
||||
name, org, startaddr[type], endaddr(type));
|
||||
}
|
||||
|
||||
if (nbbanks(type) == 1)
|
||||
bank = bankranges[type][0];
|
||||
// Check if another section exists with the same name; merge if yes, otherwise create one
|
||||
|
||||
struct Section *sect = out_FindSectionByName(name);
|
||||
|
||||
if (sect) {
|
||||
unsigned int nbSectErrors = 0;
|
||||
#define fail(...) \
|
||||
do { \
|
||||
error(__VA_ARGS__); \
|
||||
nbSectErrors++; \
|
||||
} while (0)
|
||||
|
||||
if (type != sect->type)
|
||||
fail("Section \"%s\" already exists but with type %s\n",
|
||||
sect->name, typeNames[sect->type]);
|
||||
|
||||
if (sect->modifier != mod)
|
||||
fail("Section \"%s\" already declared as %s section\n",
|
||||
sect->name, sectionModNames[sect->modifier]);
|
||||
/*
|
||||
* Normal sections need to have exactly identical constraints;
|
||||
* but unionized sections only need "compatible" constraints,
|
||||
* and they end up with the strictest combination of both
|
||||
*/
|
||||
if (mod == SECTION_UNION) {
|
||||
/*
|
||||
* WARNING: see comment about assumption in
|
||||
* `EndLoadSection` if modifying the following check!
|
||||
*/
|
||||
if (sect_HasData(type))
|
||||
fail("Cannot declare ROM sections as UNION\n");
|
||||
if (org != -1) {
|
||||
/* If both are fixed, they must be the same */
|
||||
if (sect->org != -1 && sect->org != org)
|
||||
fail("Section \"%s\" already declared as fixed at different address $%"
|
||||
PRIx32 "\n",
|
||||
sect->name, sect->org);
|
||||
else if (sect->align != 0
|
||||
&& (mask(sect->align)
|
||||
& (org - sect->alignOfs)))
|
||||
fail("Section \"%s\" already declared as aligned to %u bytes (offset %"
|
||||
PRIu16 ")\n", sect->name, 1U << sect->align, sect->alignOfs);
|
||||
else
|
||||
/* Otherwise, just override */
|
||||
sect->org = org;
|
||||
} else if (alignment != 0) {
|
||||
/* Make sure any fixed address is compatible */
|
||||
if (sect->org != -1) {
|
||||
if ((sect->org - alignOffset)
|
||||
& mask(alignment))
|
||||
fail("Section \"%s\" already declared as fixed at incompatible address $%"
|
||||
PRIx32 "\n", sect->name, sect->org);
|
||||
/* Check if alignment offsets are compatible */
|
||||
} else if ((alignOffset & mask(sect->align))
|
||||
!= (sect->alignOfs
|
||||
& mask(alignment))) {
|
||||
fail("Section \"%s\" already declared with incompatible %"
|
||||
PRIu8 "-byte alignment (offset %" PRIu16 ")\n",
|
||||
sect->name, sect->align, sect->alignOfs);
|
||||
} else if (alignment > sect->align) {
|
||||
/*
|
||||
* If the section is not fixed,
|
||||
* its alignment is the largest of both
|
||||
*/
|
||||
sect->align = alignment;
|
||||
sect->alignOfs = alignOffset;
|
||||
}
|
||||
}
|
||||
/* If the section's bank is unspecified, override it */
|
||||
if (sect->bank == -1)
|
||||
sect->bank = bank;
|
||||
/* If both specify a bank, it must be the same one */
|
||||
else if (bank != -1 && sect->bank != bank)
|
||||
fail("Section \"%s\" already declared with different bank %"
|
||||
PRIu32 "\n", sect->name, sect->bank);
|
||||
} else { /* Section fragments are handled identically in RGBASM */
|
||||
/* However, concaternating non-fragments will be made an error */
|
||||
if (sect->modifier != SECTION_FRAGMENT || mod != SECTION_FRAGMENT)
|
||||
warning(WARNING_OBSOLETE,
|
||||
"Concatenation of non-fragment sections is deprecated\n");
|
||||
|
||||
if (org != sect->org) {
|
||||
if (sect->org == -1)
|
||||
fail("Section \"%s\" already declared as floating\n",
|
||||
sect->name);
|
||||
else
|
||||
fail("Section \"%s\" already declared as fixed at $%"
|
||||
PRIx32 "\n", sect->name, sect->org);
|
||||
}
|
||||
if (bank != sect->bank) {
|
||||
if (sect->bank == -1)
|
||||
fail("Section \"%s\" already declared as floating bank\n",
|
||||
sect->name);
|
||||
else
|
||||
fail("Section \"%s\" already declared as fixed at bank %"
|
||||
PRIu32 "\n", sect->name, sect->bank);
|
||||
}
|
||||
if (alignment != sect->align) {
|
||||
if (sect->align == 0)
|
||||
fail("Section \"%s\" already declared as unaligned\n",
|
||||
sect->name);
|
||||
else
|
||||
fail("Section \"%s\" already declared as aligned to %u bytes\n",
|
||||
sect->name, 1U << sect->align);
|
||||
}
|
||||
}
|
||||
|
||||
if (nbSectErrors)
|
||||
fatalerror("Cannot create section \"%s\" (%u errors)\n",
|
||||
sect->name, nbSectErrors);
|
||||
#undef fail
|
||||
return sect;
|
||||
}
|
||||
|
||||
sect = malloc(sizeof(*sect));
|
||||
if (sect == NULL)
|
||||
fatalerror("Not enough memory for section: %s\n", strerror(errno));
|
||||
|
||||
sect->name = strdup(name);
|
||||
if (sect->name == NULL)
|
||||
fatalerror("Not enough memory for section name: %s\n", strerror(errno));
|
||||
|
||||
sect->type = type;
|
||||
sect->modifier = mod;
|
||||
sect->size = 0;
|
||||
sect->org = org;
|
||||
sect->bank = bank;
|
||||
sect->align = alignment;
|
||||
sect->alignOfs = alignOffset;
|
||||
sect->next = pSectionList;
|
||||
sect->patches = NULL;
|
||||
|
||||
/* It is only needed to allocate memory for ROM sections. */
|
||||
if (sect_HasData(type)) {
|
||||
uint32_t sectsize;
|
||||
|
||||
sectsize = maxsize[type];
|
||||
sect->data = malloc(sectsize);
|
||||
if (sect->data == NULL)
|
||||
fatalerror("Not enough memory for section: %s\n", strerror(errno));
|
||||
mergeSections(sect, type, org, bank, alignment, alignOffset, mod);
|
||||
} else {
|
||||
sect->data = NULL;
|
||||
sect = createSection(name, type, org, bank, alignment, alignOffset, mod);
|
||||
// Add the new section to the list (order doesn't matter)
|
||||
sect->next = pSectionList;
|
||||
pSectionList = sect;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add the new section to the list
|
||||
* at the beginning because order doesn't matter
|
||||
*/
|
||||
pSectionList = sect;
|
||||
|
||||
return sect;
|
||||
#undef mask
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -321,6 +383,11 @@ void out_NewSection(char const *name, uint32_t type, uint32_t org,
|
||||
if (currentLoadSection)
|
||||
fatalerror("Cannot change the section within a `LOAD` block\n");
|
||||
|
||||
for (struct SectionStackEntry *stack = sectionStack; stack; stack = stack->next) {
|
||||
if (stack->section && !strcmp(name, stack->section->name))
|
||||
fatalerror("Section '%s' is already on the stack\n", name);
|
||||
}
|
||||
|
||||
struct Section *sect = getSection(name, type, org, attribs, mod);
|
||||
|
||||
changeSection();
|
||||
@@ -332,18 +399,22 @@ void out_NewSection(char const *name, uint32_t type, uint32_t org,
|
||||
* Set the current section by name and type
|
||||
*/
|
||||
void out_SetLoadSection(char const *name, uint32_t type, uint32_t org,
|
||||
struct SectionSpec const *attribs)
|
||||
struct SectionSpec const *attribs,
|
||||
enum SectionModifier mod)
|
||||
{
|
||||
checkcodesection();
|
||||
|
||||
if (currentLoadSection)
|
||||
fatalerror("`LOAD` blocks cannot be nested\n");
|
||||
|
||||
struct Section *sect = getSection(name, type, org, attribs, false);
|
||||
if (sect_HasData(type))
|
||||
error("`LOAD` blocks cannot create a ROM section\n");
|
||||
|
||||
struct Section *sect = getSection(name, type, org, attribs, mod);
|
||||
|
||||
loadOffset = curOffset;
|
||||
curOffset = 0; /* curOffset -= loadOffset; */
|
||||
changeSection();
|
||||
loadOffset = curOffset - (mod == SECTION_UNION ? 0 : sect->size);
|
||||
curOffset -= loadOffset;
|
||||
currentLoadSection = sect;
|
||||
}
|
||||
|
||||
@@ -351,11 +422,11 @@ void out_EndLoadSection(void)
|
||||
{
|
||||
if (!currentLoadSection)
|
||||
error("Found `ENDL` outside of a `LOAD` block\n");
|
||||
currentLoadSection = NULL;
|
||||
|
||||
changeSection();
|
||||
curOffset += loadOffset;
|
||||
loadOffset = 0;
|
||||
currentLoadSection = NULL;
|
||||
}
|
||||
|
||||
struct Section *sect_GetSymbolSection(void)
|
||||
@@ -378,25 +449,26 @@ uint32_t sect_GetOutputOffset(void)
|
||||
|
||||
void sect_AlignPC(uint8_t alignment, uint16_t offset)
|
||||
{
|
||||
checksection();
|
||||
struct Section *sect = sect_GetSymbolSection();
|
||||
uint16_t alignSize = 1 << alignment; // Size of an aligned "block"
|
||||
|
||||
if (sect->org != -1) {
|
||||
if ((sym_GetPCValue() - offset) % (1 << alignment))
|
||||
error("Section's fixed address fails required alignment (PC = $%04"
|
||||
PRIx32 ")\n", sym_GetPCValue());
|
||||
if ((sym_GetPCValue() - offset) % alignSize)
|
||||
error("Section's fixed address fails required alignment (PC = $%04" PRIx32
|
||||
")\n", sym_GetPCValue());
|
||||
} else if (sect->align != 0) {
|
||||
if ((((sect->alignOfs + curOffset) % (1 << sect->align))
|
||||
- offset) % (1 << alignment)) {
|
||||
if ((((sect->alignOfs + curOffset) % (1 << sect->align)) - offset) % alignSize) {
|
||||
error("Section's alignment fails required alignment (offset from section start = $%04"
|
||||
PRIx32 ")\n", curOffset);
|
||||
} else if (alignment > sect->align) {
|
||||
sect->align = alignment;
|
||||
sect->alignOfs =
|
||||
(offset - curOffset) % (1 << alignment);
|
||||
sect->alignOfs = (offset - curOffset) % alignSize;
|
||||
}
|
||||
} else {
|
||||
sect->align = alignment;
|
||||
sect->alignOfs = offset;
|
||||
// We need `(sect->alignOfs + curOffset) % alignSize == offset
|
||||
sect->alignOfs = (offset - curOffset) % alignSize;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -429,10 +501,10 @@ static inline void writelong(uint32_t b)
|
||||
writebyte(b >> 24);
|
||||
}
|
||||
|
||||
static inline void createPatch(enum PatchType type,
|
||||
struct Expression const *expr)
|
||||
static inline void createPatch(enum PatchType type, struct Expression const *expr,
|
||||
uint32_t pcShift)
|
||||
{
|
||||
out_CreatePatch(type, expr, sect_GetOutputOffset());
|
||||
out_CreatePatch(type, expr, sect_GetOutputOffset(), pcShift);
|
||||
}
|
||||
|
||||
void sect_StartUnion(void)
|
||||
@@ -482,7 +554,7 @@ void sect_EndUnion(void)
|
||||
void sect_CheckUnionClosed(void)
|
||||
{
|
||||
if (unionStack)
|
||||
fatalerror("Unterminated UNION construct!\n");
|
||||
error("Unterminated UNION construct!\n");
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -505,6 +577,24 @@ void out_AbsByteGroup(uint8_t const *s, int32_t length)
|
||||
writebyte(*s++);
|
||||
}
|
||||
|
||||
void out_AbsWordGroup(uint8_t const *s, int32_t length)
|
||||
{
|
||||
checkcodesection();
|
||||
reserveSpace(length * 2);
|
||||
|
||||
while (length--)
|
||||
writeword(*s++);
|
||||
}
|
||||
|
||||
void out_AbsLongGroup(uint8_t const *s, int32_t length)
|
||||
{
|
||||
checkcodesection();
|
||||
reserveSpace(length * 4);
|
||||
|
||||
while (length--)
|
||||
writelong(*s++);
|
||||
}
|
||||
|
||||
/*
|
||||
* Skip this many bytes
|
||||
*/
|
||||
@@ -514,14 +604,15 @@ void out_Skip(int32_t skip, bool ds)
|
||||
reserveSpace(skip);
|
||||
|
||||
if (!ds && sect_HasData(pCurrentSection->type))
|
||||
warning(WARNING_EMPTY_DATA_DIRECTIVE, "db/dw/dl directive without data in ROM\n");
|
||||
warning(WARNING_EMPTY_DATA_DIRECTIVE, "%s directive without data in ROM\n",
|
||||
(skip == 4) ? "DL" : (skip == 2) ? "DW" : "DB");
|
||||
|
||||
if (!sect_HasData(pCurrentSection->type)) {
|
||||
growSection(skip);
|
||||
} else {
|
||||
checkcodesection();
|
||||
while (skip--)
|
||||
writebyte(CurrentOptions.fillchar);
|
||||
writebyte(fillByte);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -541,13 +632,13 @@ void out_String(char const *s)
|
||||
* Output a relocatable byte. Checking will be done to see if it
|
||||
* is an absolute value in disguise.
|
||||
*/
|
||||
void out_RelByte(struct Expression *expr)
|
||||
void out_RelByte(struct Expression *expr, uint32_t pcShift)
|
||||
{
|
||||
checkcodesection();
|
||||
reserveSpace(1);
|
||||
|
||||
if (!rpn_isKnown(expr)) {
|
||||
createPatch(PATCHTYPE_BYTE, expr);
|
||||
createPatch(PATCHTYPE_BYTE, expr, pcShift);
|
||||
writebyte(0);
|
||||
} else {
|
||||
writebyte(expr->nVal);
|
||||
@@ -559,33 +650,37 @@ void out_RelByte(struct Expression *expr)
|
||||
* Output several copies of a relocatable byte. Checking will be done to see if
|
||||
* it is an absolute value in disguise.
|
||||
*/
|
||||
void out_RelBytes(struct Expression *expr, uint32_t n)
|
||||
void out_RelBytes(uint32_t n, struct Expression *exprs, size_t size)
|
||||
{
|
||||
checkcodesection();
|
||||
reserveSpace(n);
|
||||
|
||||
while (n--) {
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
struct Expression *expr = &exprs[i % size];
|
||||
|
||||
if (!rpn_isKnown(expr)) {
|
||||
createPatch(PATCHTYPE_BYTE, expr);
|
||||
createPatch(PATCHTYPE_BYTE, expr, i);
|
||||
writebyte(0);
|
||||
} else {
|
||||
writebyte(expr->nVal);
|
||||
}
|
||||
}
|
||||
rpn_Free(expr);
|
||||
|
||||
for (size_t i = 0; i < size; i++)
|
||||
rpn_Free(&exprs[i]);
|
||||
}
|
||||
|
||||
/*
|
||||
* Output a relocatable word. Checking will be done to see if
|
||||
* it's an absolute value in disguise.
|
||||
*/
|
||||
void out_RelWord(struct Expression *expr)
|
||||
void out_RelWord(struct Expression *expr, uint32_t pcShift)
|
||||
{
|
||||
checkcodesection();
|
||||
reserveSpace(2);
|
||||
|
||||
if (!rpn_isKnown(expr)) {
|
||||
createPatch(PATCHTYPE_WORD, expr);
|
||||
createPatch(PATCHTYPE_WORD, expr, pcShift);
|
||||
writeword(0);
|
||||
} else {
|
||||
writeword(expr->nVal);
|
||||
@@ -597,13 +692,13 @@ void out_RelWord(struct Expression *expr)
|
||||
* Output a relocatable longword. Checking will be done to see if
|
||||
* is an absolute value in disguise.
|
||||
*/
|
||||
void out_RelLong(struct Expression *expr)
|
||||
void out_RelLong(struct Expression *expr, uint32_t pcShift)
|
||||
{
|
||||
checkcodesection();
|
||||
reserveSpace(2);
|
||||
|
||||
if (!rpn_isKnown(expr)) {
|
||||
createPatch(PATCHTYPE_LONG, expr);
|
||||
createPatch(PATCHTYPE_LONG, expr, pcShift);
|
||||
writelong(0);
|
||||
} else {
|
||||
writelong(expr->nVal);
|
||||
@@ -615,14 +710,14 @@ void out_RelLong(struct Expression *expr)
|
||||
* Output a PC-relative relocatable byte. Checking will be done to see if it
|
||||
* is an absolute value in disguise.
|
||||
*/
|
||||
void out_PCRelByte(struct Expression *expr)
|
||||
void out_PCRelByte(struct Expression *expr, uint32_t pcShift)
|
||||
{
|
||||
checkcodesection();
|
||||
reserveSpace(1);
|
||||
struct Symbol const *pc = sym_FindSymbol("@");
|
||||
struct Symbol const *pc = sym_GetPC();
|
||||
|
||||
if (!rpn_IsDiffConstant(expr, pc)) {
|
||||
createPatch(PATCHTYPE_JR, expr);
|
||||
createPatch(PATCHTYPE_JR, expr, pcShift);
|
||||
writebyte(0);
|
||||
} else {
|
||||
struct Symbol const *sym = rpn_SymbolOf(expr);
|
||||
@@ -662,14 +757,15 @@ void out_BinaryFile(char const *s, int32_t startPos)
|
||||
|
||||
if (fstk_FindFile(s, &fullPath, &size))
|
||||
f = fopen(fullPath, "rb");
|
||||
free(fullPath);
|
||||
|
||||
if (!f) {
|
||||
free(fullPath);
|
||||
if (oGeneratedMissingIncludes) {
|
||||
oFailedOnMissingInclude = true;
|
||||
return;
|
||||
}
|
||||
fatalerror("Error opening INCBIN file '%s': %s\n", s, strerror(errno));
|
||||
error("Error opening INCBIN file '%s': %s\n", s, strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t fsize = -1;
|
||||
@@ -679,8 +775,9 @@ void out_BinaryFile(char const *s, int32_t startPos)
|
||||
if (fseek(f, 0, SEEK_END) != -1) {
|
||||
fsize = ftell(f);
|
||||
|
||||
if (startPos >= fsize) {
|
||||
if (startPos > fsize) {
|
||||
error("Specified start position is greater than length of file\n");
|
||||
fclose(f);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -705,7 +802,6 @@ void out_BinaryFile(char const *s, int32_t startPos)
|
||||
error("Error reading INCBIN file '%s': %s\n", s, strerror(errno));
|
||||
|
||||
fclose(f);
|
||||
free(fullPath);
|
||||
}
|
||||
|
||||
void out_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length)
|
||||
@@ -735,7 +831,8 @@ void out_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length)
|
||||
oFailedOnMissingInclude = true;
|
||||
return;
|
||||
}
|
||||
fatalerror("Error opening INCBIN file '%s': %s\n", s, strerror(errno));
|
||||
error("Error opening INCBIN file '%s': %s\n", s, strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
checkcodesection();
|
||||
@@ -746,13 +843,16 @@ void out_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length)
|
||||
if (fseek(f, 0, SEEK_END) != -1) {
|
||||
fsize = ftell(f);
|
||||
|
||||
if (start_pos >= fsize) {
|
||||
if (start_pos > fsize) {
|
||||
error("Specified start position is greater than length of file\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if ((start_pos + length) > fsize)
|
||||
fatalerror("Specified range in INCBIN is out of bounds\n");
|
||||
if ((start_pos + length) > fsize) {
|
||||
error("Specified range in INCBIN is out of bounds (%" PRIu32 " + %" PRIu32
|
||||
" > %" PRIu32 ")\n", start_pos, length, fsize);
|
||||
return;
|
||||
}
|
||||
|
||||
fseek(f, start_pos, SEEK_SET);
|
||||
} else {
|
||||
|
||||
250
src/asm/symbol.c
250
src/asm/symbol.c
@@ -13,16 +13,16 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "asm/asm.h"
|
||||
#include "asm/fixpoint.h"
|
||||
#include "asm/fstack.h"
|
||||
#include "asm/macro.h"
|
||||
#include "asm/main.h"
|
||||
#include "asm/mymath.h"
|
||||
#include "asm/output.h"
|
||||
#include "asm/section.h"
|
||||
#include "asm/symbol.h"
|
||||
@@ -153,10 +153,12 @@ int32_t sym_GetValue(struct Symbol const *sym)
|
||||
|
||||
static void dumpFilename(struct Symbol const *sym)
|
||||
{
|
||||
if (!sym->src)
|
||||
fputs("<builtin>", stderr);
|
||||
else
|
||||
if (sym->src)
|
||||
fstk_Dump(sym->src, sym->fileLine);
|
||||
else if (sym->fileLine == 0)
|
||||
fputs("<command-line>", stderr);
|
||||
else
|
||||
fputs("<builtin>", stderr);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -165,7 +167,7 @@ static void dumpFilename(struct Symbol const *sym)
|
||||
static void setSymbolFilename(struct Symbol *sym)
|
||||
{
|
||||
sym->src = fstk_GetFileStack();
|
||||
sym->fileLine = lexer_GetLineNo();
|
||||
sym->fileLine = sym->src ? lexer_GetLineNo() : 0; // This is (NULL, 1) for built-ins
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -177,7 +179,7 @@ static void updateSymbolFilename(struct Symbol *sym)
|
||||
|
||||
setSymbolFilename(sym);
|
||||
/* If the old node was referenced, ensure the new one is */
|
||||
if (oldSrc->referenced && oldSrc->ID != -1)
|
||||
if (oldSrc && oldSrc->referenced && oldSrc->ID != -1)
|
||||
out_RegisterNode(sym->src);
|
||||
/* TODO: unref the old node, and use `out_ReplaceNode` instead if deleting it */
|
||||
}
|
||||
@@ -222,32 +224,55 @@ static void fullSymbolName(char *output, size_t outputSize,
|
||||
fatalerror("Symbol name is too long: '%s%s'\n", scopeName, localName);
|
||||
}
|
||||
|
||||
/*
|
||||
* Find a symbol by name and scope
|
||||
*/
|
||||
static struct Symbol *findsymbol(char const *s, char const *scope)
|
||||
static void assignStringSymbol(struct Symbol *sym, char const *value)
|
||||
{
|
||||
char fullname[MAXSYMLEN + 1];
|
||||
char *string = strdup(value);
|
||||
|
||||
if (s[0] == '.' && scope) {
|
||||
fullSymbolName(fullname, sizeof(fullname), s, scope);
|
||||
s = fullname;
|
||||
}
|
||||
if (string == NULL)
|
||||
fatalerror("No memory for string equate: %s\n", strerror(errno));
|
||||
|
||||
char const *separator = strchr(s, '.');
|
||||
|
||||
if (separator && strchr(separator + 1, '.'))
|
||||
fatalerror("'%s' is a nonsensical reference to a nested local symbol\n", s);
|
||||
|
||||
return hash_GetElement(symbols, s);
|
||||
sym->type = SYM_EQUS;
|
||||
/* TODO: use other fields */
|
||||
sym->macro = string;
|
||||
sym->macroSize = strlen(string);
|
||||
}
|
||||
|
||||
/*
|
||||
* Find a symbol by name, with automatically determined scope
|
||||
*/
|
||||
struct Symbol *sym_FindSymbol(char const *symName)
|
||||
struct Symbol *sym_FindExactSymbol(char const *name)
|
||||
{
|
||||
return findsymbol(symName, symName[0] == '.' ? labelScope : NULL);
|
||||
return hash_GetElement(symbols, name);
|
||||
}
|
||||
|
||||
struct Symbol *sym_FindUnscopedSymbol(char const *name)
|
||||
{
|
||||
if (strchr(name, '.')) {
|
||||
error("Expected non-scoped symbol name, not \"%s\"\n", name);
|
||||
return NULL;
|
||||
}
|
||||
return sym_FindExactSymbol(name);
|
||||
}
|
||||
|
||||
struct Symbol *sym_FindScopedSymbol(char const *name)
|
||||
{
|
||||
char const *dotPtr = strchr(name, '.');
|
||||
|
||||
if (dotPtr) {
|
||||
if (strchr(dotPtr + 1, '.'))
|
||||
fatalerror("'%s' is a nonsensical reference to a nested local symbol\n",
|
||||
name);
|
||||
/* If auto-scoped local label, expand the name */
|
||||
if (dotPtr == name) { /* Meaning, the name begins with the dot */
|
||||
char fullname[MAXSYMLEN + 1];
|
||||
|
||||
fullSymbolName(fullname, sizeof(fullname), name, labelScope);
|
||||
return sym_FindExactSymbol(fullname);
|
||||
}
|
||||
}
|
||||
return sym_FindExactSymbol(name);
|
||||
}
|
||||
|
||||
struct Symbol const *sym_GetPC(void)
|
||||
{
|
||||
return PCSymbol;
|
||||
}
|
||||
|
||||
static inline bool isReferenced(struct Symbol const *sym)
|
||||
@@ -260,7 +285,7 @@ static inline bool isReferenced(struct Symbol const *sym)
|
||||
*/
|
||||
void sym_Purge(char const *symName)
|
||||
{
|
||||
struct Symbol *symbol = sym_FindSymbol(symName);
|
||||
struct Symbol *symbol = sym_FindScopedSymbol(symName);
|
||||
|
||||
if (!symbol) {
|
||||
error("'%s' not defined\n", symName);
|
||||
@@ -273,6 +298,11 @@ void sym_Purge(char const *symName)
|
||||
if (symbol->name == labelScope)
|
||||
labelScope = NULL;
|
||||
|
||||
/*
|
||||
* FIXME: this leaks symbol->macro for SYM_EQUS and SYM_MACRO, but this can't
|
||||
* free(symbol->macro) because the expansion may be purging itself.
|
||||
*/
|
||||
|
||||
hash_RemoveElement(symbols, symbol->name);
|
||||
/* TODO: ideally, also unref the file stack nodes */
|
||||
free(symbol);
|
||||
@@ -312,7 +342,7 @@ uint32_t sym_GetConstantSymValue(struct Symbol const *sym)
|
||||
*/
|
||||
uint32_t sym_GetConstantValue(char const *s)
|
||||
{
|
||||
struct Symbol const *sym = sym_FindSymbol(s);
|
||||
struct Symbol const *sym = sym_FindScopedSymbol(s);
|
||||
|
||||
if (sym == NULL)
|
||||
error("'%s' not defined\n", s);
|
||||
@@ -322,23 +352,6 @@ uint32_t sym_GetConstantValue(char const *s)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return a defined symbols value... aborts if not defined yet
|
||||
*/
|
||||
uint32_t sym_GetDefinedValue(char const *s)
|
||||
{
|
||||
struct Symbol const *sym = sym_FindSymbol(s);
|
||||
|
||||
if (sym == NULL || !sym_IsDefined(sym))
|
||||
error("'%s' not defined\n", s);
|
||||
else if (!sym_IsNumeric(sym))
|
||||
error("'%s' is a macro or string symbol\n", s);
|
||||
else
|
||||
return sym_GetValue(sym);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char const *sym_GetCurrentSymbolScope(void)
|
||||
{
|
||||
return labelScope;
|
||||
@@ -353,17 +366,25 @@ void sym_SetCurrentSymbolScope(char const *newScope)
|
||||
* Create a symbol that will be non-relocatable and ensure that it
|
||||
* hasn't already been defined or referenced in a context that would
|
||||
* require that it be relocatable
|
||||
* @param symbolName The name of the symbol to create
|
||||
* @param numeric If false, the symbol may not have been referenced earlier
|
||||
*/
|
||||
static struct Symbol *createNonrelocSymbol(char const *symbolName)
|
||||
static struct Symbol *createNonrelocSymbol(char const *symbolName, bool numeric)
|
||||
{
|
||||
struct Symbol *symbol = findsymbol(symbolName, NULL);
|
||||
struct Symbol *symbol = sym_FindExactSymbol(symbolName);
|
||||
|
||||
if (!symbol)
|
||||
if (!symbol) {
|
||||
symbol = createsymbol(symbolName);
|
||||
else if (sym_IsDefined(symbol)) {
|
||||
} else if (sym_IsDefined(symbol)) {
|
||||
error("'%s' already defined at ", symbolName);
|
||||
dumpFilename(symbol);
|
||||
putc('\n', stderr);
|
||||
} else if (!numeric) {
|
||||
// The symbol has already been referenced, but it's not allowed
|
||||
error("'%s' already referenced at ", symbolName);
|
||||
dumpFilename(symbol);
|
||||
putc('\n', stderr);
|
||||
return NULL; // Don't allow overriding the symbol, that'd be bad!
|
||||
}
|
||||
|
||||
return symbol;
|
||||
@@ -374,7 +395,10 @@ static struct Symbol *createNonrelocSymbol(char const *symbolName)
|
||||
*/
|
||||
struct Symbol *sym_AddEqu(char const *symName, int32_t value)
|
||||
{
|
||||
struct Symbol *sym = createNonrelocSymbol(symName);
|
||||
struct Symbol *sym = createNonrelocSymbol(symName, true);
|
||||
|
||||
if (!sym)
|
||||
return NULL;
|
||||
|
||||
sym->type = SYM_EQU;
|
||||
sym->value = value;
|
||||
@@ -396,18 +420,33 @@ struct Symbol *sym_AddEqu(char const *symName, int32_t value)
|
||||
*/
|
||||
struct Symbol *sym_AddString(char const *symName, char const *value)
|
||||
{
|
||||
struct Symbol *sym = createNonrelocSymbol(symName);
|
||||
size_t len = strlen(value);
|
||||
char *string = malloc(len + 1);
|
||||
struct Symbol *sym = createNonrelocSymbol(symName, false);
|
||||
|
||||
if (string == NULL)
|
||||
fatalerror("No memory for string equate: %s\n", strerror(errno));
|
||||
strcpy(string, value);
|
||||
if (!sym)
|
||||
return NULL;
|
||||
|
||||
sym->type = SYM_EQUS;
|
||||
/* TODO: use other fields */
|
||||
sym->macroSize = len;
|
||||
sym->macro = string;
|
||||
assignStringSymbol(sym, value);
|
||||
return sym;
|
||||
}
|
||||
|
||||
struct Symbol *sym_RedefString(char const *symName, char const *value)
|
||||
{
|
||||
struct Symbol *sym = sym_FindExactSymbol(symName);
|
||||
|
||||
if (!sym) {
|
||||
sym = createsymbol(symName);
|
||||
} else if (sym->type != SYM_EQUS) {
|
||||
error("'%s' already defined as non-EQUS at ", symName);
|
||||
dumpFilename(sym);
|
||||
putc('\n', stderr);
|
||||
}
|
||||
|
||||
/*
|
||||
* FIXME: this leaks the previous sym->macro value, but this can't
|
||||
* free(sym->macro) because the expansion may be redefining itself.
|
||||
*/
|
||||
|
||||
assignStringSymbol(sym, value);
|
||||
|
||||
return sym;
|
||||
}
|
||||
@@ -417,7 +456,7 @@ struct Symbol *sym_AddString(char const *symName, char const *value)
|
||||
*/
|
||||
struct Symbol *sym_AddSet(char const *symName, int32_t value)
|
||||
{
|
||||
struct Symbol *sym = findsymbol(symName, NULL);
|
||||
struct Symbol *sym = sym_FindExactSymbol(symName);
|
||||
|
||||
if (sym == NULL) {
|
||||
sym = createsymbol(symName);
|
||||
@@ -426,8 +465,8 @@ struct Symbol *sym_AddSet(char const *symName, int32_t value)
|
||||
symName, sym->type == SYM_LABEL ? "label" : "constant");
|
||||
dumpFilename(sym);
|
||||
putc('\n', stderr);
|
||||
return sym;
|
||||
} else {
|
||||
/* TODO: can the scope be incorrect when taking over refs? */
|
||||
updateSymbolFilename(sym);
|
||||
}
|
||||
|
||||
@@ -445,7 +484,7 @@ struct Symbol *sym_AddSet(char const *symName, int32_t value)
|
||||
static struct Symbol *addLabel(char const *name)
|
||||
{
|
||||
assert(name[0] != '.'); /* The symbol name must have been expanded prior */
|
||||
struct Symbol *sym = findsymbol(name, NULL); /* Due to this, don't look for expansions */
|
||||
struct Symbol *sym = sym_FindExactSymbol(name);
|
||||
|
||||
if (!sym) {
|
||||
sym = createsymbol(name);
|
||||
@@ -499,8 +538,10 @@ struct Symbol *sym_AddLocalLabel(char const *name)
|
||||
* Check that `labelScope[i]` ended the check, guaranteeing that `name` is at least
|
||||
* as long, and then that this was the entirety of the `Parent` part of `name`.
|
||||
*/
|
||||
if (labelScope[i] != '\0' || name[i] != '.')
|
||||
error("Not currently in the scope of '%.*s'\n", parentLen, name);
|
||||
if (labelScope[i] != '\0' || name[i] != '.') {
|
||||
assert(parentLen <= INT_MAX);
|
||||
error("Not currently in the scope of '%.*s'\n", (int)parentLen, name);
|
||||
}
|
||||
if (strchr(&name[parentLen + 1], '.')) /* There will at least be a terminator */
|
||||
fatalerror("'%s' is a nonsensical reference to a nested local label\n",
|
||||
name);
|
||||
@@ -522,12 +563,61 @@ struct Symbol *sym_AddLabel(char const *name)
|
||||
return sym;
|
||||
}
|
||||
|
||||
static uint32_t anonLabelID;
|
||||
|
||||
/*
|
||||
* Add an anonymous label
|
||||
*/
|
||||
struct Symbol *sym_AddAnonLabel(void)
|
||||
{
|
||||
if (anonLabelID == UINT32_MAX) {
|
||||
error("Only %" PRIu32 " anonymous labels can be created!", anonLabelID);
|
||||
return NULL;
|
||||
}
|
||||
char name[MAXSYMLEN + 1];
|
||||
|
||||
sym_WriteAnonLabelName(name, 0, true); // The direction is important!!
|
||||
anonLabelID++;
|
||||
return addLabel(name);
|
||||
}
|
||||
|
||||
/*
|
||||
* Write an anonymous label's name to a buffer
|
||||
*/
|
||||
void sym_WriteAnonLabelName(char buf[MIN_NB_ELMS(MAXSYMLEN + 1)], uint32_t ofs, bool neg)
|
||||
{
|
||||
uint32_t id = 0;
|
||||
|
||||
if (neg) {
|
||||
if (ofs > anonLabelID)
|
||||
error("Reference to anonymous label %" PRIu32 " before, when only %" PRIu32
|
||||
" ha%s been created so far\n",
|
||||
ofs, anonLabelID, anonLabelID == 1 ? "s" : "ve");
|
||||
else
|
||||
id = anonLabelID - ofs;
|
||||
} else {
|
||||
ofs--; // We're referencing symbols that haven't been created yet...
|
||||
if (ofs > UINT32_MAX - anonLabelID)
|
||||
error("Reference to anonymous label %" PRIu32 " after, when only %" PRIu32
|
||||
" may still be created\n", ofs + 1, UINT32_MAX - anonLabelID);
|
||||
else
|
||||
id = anonLabelID + ofs;
|
||||
}
|
||||
|
||||
sprintf(buf, "!%u", id);
|
||||
}
|
||||
|
||||
/*
|
||||
* Export a symbol
|
||||
*/
|
||||
void sym_Export(char const *symName)
|
||||
{
|
||||
struct Symbol *sym = sym_FindSymbol(symName);
|
||||
if (symName[0] == '!') {
|
||||
error("Anonymous labels cannot be exported\n");
|
||||
return;
|
||||
}
|
||||
|
||||
struct Symbol *sym = sym_FindScopedSymbol(symName);
|
||||
|
||||
/* If the symbol doesn't exist, create a ref that can be purged */
|
||||
if (!sym)
|
||||
@@ -540,7 +630,10 @@ void sym_Export(char const *symName)
|
||||
*/
|
||||
struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char *body, size_t size)
|
||||
{
|
||||
struct Symbol *sym = createNonrelocSymbol(symName);
|
||||
struct Symbol *sym = createNonrelocSymbol(symName, false);
|
||||
|
||||
if (!sym)
|
||||
return NULL;
|
||||
|
||||
sym->type = SYM_MACRO;
|
||||
sym->macroSize = size;
|
||||
@@ -561,7 +654,7 @@ struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char *body,
|
||||
*/
|
||||
struct Symbol *sym_Ref(char const *symName)
|
||||
{
|
||||
struct Symbol *nsym = sym_FindSymbol(symName);
|
||||
struct Symbol *nsym = sym_FindScopedSymbol(symName);
|
||||
|
||||
if (nsym == NULL) {
|
||||
char fullname[MAXSYMLEN + 1];
|
||||
@@ -606,14 +699,14 @@ static inline struct Symbol *createBuiltinSymbol(char const *name)
|
||||
sym->isBuiltin = true;
|
||||
sym->hasCallback = true;
|
||||
sym->src = NULL;
|
||||
sym->fileLine = 0;
|
||||
sym->fileLine = 1; // This is 0 for CLI-defined symbols
|
||||
return sym;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the symboltable
|
||||
*/
|
||||
void sym_Init(void)
|
||||
void sym_Init(time_t now)
|
||||
{
|
||||
PCSymbol = createBuiltinSymbol("@");
|
||||
struct Symbol *_NARGSymbol = createBuiltinSymbol("_NARG");
|
||||
@@ -634,8 +727,9 @@ void sym_Init(void)
|
||||
sym_AddEqu("__RGBDS_MAJOR__", PACKAGE_VERSION_MAJOR)->isBuiltin = true;
|
||||
sym_AddEqu("__RGBDS_MINOR__", PACKAGE_VERSION_MINOR)->isBuiltin = true;
|
||||
sym_AddEqu("__RGBDS_PATCH__", PACKAGE_VERSION_PATCH)->isBuiltin = true;
|
||||
|
||||
time_t now = time(NULL);
|
||||
#ifdef PACKAGE_VERSION_RC
|
||||
sym_AddEqu("__RGBDS_RC__", PACKAGE_VERSION_RC)->isBuiltin = true;
|
||||
#endif
|
||||
|
||||
if (now == (time_t)-1) {
|
||||
warn("Couldn't determine current time");
|
||||
@@ -678,6 +772,14 @@ void sym_Init(void)
|
||||
#undef addString
|
||||
|
||||
labelScope = NULL;
|
||||
anonLabelID = 0;
|
||||
|
||||
math_DefinePI();
|
||||
/* _PI is deprecated */
|
||||
struct Symbol *_PISymbol = createBuiltinSymbol("_PI");
|
||||
|
||||
_PISymbol->type = SYM_EQU;
|
||||
_PISymbol->src = NULL;
|
||||
_PISymbol->fileLine = 0;
|
||||
_PISymbol->hasCallback = true;
|
||||
_PISymbol->numCallback = fix_Callback_PI;
|
||||
}
|
||||
|
||||
@@ -16,7 +16,9 @@
|
||||
#include "extern/utf8decoder.h"
|
||||
|
||||
/*
|
||||
* Calculate the hash value for a string
|
||||
* Calculate the hash value for a string.
|
||||
* Uses the djb2 algorithm (xor version).
|
||||
* http://www.cse.yorku.ca/~oz/hash.html
|
||||
*/
|
||||
uint32_t calchash(const char *s)
|
||||
{
|
||||
@@ -69,8 +71,8 @@ size_t readUTF8Char(uint8_t *dest, char const *src)
|
||||
size_t i = 0;
|
||||
|
||||
for (;;) {
|
||||
if (decode(&state, &codep, (uint8_t)src[i]) == 1)
|
||||
fatalerror("invalid UTF-8 character\n");
|
||||
if (decode(&state, &codep, src[i]) == 1)
|
||||
return 0;
|
||||
|
||||
dest[i] = src[i];
|
||||
i++;
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "asm/fstack.h"
|
||||
@@ -33,9 +34,12 @@ static enum WarningState const defaultWarnings[NB_WARNINGS] = {
|
||||
[WARNING_CHARMAP_REDEF] = WARNING_DISABLED,
|
||||
[WARNING_DIV] = WARNING_DISABLED,
|
||||
[WARNING_EMPTY_DATA_DIRECTIVE] = WARNING_DISABLED,
|
||||
[WARNING_EMPTY_ENTRY] = WARNING_DISABLED,
|
||||
[WARNING_EMPTY_MACRO_ARG] = WARNING_DISABLED,
|
||||
[WARNING_EMPTY_STRRPL] = WARNING_DISABLED,
|
||||
[WARNING_LARGE_CONSTANT] = WARNING_DISABLED,
|
||||
[WARNING_LONG_STR] = WARNING_DISABLED,
|
||||
[WARNING_MACRO_SHIFT] = WARNING_DISABLED,
|
||||
[WARNING_NESTED_COMMENT] = WARNING_ENABLED,
|
||||
[WARNING_OBSOLETE] = WARNING_ENABLED,
|
||||
[WARNING_SHIFT] = WARNING_DISABLED,
|
||||
[WARNING_SHIFT_AMOUNT] = WARNING_DISABLED,
|
||||
@@ -72,9 +76,12 @@ static char const *warningFlags[NB_WARNINGS_ALL] = {
|
||||
"charmap-redef",
|
||||
"div",
|
||||
"empty-data-directive",
|
||||
"empty-entry",
|
||||
"empty-macro-arg",
|
||||
"empty-strrpl",
|
||||
"large-constant",
|
||||
"long-string",
|
||||
"macro-shift",
|
||||
"nested-comment",
|
||||
"obsolete",
|
||||
"shift",
|
||||
"shift-amount",
|
||||
@@ -96,6 +103,7 @@ static uint8_t const _wallCommands[] = {
|
||||
WARNING_BUILTIN_ARG,
|
||||
WARNING_CHARMAP_REDEF,
|
||||
WARNING_EMPTY_DATA_DIRECTIVE,
|
||||
WARNING_EMPTY_STRRPL,
|
||||
WARNING_LARGE_CONSTANT,
|
||||
WARNING_LONG_STR,
|
||||
META_WARNING_DONE
|
||||
@@ -103,7 +111,9 @@ static uint8_t const _wallCommands[] = {
|
||||
|
||||
/* Warnings that are less likely to indicate an error */
|
||||
static uint8_t const _wextraCommands[] = {
|
||||
WARNING_EMPTY_ENTRY,
|
||||
WARNING_EMPTY_MACRO_ARG,
|
||||
WARNING_MACRO_SHIFT,
|
||||
WARNING_NESTED_COMMENT,
|
||||
META_WARNING_DONE
|
||||
};
|
||||
|
||||
@@ -112,9 +122,12 @@ static uint8_t const _weverythingCommands[] = {
|
||||
WARNING_BUILTIN_ARG,
|
||||
WARNING_DIV,
|
||||
WARNING_EMPTY_DATA_DIRECTIVE,
|
||||
WARNING_EMPTY_ENTRY,
|
||||
WARNING_EMPTY_MACRO_ARG,
|
||||
WARNING_EMPTY_STRRPL,
|
||||
WARNING_LARGE_CONSTANT,
|
||||
WARNING_LONG_STR,
|
||||
WARNING_MACRO_SHIFT,
|
||||
WARNING_NESTED_COMMENT,
|
||||
WARNING_OBSOLETE,
|
||||
WARNING_SHIFT,
|
||||
WARNING_SHIFT_AMOUNT,
|
||||
@@ -218,7 +231,7 @@ void error(const char *fmt, ...)
|
||||
nbErrors++;
|
||||
}
|
||||
|
||||
noreturn_ void fatalerror(const char *fmt, ...)
|
||||
_Noreturn void fatalerror(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
@@ -246,7 +259,7 @@ void warning(enum WarningID id, char const *fmt, ...)
|
||||
return;
|
||||
|
||||
case WARNING_DEFAULT:
|
||||
trap_;
|
||||
unreachable_();
|
||||
/* Not reached */
|
||||
|
||||
case WARNING_ENABLED:
|
||||
|
||||
7
src/check_bison_ver.sh
Executable file
7
src/check_bison_ver.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
bison -V | awk -v major="$1" -v minor="$2" '
|
||||
/^bison.*[0-9]+(\.[0-9]+)(\.[0-9]+)?$/ {
|
||||
match($0, /[0-9]+(\.[0-9]+)(\.[0-9]+)?$/);
|
||||
split(substr($0, RSTART), ver, ".");
|
||||
if (ver[1] == major && ver[2] >= minor) { exit 0 } else { exit 1 }
|
||||
}'
|
||||
8
src/extern/err.c
vendored
8
src/extern/err.c
vendored
@@ -34,7 +34,7 @@ void rgbds_vwarnx(const char *fmt, va_list ap)
|
||||
putc('\n', stderr);
|
||||
}
|
||||
|
||||
noreturn_ void rgbds_verr(int status, const char *fmt, va_list ap)
|
||||
_Noreturn void rgbds_verr(int status, const char *fmt, va_list ap)
|
||||
{
|
||||
fprintf(stderr, "error: ");
|
||||
if (fmt) {
|
||||
@@ -46,7 +46,7 @@ noreturn_ void rgbds_verr(int status, const char *fmt, va_list ap)
|
||||
exit(status);
|
||||
}
|
||||
|
||||
noreturn_ void rgbds_verrx(int status, const char *fmt, va_list ap)
|
||||
_Noreturn void rgbds_verrx(int status, const char *fmt, va_list ap)
|
||||
{
|
||||
fprintf(stderr, "error");
|
||||
if (fmt) {
|
||||
@@ -75,7 +75,7 @@ void rgbds_warnx(const char *fmt, ...)
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
noreturn_ void rgbds_err(int status, const char *fmt, ...)
|
||||
_Noreturn void rgbds_err(int status, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
@@ -84,7 +84,7 @@ noreturn_ void rgbds_err(int status, const char *fmt, ...)
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
noreturn_ void rgbds_errx(int status, const char *fmt, ...)
|
||||
_Noreturn void rgbds_errx(int status, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
|
||||
203
src/extern/getopt.c
vendored
203
src/extern/getopt.c
vendored
@@ -26,21 +26,16 @@
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#ifndef _MSC_VER
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#include "extern/getopt.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
char *optarg;
|
||||
int optind=1, opterr=1, optopt;
|
||||
#endif
|
||||
int optreset=0;
|
||||
static int optpos;
|
||||
char *musl_optarg;
|
||||
int musl_optind = 1, musl_opterr = 1, musl_optopt;
|
||||
int musl_optreset = 0;
|
||||
static int musl_optpos;
|
||||
|
||||
static void musl_getopt_msg(const char *a, const char *b, const char *c, size_t l)
|
||||
{
|
||||
@@ -52,7 +47,6 @@ static void musl_getopt_msg(const char *a, const char *b, const char *c, size_t
|
||||
putc('\n', f);
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
static int getopt(int argc, char *argv[], const char *optstring)
|
||||
{
|
||||
int i;
|
||||
@@ -60,40 +54,42 @@ static int getopt(int argc, char *argv[], const char *optstring)
|
||||
int k, l;
|
||||
char *optchar;
|
||||
|
||||
if (!optind || optreset) {
|
||||
optreset = 0;
|
||||
optpos = 0;
|
||||
optind = 1;
|
||||
if (!musl_optind || musl_optreset) {
|
||||
musl_optreset = 0;
|
||||
musl_optpos = 0;
|
||||
musl_optind = 1;
|
||||
}
|
||||
|
||||
if (optind >= argc || !argv[optind])
|
||||
if (musl_optind >= argc || !argv[musl_optind])
|
||||
return -1;
|
||||
|
||||
if (argv[optind][0] != '-') {
|
||||
if (argv[musl_optind][0] != '-') {
|
||||
if (optstring[0] == '-') {
|
||||
optarg = argv[optind++];
|
||||
musl_optarg = argv[musl_optind++];
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!argv[optind][1])
|
||||
if (!argv[musl_optind][1])
|
||||
return -1;
|
||||
|
||||
if (argv[optind][1] == '-' && !argv[optind][2])
|
||||
return optind++, -1;
|
||||
if (argv[musl_optind][1] == '-' && !argv[musl_optind][2])
|
||||
return musl_optind++, -1;
|
||||
|
||||
if (!optpos) optpos++;
|
||||
if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) {
|
||||
if (!musl_optpos)
|
||||
musl_optpos++;
|
||||
k = mbtowc(&c, argv[musl_optind] + musl_optpos, MB_LEN_MAX);
|
||||
if (k < 0) {
|
||||
k = 1;
|
||||
c = 0xfffd; /* replacement char */
|
||||
}
|
||||
optchar = argv[optind]+optpos;
|
||||
optpos += k;
|
||||
optchar = argv[musl_optind] + musl_optpos;
|
||||
musl_optpos += k;
|
||||
|
||||
if (!argv[optind][optpos]) {
|
||||
optind++;
|
||||
optpos = 0;
|
||||
if (!argv[musl_optind][musl_optpos]) {
|
||||
musl_optind++;
|
||||
musl_optpos = 0;
|
||||
}
|
||||
|
||||
if (optstring[0] == '-' || optstring[0] == '+')
|
||||
@@ -103,39 +99,43 @@ static int getopt(int argc, char *argv[], const char *optstring)
|
||||
d = 0;
|
||||
do {
|
||||
l = mbtowc(&d, optstring+i, MB_LEN_MAX);
|
||||
if (l>0) i+=l; else i++;
|
||||
if (l > 0)
|
||||
i += l;
|
||||
else
|
||||
i++;
|
||||
} while (l && d != c);
|
||||
|
||||
if (d != c || c == ':') {
|
||||
optopt = c;
|
||||
if (optstring[0] != ':' && opterr)
|
||||
musl_optopt = c;
|
||||
if (optstring[0] != ':' && musl_opterr)
|
||||
musl_getopt_msg(argv[0], ": unrecognized option: ", optchar, k);
|
||||
return '?';
|
||||
}
|
||||
if (optstring[i] == ':') {
|
||||
optarg = 0;
|
||||
if (optstring[i+1] != ':' || optpos) {
|
||||
optarg = argv[optind++] + optpos;
|
||||
optpos = 0;
|
||||
musl_optarg = 0;
|
||||
if (optstring[i + 1] != ':' || musl_optpos) {
|
||||
musl_optarg = argv[musl_optind++] + musl_optpos;
|
||||
musl_optpos = 0;
|
||||
}
|
||||
if (optind > argc) {
|
||||
optopt = c;
|
||||
if (optstring[0] == ':') return ':';
|
||||
if (opterr) musl_getopt_msg(argv[0],
|
||||
": option requires an argument: ",
|
||||
optchar, k);
|
||||
if (musl_optind > argc) {
|
||||
musl_optopt = c;
|
||||
if (optstring[0] == ':')
|
||||
return ':';
|
||||
if (musl_opterr)
|
||||
musl_getopt_msg(argv[0], ": option requires an argument: ",
|
||||
optchar, k);
|
||||
return '?';
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
#endif /* _MSC_VER */
|
||||
|
||||
static void permute(char **argv, int dest, int src)
|
||||
{
|
||||
char *tmp = argv[src];
|
||||
int i;
|
||||
for (i=src; i>dest; i--)
|
||||
|
||||
for (i = src; i > dest; i--)
|
||||
argv[i] = argv[i-1];
|
||||
argv[dest] = tmp;
|
||||
}
|
||||
@@ -145,49 +145,61 @@ static int musl_getopt_long_core(int argc, char **argv, const char *optstring, c
|
||||
static int musl_getopt_long(int argc, char **argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
|
||||
{
|
||||
int ret, skipped, resumed;
|
||||
if (!optind || optreset) {
|
||||
optreset = 0;
|
||||
optpos = 0;
|
||||
optind = 1;
|
||||
|
||||
if (!musl_optind || musl_optreset) {
|
||||
musl_optreset = 0;
|
||||
musl_optpos = 0;
|
||||
musl_optind = 1;
|
||||
}
|
||||
if (optind >= argc || !argv[optind]) return -1;
|
||||
skipped = optind;
|
||||
|
||||
if (musl_optind >= argc || !argv[musl_optind])
|
||||
return -1;
|
||||
|
||||
skipped = musl_optind;
|
||||
if (optstring[0] != '+' && optstring[0] != '-') {
|
||||
int i;
|
||||
for (i=optind; ; i++) {
|
||||
if (i >= argc || !argv[i]) return -1;
|
||||
if (argv[i][0] == '-' && argv[i][1]) break;
|
||||
for (i = musl_optind; ; i++) {
|
||||
if (i >= argc || !argv[i])
|
||||
return -1;
|
||||
if (argv[i][0] == '-' && argv[i][1])
|
||||
break;
|
||||
}
|
||||
optind = i;
|
||||
musl_optind = i;
|
||||
}
|
||||
resumed = optind;
|
||||
resumed = musl_optind;
|
||||
ret = musl_getopt_long_core(argc, argv, optstring, longopts, idx, longonly);
|
||||
if (resumed > skipped) {
|
||||
int i, cnt = optind-resumed;
|
||||
for (i=0; i<cnt; i++)
|
||||
permute(argv, skipped, optind-1);
|
||||
optind = skipped + cnt;
|
||||
int i, cnt = musl_optind - resumed;
|
||||
|
||||
for (i = 0; i < cnt; i++)
|
||||
permute(argv, skipped, musl_optind - 1);
|
||||
musl_optind = skipped + cnt;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int musl_getopt_long_core(int argc, char **argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
|
||||
{
|
||||
optarg = 0;
|
||||
if (longopts && argv[optind][0] == '-' &&
|
||||
((longonly && argv[optind][1] && argv[optind][1] != '-') ||
|
||||
(argv[optind][1] == '-' && argv[optind][2])))
|
||||
{
|
||||
int colon = optstring[optstring[0]=='+'||optstring[0]=='-']==':';
|
||||
musl_optarg = 0;
|
||||
if (longopts && argv[musl_optind][0] == '-' &&
|
||||
((longonly && argv[musl_optind][1] && argv[musl_optind][1] != '-') ||
|
||||
(argv[musl_optind][1] == '-' && argv[musl_optind][2]))) {
|
||||
int colon = optstring[optstring[0] == '+' || optstring[0] == '-'] == ':';
|
||||
int i, cnt, match = 0;
|
||||
char *arg = 0, *opt, *start = argv[optind]+1;
|
||||
for (cnt=i=0; longopts[i].name; i++) {
|
||||
char *arg = 0, *opt, *start = argv[musl_optind] + 1;
|
||||
|
||||
for (cnt = i = 0; longopts[i].name; i++) {
|
||||
const char *name = longopts[i].name;
|
||||
|
||||
opt = start;
|
||||
if (*opt == '-') opt++;
|
||||
while (*opt && *opt != '=' && *opt == *name)
|
||||
name++, opt++;
|
||||
if (*opt && *opt != '=') continue;
|
||||
if (*opt == '-')
|
||||
opt++;
|
||||
while (*opt && *opt != '=' && *opt == *name) {
|
||||
name++;
|
||||
opt++;
|
||||
}
|
||||
if (*opt && *opt != '=')
|
||||
continue;
|
||||
arg = opt;
|
||||
match = i;
|
||||
if (!*name) {
|
||||
@@ -196,25 +208,28 @@ static int musl_getopt_long_core(int argc, char **argv, const char *optstring, c
|
||||
}
|
||||
cnt++;
|
||||
}
|
||||
if (cnt==1 && longonly && arg-start == mblen(start, MB_LEN_MAX)) {
|
||||
int l = arg-start;
|
||||
for (i=0; optstring[i]; i++) {
|
||||
int j;
|
||||
for (j=0; j<l && start[j]==optstring[i+j]; j++);
|
||||
if (j==l) {
|
||||
if (cnt == 1 && longonly && arg - start == mblen(start, MB_LEN_MAX)) {
|
||||
int l = arg - start;
|
||||
|
||||
for (i = 0; optstring[i]; i++) {
|
||||
int j = 0;
|
||||
|
||||
while (j < l && start[j] == optstring[i + j])
|
||||
j++;
|
||||
if (j == l) {
|
||||
cnt++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cnt==1) {
|
||||
if (cnt == 1) {
|
||||
i = match;
|
||||
opt = arg;
|
||||
optind++;
|
||||
musl_optind++;
|
||||
if (*opt == '=') {
|
||||
if (!longopts[i].has_arg) {
|
||||
optopt = longopts[i].val;
|
||||
if (colon || !opterr)
|
||||
musl_optopt = longopts[i].val;
|
||||
if (colon || !musl_opterr)
|
||||
return '?';
|
||||
musl_getopt_msg(argv[0],
|
||||
": option does not take an argument: ",
|
||||
@@ -222,36 +237,40 @@ static int musl_getopt_long_core(int argc, char **argv, const char *optstring, c
|
||||
strlen(longopts[i].name));
|
||||
return '?';
|
||||
}
|
||||
optarg = opt+1;
|
||||
musl_optarg = opt + 1;
|
||||
} else if (longopts[i].has_arg == required_argument) {
|
||||
if (!(optarg = argv[optind])) {
|
||||
optopt = longopts[i].val;
|
||||
if (colon) return ':';
|
||||
if (!opterr) return '?';
|
||||
musl_optarg = argv[musl_optind];
|
||||
if (!musl_optarg) {
|
||||
musl_optopt = longopts[i].val;
|
||||
if (colon)
|
||||
return ':';
|
||||
if (!musl_opterr)
|
||||
return '?';
|
||||
musl_getopt_msg(argv[0],
|
||||
": option requires an argument: ",
|
||||
longopts[i].name,
|
||||
strlen(longopts[i].name));
|
||||
return '?';
|
||||
}
|
||||
optind++;
|
||||
musl_optind++;
|
||||
}
|
||||
if (idx) *idx = i;
|
||||
if (idx)
|
||||
*idx = i;
|
||||
if (longopts[i].flag) {
|
||||
*longopts[i].flag = longopts[i].val;
|
||||
return 0;
|
||||
}
|
||||
return longopts[i].val;
|
||||
}
|
||||
if (argv[optind][1] == '-') {
|
||||
optopt = 0;
|
||||
if (!colon && opterr)
|
||||
if (argv[musl_optind][1] == '-') {
|
||||
musl_optopt = 0;
|
||||
if (!colon && musl_opterr)
|
||||
musl_getopt_msg(argv[0], cnt ?
|
||||
": option is ambiguous: " :
|
||||
": unrecognized option: ",
|
||||
argv[optind]+2,
|
||||
strlen(argv[optind]+2));
|
||||
optind++;
|
||||
argv[musl_optind] + 2,
|
||||
strlen(argv[musl_optind] + 2));
|
||||
musl_optind++;
|
||||
return '?';
|
||||
}
|
||||
}
|
||||
|
||||
2
src/extern/utf8decoder.c
vendored
2
src/extern/utf8decoder.c
vendored
@@ -38,7 +38,7 @@ static const uint8_t utf8d[] = {
|
||||
1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* s8 */
|
||||
};
|
||||
|
||||
uint32_t decode(uint32_t *state, uint32_t *codep, uint32_t byte)
|
||||
uint32_t decode(uint32_t *state, uint32_t *codep, uint8_t byte)
|
||||
{
|
||||
uint32_t type = utf8d[byte];
|
||||
|
||||
|
||||
1517
src/fix/main.c
1517
src/fix/main.c
File diff suppressed because it is too large
Load Diff
115
src/fix/rgbfix.1
115
src/fix/rgbfix.1
@@ -24,39 +24,48 @@
|
||||
.Op Fl p Ar pad_value
|
||||
.Op Fl r Ar ram_size
|
||||
.Op Fl t Ar title_str
|
||||
.Ar file
|
||||
.Op Ar
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Nm
|
||||
program changes headers of Game Boy ROM images.
|
||||
program changes headers of Game Boy ROM images, typically generated by
|
||||
.Xr rgblink 1 ,
|
||||
though it will work with
|
||||
.Em any
|
||||
Game Boy ROM.
|
||||
It also performs other correctness operations, such as padding.
|
||||
.Nm
|
||||
only changes the fields for which it has values specified.
|
||||
Developers are advised to fill those fields with 0x00 bytes in their source code before running
|
||||
.Nm ,
|
||||
and to have already populated whichever fields they don't specify using
|
||||
.Nm .
|
||||
.Pp
|
||||
Note that options can be abbreviated as long as the abbreviation is unambiguous:
|
||||
.Fl Fl verb
|
||||
.Fl Fl color-o
|
||||
is
|
||||
.Fl Fl verbose ,
|
||||
.Fl Fl color-only ,
|
||||
but
|
||||
.Fl Fl ver
|
||||
.Fl Fl color
|
||||
is invalid because it could also be
|
||||
.Fl Fl version .
|
||||
The arguments are as follows:
|
||||
.Fl Fl color-compatible .
|
||||
Options later in the command line override those set earlier.
|
||||
Accepted options are as follows:
|
||||
.Bl -tag -width Ds
|
||||
.It Fl C , Fl Fl color-only
|
||||
Set the Game Boy Color\(enonly flag:
|
||||
.Ad 0x143
|
||||
= 0xC0.
|
||||
If both this and the
|
||||
Set the Game Boy Color\(enonly flag
|
||||
.Pq Ad 0x143
|
||||
to 0xC0.
|
||||
This overrides
|
||||
.Fl c
|
||||
flag are set, this takes precedence.
|
||||
if it was set prior.
|
||||
.It Fl c , Fl Fl color-compatible
|
||||
Set the Game Boy Color\(encompatible flag:
|
||||
.Ad 0x143
|
||||
= 0x80.
|
||||
If both this and the
|
||||
.Fl C
|
||||
flag are set,
|
||||
.Fl C
|
||||
takes precedence.
|
||||
.Pq Ad 0x143
|
||||
to 0x80.
|
||||
This overrides
|
||||
.Fl c
|
||||
if it was set prior.
|
||||
.It Fl f Ar fix_spec , Fl Fl fix-spec Ar fix_spec
|
||||
Fix certain header values that the Game Boy checks for correctness.
|
||||
Alternatively, intentionally trash these values by writing their binary inverse instead.
|
||||
@@ -83,54 +92,63 @@ Trash the global checksum.
|
||||
.It Fl i Ar game_id , Fl Fl game-id Ar game_id
|
||||
Set the game ID string
|
||||
.Pq Ad 0x13F Ns \(en Ns Ad 0x142
|
||||
to a given string of exactly 4 characters.
|
||||
If both this and the title are set, the game ID will overwrite the overlapping portion of the title.
|
||||
to a given string.
|
||||
If it's longer than 4 chars, it will be truncated, and a warning emitted.
|
||||
.It Fl j , Fl Fl non-japanese
|
||||
Set the non-Japanese region flag:
|
||||
.Ad 0x14A
|
||||
= 1.
|
||||
Set the non-Japanese region flag
|
||||
.Pq Ad 0x14A
|
||||
to 0x01.
|
||||
.It Fl k Ar licensee_str , Fl Fl new-licensee Ar licensee_str
|
||||
Set the new licensee string
|
||||
.Pq Ad 0x144 Ns \(en Ns Ad 0x145
|
||||
to a given string, truncated to at most two characters.
|
||||
to a given string.
|
||||
If it's longer than 2 chars, it will be truncated, and a warning emitted.
|
||||
.It Fl l Ar licensee_id , Fl Fl old-licensee Ar licensee_id
|
||||
Set the old licensee code,
|
||||
.Ad 0x14B ,
|
||||
Set the old licensee code
|
||||
.Pq Ad 0x14B
|
||||
to a given value from 0 to 0xFF.
|
||||
This value is deprecated and should be set to 0x33 in all new software.
|
||||
.It Fl m Ar mbc_type , Fl Fl mbc-type Ar mbc_type
|
||||
Set the MBC type,
|
||||
.Ad 0x147 ,
|
||||
Set the MBC type
|
||||
.Pq Ad 0x147
|
||||
to a given value from 0 to 0xFF.
|
||||
This value may also be an MBC name from the Pan Docs.
|
||||
.It Fl n Ar rom_version , Fl Fl rom-version Ar rom_version
|
||||
Set the ROM version,
|
||||
.Ad 0x14C ,
|
||||
Set the ROM version
|
||||
.Pq Ad 0x14C
|
||||
to a given value from 0 to 0xFF.
|
||||
.It Fl p Ar pad_value , Fl Fl pad-value Ar pad_value
|
||||
Pad the image to a valid size with a given pad value from 0 to 0xFF.
|
||||
Pad the ROM image to a valid size with a given pad value from 0 to 255 (0xFF).
|
||||
.Nm
|
||||
will automatically pick a size from 32 KiB, 64 KiB, 128 KiB, ..., 8192 KiB.
|
||||
The cartridge size byte
|
||||
.Pq Ad 0x148
|
||||
will be changed to reflect this new size.
|
||||
The recommended padding value is 0xFF, to speed up writing the ROM to flash chips, and to avoid "nop slides" into VRAM.
|
||||
.It Fl r Ar ram_size , Fl Fl ram-size Ar ram_size
|
||||
Set the RAM size,
|
||||
.Ad 0x149 ,
|
||||
Set the RAM size
|
||||
.Pq Ad 0x149
|
||||
to a given value from 0 to 0xFF.
|
||||
.It Fl s , Fl Fl sgb-compatible
|
||||
Set the SGB flag:
|
||||
.Ad 0x146
|
||||
= 3. This flag will be ignored by the SGB unless the old licensee code is 0x33!
|
||||
Set the SGB flag
|
||||
.Pq Ad 0x146
|
||||
to 0x03.
|
||||
This flag will be ignored by the SGB unless the old licensee code is 0x33!
|
||||
If this is given as well as
|
||||
.Fl l ,
|
||||
but is not set to 0x33, a warning will be printed.
|
||||
.It Fl t Ar title , Fl Fl title Ar title
|
||||
Set the title string
|
||||
.Pq Ad 0x134 Ns \(en Ns Ad 0x143
|
||||
to a given string, truncated to at most 16 characters.
|
||||
It is recommended to use 15 characters instead, to avoid clashing with the CGB flag
|
||||
.Po Fl c
|
||||
to a given string.
|
||||
If the title is longer than the max length, it will be truncated, and a warning emitted.
|
||||
The max length is 11 characters if the game ID
|
||||
.Pq Fl i
|
||||
is specified, 15 characters if the CGB flag
|
||||
.Fl ( c
|
||||
or
|
||||
.Fl C
|
||||
.Pc .
|
||||
If both this and the game ID are set, the game ID will overwrite the overlapping portion of the title.
|
||||
.Fl C )
|
||||
is specified but the game ID is not, and 16 characters otherwise.
|
||||
.It Fl V , Fl Fl version
|
||||
Print the version of the program and exit.
|
||||
.It Fl v , Fl Fl validate
|
||||
@@ -138,7 +156,7 @@ Equivalent to
|
||||
.Fl f Cm lhg .
|
||||
.El
|
||||
.Sh EXAMPLES
|
||||
Most values in the ROM header are only cosmetic.
|
||||
Most values in the ROM header do not matter to the actual console, and most are seldom useful anyway.
|
||||
The bare minimum requirements for a workable program are the header checksum, the Nintendo logo, and (if needed) the CGB/SGB flags.
|
||||
It is a good idea to pad the image to a valid size as well
|
||||
.Pq Do valid Dc meaning a power of 2, times 32 KiB .
|
||||
@@ -151,14 +169,13 @@ a valid size:
|
||||
The following will make a SGB-enabled, color-enabled game with a title of
|
||||
.Dq foobar ,
|
||||
and pad it to a valid size.
|
||||
.Po
|
||||
The Game Boy itself does not use the title, but some emulators or ROM managers do.
|
||||
.Pc
|
||||
.Pq The Game Boy itself does not use the title, but some emulators or ROM managers do.
|
||||
.Pp
|
||||
.D1 $ rgbfix -vcs -l 0x33 -p 255 -t foobar baz.gb
|
||||
.Pp
|
||||
The following will duplicate the header (sans global checksum) of the game
|
||||
.Dq Survival Kids :
|
||||
The following will duplicate the header of the game
|
||||
.Dq Survival Kids ,
|
||||
sans global checksum:
|
||||
.Pp
|
||||
.D1 $ rgbfix -cjsv -k A4 -l 0x33 -m 0x1B -p 0xFF -r 3 -t SURVIVALKIDAVKE \
|
||||
SurvivalKids.gbc
|
||||
|
||||
@@ -22,12 +22,10 @@ as destination can omit the destination as it is assumed to be register
|
||||
.Sy A
|
||||
by default.
|
||||
The following two lines have the same effect:
|
||||
.Pp
|
||||
.Bd -literal -offset indent
|
||||
OR A,B
|
||||
OR B
|
||||
.Ed
|
||||
.Pp
|
||||
.Sh LEGEND
|
||||
List of abbreviations used in this document.
|
||||
.Bl -tag
|
||||
|
||||
@@ -96,7 +96,7 @@ int main(int argc, char *argv[])
|
||||
opts.attrmapout = true;
|
||||
break;
|
||||
case 'a':
|
||||
opts.attrmapfile = optarg;
|
||||
opts.attrmapfile = musl_optarg;
|
||||
break;
|
||||
case 'C':
|
||||
opts.colorcurve = true;
|
||||
@@ -105,7 +105,7 @@ int main(int argc, char *argv[])
|
||||
opts.debug = true;
|
||||
break;
|
||||
case 'd':
|
||||
depth = strtoul(optarg, NULL, 0);
|
||||
depth = strtoul(musl_optarg, NULL, 0);
|
||||
break;
|
||||
case 'F':
|
||||
opts.hardfix = true;
|
||||
@@ -121,19 +121,19 @@ int main(int argc, char *argv[])
|
||||
opts.unique = true;
|
||||
break;
|
||||
case 'o':
|
||||
opts.outfile = optarg;
|
||||
opts.outfile = musl_optarg;
|
||||
break;
|
||||
case 'P':
|
||||
opts.palout = true;
|
||||
break;
|
||||
case 'p':
|
||||
opts.palfile = optarg;
|
||||
opts.palfile = musl_optarg;
|
||||
break;
|
||||
case 'T':
|
||||
opts.tilemapout = true;
|
||||
break;
|
||||
case 't':
|
||||
opts.tilemapfile = optarg;
|
||||
opts.tilemapfile = musl_optarg;
|
||||
break;
|
||||
case 'u':
|
||||
opts.unique = true;
|
||||
@@ -145,15 +145,15 @@ int main(int argc, char *argv[])
|
||||
opts.verbose = true;
|
||||
break;
|
||||
case 'x':
|
||||
opts.trim = strtoul(optarg, NULL, 0);
|
||||
opts.trim = strtoul(musl_optarg, NULL, 0);
|
||||
break;
|
||||
default:
|
||||
print_usage();
|
||||
/* NOTREACHED */
|
||||
}
|
||||
}
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
argc -= musl_optind;
|
||||
argv += musl_optind;
|
||||
|
||||
if (argc == 0) {
|
||||
fputs("FATAL: no input files\n", stderr);
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
The
|
||||
.Nm
|
||||
program converts PNG images into the Nintendo Game Boy's planar tile format.
|
||||
|
||||
.Pp
|
||||
The resulting colors and their palette indices are determined differently depending on the input PNG file:
|
||||
.Bl -dash -width Ds
|
||||
.It
|
||||
@@ -39,7 +39,7 @@ If the image has multiple shades that map to the same index, the palette is inst
|
||||
.It
|
||||
If the image has color (or the grayscale method failed), the colors are sorted from lightest to darkest.
|
||||
.El
|
||||
|
||||
.Pp
|
||||
The input image may not contain more colors than the selected bit depth allows.
|
||||
Transparent pixels are set to palette index 0.
|
||||
.Sh ARGUMENTS
|
||||
@@ -53,7 +53,7 @@ is invalid because it could also be
|
||||
.Fl Fl version .
|
||||
The arguments are as follows:
|
||||
.Bl -tag -width Ds
|
||||
.It Fl a Ar attrmap, Fl Fl attr-map Ar attrmap
|
||||
.It Fl a Ar attrmap , Fl Fl attr-map Ar attrmap
|
||||
Generate a file of tile mirroring attributes for OAM or (CGB-only) background tiles.
|
||||
For each tile in the input file, a byte is written representing the dimensions that the associated tile in the output file should be mirrored.
|
||||
Useful in combination with
|
||||
|
||||
@@ -64,6 +64,22 @@ bool hash_AddElement(HashMap map, char const *key, void *element)
|
||||
return newEntry->next != NULL;
|
||||
}
|
||||
|
||||
bool hash_ReplaceElement(HashMap const map, char const *key, void *element)
|
||||
{
|
||||
HashType hashedKey = hash(key);
|
||||
struct HashMapEntry *ptr = map[(HalfHashType)hashedKey];
|
||||
|
||||
while (ptr) {
|
||||
if (hashedKey >> HALF_HASH_NB_BITS == ptr->hash
|
||||
&& !strcmp(ptr->key, key)) {
|
||||
ptr->content = element;
|
||||
return true;
|
||||
}
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool hash_RemoveElement(HashMap map, char const *key)
|
||||
{
|
||||
HashType hashedKey = hash(key);
|
||||
|
||||
@@ -439,7 +439,7 @@ void assign_AssignSections(void)
|
||||
return;
|
||||
}
|
||||
|
||||
trap_;
|
||||
unreachable_();
|
||||
}
|
||||
|
||||
void assign_Cleanup(void)
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
@@ -99,7 +100,7 @@ void error(struct FileStackNode const *where, uint32_t lineNo, char const *fmt,
|
||||
nbErrors++;
|
||||
}
|
||||
|
||||
noreturn_ void fatal(struct FileStackNode const *where, uint32_t lineNo, char const *fmt, ...)
|
||||
_Noreturn void fatal(struct FileStackNode const *where, uint32_t lineNo, char const *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
@@ -126,7 +127,13 @@ FILE *openFile(char const *fileName, char const *mode)
|
||||
if (!fileName)
|
||||
return NULL;
|
||||
|
||||
FILE *file = fopen(fileName, mode);
|
||||
FILE *file;
|
||||
if (strcmp(fileName, "-") != 0)
|
||||
file = fopen(fileName, mode);
|
||||
else if (mode[0] == 'r')
|
||||
file = fdopen(0, mode);
|
||||
else
|
||||
file = fdopen(1, mode);
|
||||
|
||||
if (!file)
|
||||
err(1, "Could not open file \"%s\"", fileName);
|
||||
@@ -210,23 +217,23 @@ int main(int argc, char *argv[])
|
||||
isWRA0Mode = true;
|
||||
break;
|
||||
case 'l':
|
||||
linkerScriptName = optarg;
|
||||
linkerScriptName = musl_optarg;
|
||||
break;
|
||||
case 'm':
|
||||
mapFileName = optarg;
|
||||
mapFileName = musl_optarg;
|
||||
break;
|
||||
case 'n':
|
||||
symFileName = optarg;
|
||||
symFileName = musl_optarg;
|
||||
break;
|
||||
case 'O':
|
||||
overlayFileName = optarg;
|
||||
overlayFileName = musl_optarg;
|
||||
break;
|
||||
case 'o':
|
||||
outputFileName = optarg;
|
||||
outputFileName = musl_optarg;
|
||||
break;
|
||||
case 'p':
|
||||
value = strtoul(optarg, &endptr, 0);
|
||||
if (optarg[0] == '\0' || *endptr != '\0') {
|
||||
value = strtoul(musl_optarg, &endptr, 0);
|
||||
if (musl_optarg[0] == '\0' || *endptr != '\0') {
|
||||
error(NULL, 0, "Invalid argument for option 'p'");
|
||||
value = 0xFF;
|
||||
}
|
||||
@@ -238,7 +245,7 @@ int main(int argc, char *argv[])
|
||||
break;
|
||||
case 's':
|
||||
/* FIXME: nobody knows what this does, figure it out */
|
||||
(void)optarg;
|
||||
(void)musl_optarg;
|
||||
warning(NULL, 0, "Nobody has any idea what `-s` does");
|
||||
break;
|
||||
case 't':
|
||||
@@ -264,7 +271,7 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
|
||||
int curArgIndex = optind;
|
||||
int curArgIndex = musl_optind;
|
||||
|
||||
/* If no input files were specified, the user must have screwed up */
|
||||
if (curArgIndex == argc) {
|
||||
|
||||
@@ -260,11 +260,11 @@ static void readSymbol(FILE *file, struct Symbol *symbol,
|
||||
* @param fileName The filename to report in errors
|
||||
* @param i The number of the patch to report in errors
|
||||
*/
|
||||
static void readPatch(FILE *file, struct Patch *patch, char const *fileName,
|
||||
char const *sectName, uint32_t i,
|
||||
struct Section *fileSections[], struct FileStackNode fileNodes[])
|
||||
static void readPatch(FILE *file, struct Patch *patch, char const *fileName, char const *sectName,
|
||||
uint32_t i, struct Section *fileSections[], struct FileStackNode fileNodes[])
|
||||
{
|
||||
uint32_t nodeID;
|
||||
uint8_t type;
|
||||
|
||||
tryReadlong(nodeID, file,
|
||||
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s node ID: %s",
|
||||
@@ -279,14 +279,14 @@ static void readPatch(FILE *file, struct Patch *patch, char const *fileName,
|
||||
tryReadlong(patch->pcSectionID, file,
|
||||
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s PC offset: %s",
|
||||
fileName, sectName, i);
|
||||
patch->pcSection = patch->pcSectionID == -1 ? NULL
|
||||
: fileSections[patch->pcSectionID];
|
||||
patch->pcSection = patch->pcSectionID == -1 ? NULL : fileSections[patch->pcSectionID];
|
||||
tryReadlong(patch->pcOffset, file,
|
||||
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s PC offset: %s",
|
||||
fileName, sectName, i);
|
||||
tryGetc(patch->type, file,
|
||||
tryGetc(type, file,
|
||||
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s type: %s",
|
||||
fileName, sectName, i);
|
||||
patch->type = type;
|
||||
tryReadlong(patch->rpnSize, file,
|
||||
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s RPN size: %s",
|
||||
fileName, sectName, i);
|
||||
@@ -349,6 +349,8 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam
|
||||
section->bank = tmp;
|
||||
tryGetc(byte, file, "%s: Cannot read \"%s\"'s alignment: %s",
|
||||
fileName, section->name);
|
||||
if (byte > 16)
|
||||
byte = 16;
|
||||
section->isAlignFixed = byte != 0;
|
||||
section->alignMask = (1 << byte) - 1;
|
||||
tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s alignment offset: %s",
|
||||
@@ -435,7 +437,7 @@ static void readAssertion(FILE *file, struct Assertion *assert,
|
||||
char const *fileName, uint32_t i,
|
||||
struct Section *fileSections[], struct FileStackNode fileNodes[])
|
||||
{
|
||||
char assertName[sizeof("Assertion #" EXPAND_AND_STR(UINT32_MAX))];
|
||||
char assertName[sizeof("Assertion #4294967295")]; // UINT32_MAX
|
||||
|
||||
snprintf(assertName, sizeof(assertName), "Assertion #%" PRIu32, i);
|
||||
|
||||
@@ -497,7 +499,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
|
||||
if (!nodes[fileID].nodes)
|
||||
err(1, "Failed to get memory for %s's nodes", fileName);
|
||||
verbosePrint("Reading %u nodes...\n", nodes[fileID].nbNodes);
|
||||
for (uint32_t i = 0; i < nodes[fileID].nbNodes; i++)
|
||||
for (uint32_t i = nodes[fileID].nbNodes; i--; )
|
||||
readFileStackNode(file, nodes[fileID].nodes, i, fileName);
|
||||
|
||||
/* This file's symbols, kept to link sections to them */
|
||||
|
||||
@@ -17,7 +17,13 @@
|
||||
|
||||
#include "extern/err.h"
|
||||
|
||||
FILE * outputFile;
|
||||
#include "linkdefs.h"
|
||||
|
||||
#include "platform.h" // MIN_NB_ELMS
|
||||
|
||||
#define BANK_SIZE 0x4000
|
||||
|
||||
FILE *outputFile;
|
||||
FILE *overlayFile;
|
||||
FILE *symFile;
|
||||
FILE *mapFile;
|
||||
@@ -35,6 +41,18 @@ static struct {
|
||||
} *banks;
|
||||
} sections[SECTTYPE_INVALID];
|
||||
|
||||
/* Defines the order in which types are output to the sym and map files */
|
||||
static enum SectionType typeMap[SECTTYPE_INVALID] = {
|
||||
SECTTYPE_ROM0,
|
||||
SECTTYPE_ROMX,
|
||||
SECTTYPE_VRAM,
|
||||
SECTTYPE_SRAM,
|
||||
SECTTYPE_WRAM0,
|
||||
SECTTYPE_WRAMX,
|
||||
SECTTYPE_OAM,
|
||||
SECTTYPE_HRAM
|
||||
};
|
||||
|
||||
void out_AddSection(struct Section const *section)
|
||||
{
|
||||
static uint32_t maxNbBanks[] = {
|
||||
@@ -60,8 +78,7 @@ void out_AddSection(struct Section const *section)
|
||||
sections[section->type].banks =
|
||||
realloc(sections[section->type].banks,
|
||||
sizeof(*sections[0].banks) * minNbBanks);
|
||||
for (uint32_t i = sections[section->type].nbBanks;
|
||||
i < minNbBanks; i++) {
|
||||
for (uint32_t i = sections[section->type].nbBanks; i < minNbBanks; i++) {
|
||||
sections[section->type].banks[i].sections = NULL;
|
||||
sections[section->type].banks[i].zeroLenSections = NULL;
|
||||
}
|
||||
@@ -103,43 +120,63 @@ struct Section const *out_OverlappingSection(struct Section const *section)
|
||||
|
||||
/**
|
||||
* Performs sanity checks on the overlay file.
|
||||
* @return The number of ROM banks in the overlay file
|
||||
*/
|
||||
static void checkOverlay(void)
|
||||
static uint32_t checkOverlaySize(void)
|
||||
{
|
||||
if (!overlayFile)
|
||||
return;
|
||||
return 0;
|
||||
|
||||
if (fseek(overlayFile, 0, SEEK_END) != 0) {
|
||||
warnx("Overlay file is not seekable, cannot check if properly formed");
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
|
||||
long overlaySize = ftell(overlayFile);
|
||||
|
||||
if (overlaySize % 0x4000)
|
||||
errx(1, "Overlay file must have a size multiple of 0x4000");
|
||||
|
||||
/* Reset back to beginning */
|
||||
fseek(overlayFile, 0, SEEK_SET);
|
||||
|
||||
uint32_t nbOverlayBanks = overlaySize / 0x4000 - 1;
|
||||
if (overlaySize % BANK_SIZE)
|
||||
errx(1, "Overlay file must have a size multiple of 0x4000");
|
||||
|
||||
if (nbOverlayBanks < 1)
|
||||
uint32_t nbOverlayBanks = overlaySize / BANK_SIZE;
|
||||
|
||||
if (is32kMode && nbOverlayBanks != 2)
|
||||
errx(1, "Overlay must be exactly 0x8000 bytes large");
|
||||
|
||||
if (nbOverlayBanks < 2)
|
||||
errx(1, "Overlay must be at least 0x8000 bytes large");
|
||||
|
||||
if (nbOverlayBanks > sections[SECTTYPE_ROMX].nbBanks) {
|
||||
return nbOverlayBanks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expand sections[SECTTYPE_ROMX].banks to cover all the overlay banks.
|
||||
* This ensures that writeROM will output each bank, even if some are not
|
||||
* covered by any sections.
|
||||
* @param nbOverlayBanks The number of banks in the overlay file
|
||||
*/
|
||||
static void coverOverlayBanks(uint32_t nbOverlayBanks)
|
||||
{
|
||||
/* 2 if is32kMode, 1 otherwise */
|
||||
uint32_t nbRom0Banks = maxsize[SECTTYPE_ROM0] / BANK_SIZE;
|
||||
/* Discount ROM0 banks to avoid outputting too much */
|
||||
uint32_t nbUncoveredBanks = nbOverlayBanks - nbRom0Banks > sections[SECTTYPE_ROMX].nbBanks
|
||||
? nbOverlayBanks - nbRom0Banks
|
||||
: 0;
|
||||
|
||||
if (nbUncoveredBanks > sections[SECTTYPE_ROMX].nbBanks) {
|
||||
sections[SECTTYPE_ROMX].banks =
|
||||
realloc(sections[SECTTYPE_ROMX].banks,
|
||||
sizeof(*sections[SECTTYPE_ROMX].banks) *
|
||||
nbOverlayBanks);
|
||||
sizeof(*sections[SECTTYPE_ROMX].banks) * nbUncoveredBanks);
|
||||
if (!sections[SECTTYPE_ROMX].banks)
|
||||
err(1, "Failed to realloc banks for overlay");
|
||||
for (uint32_t i = sections[SECTTYPE_ROMX].nbBanks;
|
||||
i < nbOverlayBanks; i++) {
|
||||
for (uint32_t i = sections[SECTTYPE_ROMX].nbBanks; i < nbUncoveredBanks; i++) {
|
||||
sections[SECTTYPE_ROMX].banks[i].sections = NULL;
|
||||
sections[SECTTYPE_ROMX].banks[i].zeroLenSections = NULL;
|
||||
}
|
||||
sections[SECTTYPE_ROMX].nbBanks = nbOverlayBanks;
|
||||
sections[SECTTYPE_ROMX].nbBanks = nbUncoveredBanks;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,16 +232,19 @@ static void writeROM(void)
|
||||
outputFile = openFile(outputFileName, "wb");
|
||||
overlayFile = openFile(overlayFileName, "rb");
|
||||
|
||||
checkOverlay();
|
||||
uint32_t nbOverlayBanks = checkOverlaySize();
|
||||
|
||||
if (nbOverlayBanks > 0)
|
||||
coverOverlayBanks(nbOverlayBanks);
|
||||
|
||||
if (outputFile) {
|
||||
if (sections[SECTTYPE_ROM0].nbBanks > 0)
|
||||
writeBank(sections[SECTTYPE_ROM0].banks[0].sections,
|
||||
0x0000, 0x4000);
|
||||
startaddr[SECTTYPE_ROM0], maxsize[SECTTYPE_ROM0]);
|
||||
|
||||
for (uint32_t i = 0 ; i < sections[SECTTYPE_ROMX].nbBanks; i++)
|
||||
writeBank(sections[SECTTYPE_ROMX].banks[i].sections,
|
||||
0x4000, 0x4000);
|
||||
startaddr[SECTTYPE_ROMX], maxsize[SECTTYPE_ROMX]);
|
||||
}
|
||||
|
||||
closeFile(outputFile);
|
||||
@@ -296,12 +336,13 @@ static void writeSymBank(struct SortedSections const *bankSections)
|
||||
/**
|
||||
* Write a bank's contents to the map file
|
||||
* @param bankSections The bank's sections
|
||||
* @return The bank's slack space
|
||||
*/
|
||||
static void writeMapBank(struct SortedSections const *sectList,
|
||||
enum SectionType type, uint32_t bank)
|
||||
static uint16_t writeMapBank(struct SortedSections const *sectList,
|
||||
enum SectionType type, uint32_t bank)
|
||||
{
|
||||
if (!mapFile)
|
||||
return;
|
||||
return 0;
|
||||
|
||||
struct SortedSection const *section = sectList->sections;
|
||||
struct SortedSection const *zeroLenSection = sectList->zeroLenSections;
|
||||
@@ -340,6 +381,34 @@ static void writeMapBank(struct SortedSections const *sectList,
|
||||
else
|
||||
fprintf(mapFile, " SLACK: $%04" PRIx16 " byte%s\n\n", slack,
|
||||
slack == 1 ? "" : "s");
|
||||
|
||||
return slack;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the total slack space by section type to the map file
|
||||
* @param slackMap The total slack space by section type
|
||||
*/
|
||||
static void writeMapSlack(uint32_t slackMap[MIN_NB_ELMS(SECTTYPE_INVALID)])
|
||||
{
|
||||
if (!mapFile)
|
||||
return;
|
||||
|
||||
fputs("FREE:\n", mapFile);
|
||||
|
||||
for (uint8_t i = 0; i < SECTTYPE_INVALID; i++) {
|
||||
enum SectionType type = typeMap[i];
|
||||
|
||||
// Do not output slack space for VRAM or OAM
|
||||
if (type == SECTTYPE_VRAM || type == SECTTYPE_OAM)
|
||||
continue;
|
||||
|
||||
if (sections[type].nbBanks > 0) {
|
||||
fprintf(mapFile, " %s: $%04" PRIx32 " byte%s in %" PRIu32 " bank%s\n",
|
||||
typeNames[type], slackMap[type], slackMap[type] == 1 ? "" : "s",
|
||||
sections[type].nbBanks, sections[type].nbBanks == 1 ? "" : "s");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -350,16 +419,7 @@ static void writeSymAndMap(void)
|
||||
if (!symFileName && !mapFileName)
|
||||
return;
|
||||
|
||||
enum SectionType typeMap[SECTTYPE_INVALID] = {
|
||||
SECTTYPE_ROM0,
|
||||
SECTTYPE_ROMX,
|
||||
SECTTYPE_VRAM,
|
||||
SECTTYPE_SRAM,
|
||||
SECTTYPE_WRAM0,
|
||||
SECTTYPE_WRAMX,
|
||||
SECTTYPE_OAM,
|
||||
SECTTYPE_HRAM
|
||||
};
|
||||
uint32_t slackMap[SECTTYPE_INVALID] = {0};
|
||||
|
||||
symFile = openFile(symFileName, "w");
|
||||
mapFile = openFile(mapFileName, "w");
|
||||
@@ -370,16 +430,16 @@ static void writeSymAndMap(void)
|
||||
for (uint8_t i = 0; i < SECTTYPE_INVALID; i++) {
|
||||
enum SectionType type = typeMap[i];
|
||||
|
||||
if (sections[type].nbBanks > 0) {
|
||||
for (uint32_t bank = 0; bank < sections[type].nbBanks;
|
||||
bank++) {
|
||||
writeSymBank(§ions[type].banks[bank]);
|
||||
writeMapBank(§ions[type].banks[bank],
|
||||
type, bank);
|
||||
}
|
||||
for (uint32_t bank = 0; bank < sections[type].nbBanks; bank++) {
|
||||
struct SortedSections const *sect = §ions[type].banks[bank];
|
||||
|
||||
writeSymBank(sect);
|
||||
slackMap[type] += writeMapBank(sect, type, bank);
|
||||
}
|
||||
}
|
||||
|
||||
writeMapSlack(slackMap);
|
||||
|
||||
closeFile(symFile);
|
||||
closeFile(mapFile);
|
||||
}
|
||||
|
||||
@@ -18,50 +18,10 @@
|
||||
#include "link/symbol.h"
|
||||
|
||||
#include "linkdefs.h"
|
||||
#include "opmath.h"
|
||||
|
||||
#include "extern/err.h"
|
||||
|
||||
static int32_t asl(int32_t value, int32_t shiftamt); // Forward decl for below
|
||||
static int32_t asr(int32_t value, int32_t shiftamt)
|
||||
{
|
||||
uint32_t uvalue = value;
|
||||
|
||||
// Get the easy cases out of the way
|
||||
if (shiftamt == 0)
|
||||
return value;
|
||||
if (value == 0 || shiftamt <= -32)
|
||||
return 0;
|
||||
if (shiftamt > 31)
|
||||
return (value < 0) ? -1 : 0;
|
||||
if (shiftamt < 0)
|
||||
return asl(value, -shiftamt);
|
||||
if (value > 0)
|
||||
return uvalue >> shiftamt;
|
||||
|
||||
{
|
||||
// Calculate an OR mask for sign extension
|
||||
// 1->0x80000000, 2->0xC0000000, ..., 31->0xFFFFFFFE
|
||||
uint32_t shiftamt_high_bits = -((uint32_t)1 << (32 - shiftamt));
|
||||
|
||||
return (uvalue >> shiftamt) | shiftamt_high_bits;
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t asl(int32_t value, int32_t shiftamt)
|
||||
{
|
||||
// Repeat the easy cases here to avoid INT_MIN funny business
|
||||
if (shiftamt == 0)
|
||||
return value;
|
||||
if (value == 0 || shiftamt >= 32)
|
||||
return 0;
|
||||
if (shiftamt < -31)
|
||||
return (value < 0) ? -1 : 0;
|
||||
if (shiftamt < 0)
|
||||
return asr(value, -shiftamt);
|
||||
|
||||
return (uint32_t)value << shiftamt;
|
||||
}
|
||||
|
||||
/*
|
||||
* This is an "empty"-type stack. Apart from the actual values, we also remember
|
||||
* whether the value is a placeholder inserted for error recovery. This allows
|
||||
@@ -219,7 +179,7 @@ static int32_t computeRPNExpr(struct Patch const *patch,
|
||||
popRPN();
|
||||
value = INT32_MAX;
|
||||
} else {
|
||||
value = popRPN() / value;
|
||||
value = op_divide(popRPN(), value);
|
||||
}
|
||||
break;
|
||||
case RPN_MOD:
|
||||
@@ -231,12 +191,24 @@ static int32_t computeRPNExpr(struct Patch const *patch,
|
||||
popRPN();
|
||||
value = 0;
|
||||
} else {
|
||||
value = popRPN() % value;
|
||||
value = op_modulo(popRPN(), value);
|
||||
}
|
||||
break;
|
||||
case RPN_UNSUB:
|
||||
value = -popRPN();
|
||||
break;
|
||||
case RPN_EXP:
|
||||
value = popRPN();
|
||||
if (value < 0) {
|
||||
if (!isError)
|
||||
error(patch->src, patch->lineNo, "Exponent by negative");
|
||||
isError = true;
|
||||
popRPN();
|
||||
value = 0;
|
||||
} else {
|
||||
value = op_exponent(popRPN(), value);
|
||||
}
|
||||
break;
|
||||
|
||||
case RPN_OR:
|
||||
value = popRPN() | popRPN();
|
||||
@@ -288,11 +260,11 @@ static int32_t computeRPNExpr(struct Patch const *patch,
|
||||
|
||||
case RPN_SHL:
|
||||
value = popRPN();
|
||||
value = asl(popRPN(), value);
|
||||
value = op_shift_left(popRPN(), value);
|
||||
break;
|
||||
case RPN_SHR:
|
||||
value = popRPN();
|
||||
value = asr(popRPN(), value);
|
||||
value = op_shift_right(popRPN(), value);
|
||||
break;
|
||||
|
||||
case RPN_BANK_SYM:
|
||||
@@ -490,9 +462,10 @@ static void applyFilePatches(struct Section *section, struct Section *dataSectio
|
||||
|
||||
/* `jr` is quite unlike the others... */
|
||||
if (patch->type == PATCHTYPE_JR) {
|
||||
/* Target is relative to the byte *after* the operand */
|
||||
// Offset is relative to the byte *after* the operand
|
||||
// PC as operand to `jr` is lower than reference PC by 2
|
||||
uint16_t address = patch->pcSection->org
|
||||
+ patch->pcOffset + 1;
|
||||
+ patch->pcOffset + 2;
|
||||
int16_t jumpOffset = value - address;
|
||||
|
||||
if (!isError && (jumpOffset < -128 || jumpOffset > 127))
|
||||
|
||||
@@ -66,7 +66,7 @@ Enable DMG mode.
|
||||
Prohibit the use of sections that doesn't exist on a DMG, such as VRAM bank 1.
|
||||
This option automatically enables
|
||||
.Fl w .
|
||||
.It Fl l Ar linker_script, Fl Fl linkerscript Ar linker_script
|
||||
.It Fl l Ar linker_script , Fl Fl linkerscript Ar linker_script
|
||||
Specify a linker script file that tells the linker how sections must be placed in the ROM.
|
||||
The attributes assigned in the linker script must be consistent with any assigned in the code.
|
||||
See
|
||||
@@ -116,7 +116,7 @@ option!
|
||||
.Sh EXAMPLES
|
||||
All you need for a basic ROM is an object file, which can be made into a ROM image like so:
|
||||
.Pp
|
||||
.D1 $ rgblink -o bar.gb foo.o
|
||||
.Dl $ rgblink -o bar.gb foo.o
|
||||
.Pp
|
||||
The resulting
|
||||
.Ar bar.gb
|
||||
@@ -126,12 +126,10 @@ You should use
|
||||
to fix these so that the program will actually run in a Game Boy:
|
||||
.Pp
|
||||
.Dl $ rgbfix -v bar.gb
|
||||
.Ed
|
||||
.Pp
|
||||
Here is a more complete example:
|
||||
.Pp
|
||||
.Dl $ rgblink -o bin/game.gb -n bin/game.sym -p 0xFF obj/title.o obj/engine.o
|
||||
.Ed
|
||||
.Sh BUGS
|
||||
Please report bugs on
|
||||
.Lk https://github.com/gbdev/rgbds/issues GitHub .
|
||||
|
||||
@@ -19,7 +19,6 @@ They can be lowercase or uppercase, it is ignored.
|
||||
Any line can contain a comment starting with
|
||||
.Ql \&;
|
||||
that ends at the end of the line:
|
||||
.Pp
|
||||
.Bd -literal -offset indent
|
||||
ROMX $F ; This is a comment
|
||||
"Functions to read array"
|
||||
@@ -86,6 +85,6 @@ in the linker script.
|
||||
.Sh HISTORY
|
||||
.Nm
|
||||
was originally written by Carsten S\(/orensen as part of the ASMotor package,
|
||||
and was later packaged in RGBDS by Justin Lloyd. It is now maintained by a
|
||||
number of contributors at
|
||||
and was later packaged in RGBDS by Justin Lloyd.
|
||||
It is now maintained by a number of contributors at
|
||||
.Lk https://github.com/gbdev/rgbds .
|
||||
|
||||
@@ -318,7 +318,7 @@ static void processCommand(enum LinkerScriptCommand command, uint16_t arg,
|
||||
{
|
||||
switch (command) {
|
||||
case COMMAND_INVALID:
|
||||
trap_;
|
||||
unreachable_();
|
||||
|
||||
case COMMAND_ORG:
|
||||
break;
|
||||
@@ -331,8 +331,8 @@ static void processCommand(enum LinkerScriptCommand command, uint16_t arg,
|
||||
}
|
||||
|
||||
if (arg < *pc)
|
||||
errx(1, "%s(%" PRIu32 "): `%s` cannot be used to go backwards",
|
||||
linkerScriptName, lineNo, commands[command]);
|
||||
errx(1, "%s(%" PRIu32 "): `%s` cannot be used to go backwards (currently at $%x)",
|
||||
linkerScriptName, lineNo, commands[command], *pc);
|
||||
*pc = arg;
|
||||
}
|
||||
|
||||
@@ -391,12 +391,12 @@ struct SectionPlacement *script_NextSection(void)
|
||||
|
||||
switch (parserState) {
|
||||
case PARSER_FIRSTTIME:
|
||||
trap_;
|
||||
unreachable_();
|
||||
|
||||
case PARSER_LINESTART:
|
||||
switch (token->type) {
|
||||
case TOKEN_INVALID:
|
||||
trap_;
|
||||
unreachable_();
|
||||
|
||||
case TOKEN_EOF:
|
||||
if (!popFile())
|
||||
|
||||
@@ -39,62 +39,123 @@ void sect_ForEach(void (*callback)(struct Section *, void *), void *arg)
|
||||
hash_ForEach(sections, forEach, &callbackArg);
|
||||
}
|
||||
|
||||
static void mergeSections(struct Section *target, struct Section *other, enum SectionModifier mod)
|
||||
static void checkSectUnionCompat(struct Section *target, struct Section *other)
|
||||
{
|
||||
if (target->type != other->type)
|
||||
errx(1, "Section \"%s\" is defined with conflicting types %s and %s",
|
||||
other->name,
|
||||
typeNames[target->type], typeNames[other->type]);
|
||||
if (other->isAddressFixed) {
|
||||
if (target->isAddressFixed) {
|
||||
if (target->org != other->org)
|
||||
errx(1, "Section \"%s\" is defined with conflicting addresses $%" PRIx16 " and $%" PRIx16,
|
||||
errx(1, "Section \"%s\" is defined with conflicting addresses $%04"
|
||||
PRIx16 " and $%04" PRIx16,
|
||||
other->name, target->org, other->org);
|
||||
} else if (target->isAlignFixed) {
|
||||
if ((other->org - target->alignOfs) & target->alignMask)
|
||||
errx(1, "Section \"%s\" is defined with conflicting %" PRIu16 "-byte alignment (offset %" PRIu16 ") and address $%" PRIx16,
|
||||
errx(1, "Section \"%s\" is defined with conflicting %" PRIu16
|
||||
"-byte alignment (offset %" PRIu16 ") and address $%04" PRIx16,
|
||||
other->name, target->alignMask + 1,
|
||||
target->alignOfs, other->org);
|
||||
}
|
||||
target->isAddressFixed = true;
|
||||
target->org = other->org;
|
||||
|
||||
} else if (other->isAlignFixed) {
|
||||
if (target->isAddressFixed) {
|
||||
if ((target->org - other->alignOfs) & other->alignMask)
|
||||
errx(1, "Section \"%s\" is defined with conflicting address $%" PRIx16 " and %" PRIu16 "-byte alignment (offset %" PRIu16 ")",
|
||||
errx(1, "Section \"%s\" is defined with conflicting address $%04"
|
||||
PRIx16 " and %" PRIu16 "-byte alignment (offset %" PRIu16 ")",
|
||||
other->name, target->org,
|
||||
other->alignMask + 1, other->alignOfs);
|
||||
} else if (target->isAlignFixed
|
||||
&& (other->alignMask & target->alignOfs)
|
||||
!= (target->alignMask & other->alignOfs)) {
|
||||
errx(1, "Section \"%s\" is defined with conflicting %" PRIu16 "-byte alignment (offset %" PRIu16 ") and %" PRIu16 "-byte alignment (offset %" PRIu16 ")",
|
||||
other->name, target->alignMask + 1,
|
||||
target->alignOfs, other->alignMask + 1,
|
||||
other->alignOfs);
|
||||
} else if (!target->isAlignFixed
|
||||
|| (other->alignMask > target->alignMask)) {
|
||||
errx(1, "Section \"%s\" is defined with conflicting %" PRIu16
|
||||
"-byte alignment (offset %" PRIu16 ") and %" PRIu16
|
||||
"-byte alignment (offset %" PRIu16 ")",
|
||||
other->name, target->alignMask + 1, target->alignOfs,
|
||||
other->alignMask + 1, other->alignOfs);
|
||||
} else if (!target->isAlignFixed || (other->alignMask > target->alignMask)) {
|
||||
target->isAlignFixed = true;
|
||||
target->alignMask = other->alignMask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void checkFragmentCompat(struct Section *target, struct Section *other)
|
||||
{
|
||||
if (other->isAddressFixed) {
|
||||
uint16_t org = other->org - target->size;
|
||||
|
||||
if (target->isAddressFixed) {
|
||||
if (target->org != org)
|
||||
errx(1, "Section \"%s\" is defined with conflicting addresses $%04"
|
||||
PRIx16 " and $%04" PRIx16,
|
||||
other->name, target->org, other->org);
|
||||
|
||||
} else if (target->isAlignFixed) {
|
||||
if ((org - target->alignOfs) & target->alignMask)
|
||||
errx(1, "Section \"%s\" is defined with conflicting %" PRIu16
|
||||
"-byte alignment (offset %" PRIu16 ") and address $%04" PRIx16,
|
||||
other->name, target->alignMask + 1,
|
||||
target->alignOfs, other->org);
|
||||
}
|
||||
target->isAddressFixed = true;
|
||||
target->org = org;
|
||||
|
||||
} else if (other->isAlignFixed) {
|
||||
int32_t ofs = (other->alignOfs - target->size) % (other->alignMask + 1);
|
||||
|
||||
if (ofs < 0)
|
||||
ofs += other->alignMask + 1;
|
||||
|
||||
if (target->isAddressFixed) {
|
||||
if ((target->org - ofs) & other->alignMask)
|
||||
errx(1, "Section \"%s\" is defined with conflicting address $%04"
|
||||
PRIx16 " and %" PRIu16 "-byte alignment (offset %" PRIu16 ")",
|
||||
other->name, target->org,
|
||||
other->alignMask + 1, other->alignOfs);
|
||||
|
||||
} else if (target->isAlignFixed
|
||||
&& (other->alignMask & target->alignOfs) != (target->alignMask & ofs)) {
|
||||
errx(1, "Section \"%s\" is defined with conflicting %" PRIu16
|
||||
"-byte alignment (offset %" PRIu16 ") and %" PRIu16
|
||||
"-byte alignment (offset %" PRIu16 ")",
|
||||
other->name, target->alignMask + 1, target->alignOfs,
|
||||
other->alignMask + 1, other->alignOfs);
|
||||
|
||||
} else if (!target->isAlignFixed || (other->alignMask > target->alignMask)) {
|
||||
target->isAlignFixed = true;
|
||||
target->alignMask = other->alignMask;
|
||||
target->alignOfs = ofs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void mergeSections(struct Section *target, struct Section *other, enum SectionModifier mod)
|
||||
{
|
||||
// Common checks
|
||||
|
||||
if (target->type != other->type)
|
||||
errx(1, "Section \"%s\" is defined with conflicting types %s and %s",
|
||||
other->name, typeNames[target->type], typeNames[other->type]);
|
||||
|
||||
if (other->isBankFixed) {
|
||||
if (!target->isBankFixed) {
|
||||
target->isBankFixed = true;
|
||||
target->bank = other->bank;
|
||||
} else if (target->bank != other->bank) {
|
||||
errx(1, "Section \"%s\" is defined with conflicting banks %" PRIu32 " and %" PRIu32,
|
||||
other->name, target->bank, other->bank);
|
||||
errx(1, "Section \"%s\" is defined with conflicting banks %" PRIu32 " and %"
|
||||
PRIu32, other->name, target->bank, other->bank);
|
||||
}
|
||||
}
|
||||
|
||||
switch (mod) {
|
||||
case SECTION_UNION:
|
||||
checkSectUnionCompat(target, other);
|
||||
if (other->size > target->size)
|
||||
target->size = other->size;
|
||||
break;
|
||||
|
||||
case SECTION_FRAGMENT:
|
||||
checkFragmentCompat(target, other);
|
||||
target->size += other->size;
|
||||
other->offset = target->size - other->size;
|
||||
if (sect_HasData(target->type)) {
|
||||
@@ -108,7 +169,7 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se
|
||||
break;
|
||||
|
||||
case SECTION_NORMAL:
|
||||
trap_;
|
||||
unreachable_();
|
||||
}
|
||||
|
||||
other->nextu = target->nextu;
|
||||
@@ -167,20 +228,20 @@ static void doSanityChecks(struct Section *section, void *ptr)
|
||||
fail("Section \"%s\" has an invalid type.", section->name);
|
||||
if (is32kMode && section->type == SECTTYPE_ROMX) {
|
||||
if (section->isBankFixed && section->bank != 1)
|
||||
fail("%s: ROMX sections must be in bank 1 with option -t.",
|
||||
fail("%s: ROMX sections must be in bank 1 (if any) with option -t",
|
||||
section->name);
|
||||
else
|
||||
section->type = SECTTYPE_ROM0;
|
||||
}
|
||||
if (isWRA0Mode && section->type == SECTTYPE_WRAMX) {
|
||||
if (section->isBankFixed && section->bank != 1)
|
||||
fail("%s: WRAMX sections must be in bank 1 with options -w or -d.",
|
||||
fail("%s: WRAMX sections must be in bank 1 with options -w or -d",
|
||||
section->name);
|
||||
else
|
||||
section->type = SECTTYPE_WRAMX;
|
||||
}
|
||||
if (isDmgMode && section->type == SECTTYPE_VRAM && section->bank == 1)
|
||||
fail("%s: VRAM bank 1 can't be used with option -d.",
|
||||
fail("%s: VRAM bank 1 can't be used with option -d",
|
||||
section->name);
|
||||
|
||||
/*
|
||||
@@ -191,17 +252,13 @@ static void doSanityChecks(struct Section *section, void *ptr)
|
||||
section->isAlignFixed = false;
|
||||
|
||||
/* Too large an alignment may not be satisfiable */
|
||||
if (section->isAlignFixed
|
||||
&& (section->alignMask & startaddr[section->type]))
|
||||
fail("%s: %s sections cannot be aligned to $%" PRIx16 " bytes",
|
||||
section->name, typeNames[section->type],
|
||||
section->alignMask + 1);
|
||||
if (section->isAlignFixed && (section->alignMask & startaddr[section->type]))
|
||||
fail("%s: %s sections cannot be aligned to $%04" PRIx16 " bytes",
|
||||
section->name, typeNames[section->type], section->alignMask + 1);
|
||||
|
||||
uint32_t minbank = bankranges[section->type][0],
|
||||
maxbank = bankranges[section->type][1];
|
||||
uint32_t minbank = bankranges[section->type][0], maxbank = bankranges[section->type][1];
|
||||
|
||||
if (section->isBankFixed && section->bank < minbank
|
||||
&& section->bank > maxbank)
|
||||
if (section->isBankFixed && section->bank < minbank && section->bank > maxbank)
|
||||
fail(minbank == maxbank
|
||||
? "Cannot place section \"%s\" in bank %" PRIu32 ", it must be %" PRIu32
|
||||
: "Cannot place section \"%s\" in bank %" PRIu32 ", it must be between %" PRIu32 " and %" PRIu32,
|
||||
@@ -219,35 +276,26 @@ static void doSanityChecks(struct Section *section, void *ptr)
|
||||
section->isBankFixed = true;
|
||||
}
|
||||
|
||||
if (section->isAlignFixed) {
|
||||
enum SectionType type = section->type;
|
||||
|
||||
if (section->isAddressFixed) {
|
||||
/* It doesn't make sense to have both org and alignment set */
|
||||
if (section->isAddressFixed) {
|
||||
if (section->org & section->alignMask)
|
||||
if (section->isAlignFixed) {
|
||||
if ((section->org & section->alignMask) != section->alignOfs)
|
||||
fail("Section \"%s\"'s fixed address doesn't match its alignment",
|
||||
section->name);
|
||||
section->isAlignFixed = false;
|
||||
} else if ((endaddr(type) & section->alignMask)
|
||||
== startaddr[type]) {
|
||||
section->org = startaddr[type];
|
||||
section->isAlignFixed = false;
|
||||
section->isAddressFixed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (section->isAddressFixed) {
|
||||
/* Ensure the target address is valid */
|
||||
if (section->org < startaddr[section->type]
|
||||
|| section->org > endaddr(section->type))
|
||||
fail("Section \"%s\"'s fixed address %#" PRIx16 " is outside of range [%#" PRIx16 "; %#" PRIx16 "]",
|
||||
section->name, section->org,
|
||||
fail("Section \"%s\"'s fixed address %#" PRIx16 " is outside of range [%#"
|
||||
PRIx16 "; %#" PRIx16 "]", section->name, section->org,
|
||||
startaddr[section->type], endaddr(section->type));
|
||||
|
||||
if (section->org + section->size > endaddr(section->type) + 1)
|
||||
fail("Section \"%s\"'s end address %#" PRIx16 " is greater than last address %#" PRIx16,
|
||||
section->name, section->org + section->size,
|
||||
endaddr(section->type) + 1);
|
||||
fail("Section \"%s\"'s end address %#" PRIx16
|
||||
" is greater than last address %#" PRIx16, section->name,
|
||||
section->org + section->size, endaddr(section->type) + 1);
|
||||
}
|
||||
|
||||
#undef fail
|
||||
|
||||
@@ -13,11 +13,11 @@ uint16_t startaddr[] = {
|
||||
};
|
||||
|
||||
uint16_t maxsize[] = {
|
||||
[SECTTYPE_ROM0] = 0x8000,
|
||||
[SECTTYPE_ROM0] = 0x8000, // patched to 0x4000 if !is32kMode
|
||||
[SECTTYPE_ROMX] = 0x4000,
|
||||
[SECTTYPE_VRAM] = 0x2000,
|
||||
[SECTTYPE_SRAM] = 0x2000,
|
||||
[SECTTYPE_WRAM0] = 0x2000,
|
||||
[SECTTYPE_WRAM0] = 0x2000, // patched to 0x1000 if !isWRA0Mode
|
||||
[SECTTYPE_WRAMX] = 0x1000,
|
||||
[SECTTYPE_OAM] = 0x00A0,
|
||||
[SECTTYPE_HRAM] = 0x007F
|
||||
|
||||
88
src/opmath.c
Normal file
88
src/opmath.c
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* This file is part of RGBDS.
|
||||
*
|
||||
* Copyright (c) 1997-2021, RGBDS contributors.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
/*
|
||||
* Mathematical operators that don't reuse C's behavior
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "opmath.h"
|
||||
|
||||
int32_t op_divide(int32_t dividend, int32_t divisor)
|
||||
{
|
||||
// Adjust division to floor toward negative infinity,
|
||||
// not truncate toward zero
|
||||
return dividend / divisor - ((dividend % divisor < 0) != (divisor < 0));
|
||||
}
|
||||
|
||||
int32_t op_modulo(int32_t dividend, int32_t divisor)
|
||||
{
|
||||
int32_t remainder = dividend % divisor;
|
||||
|
||||
// Adjust modulo to have the sign of the divisor,
|
||||
// not the sign of the dividend
|
||||
return remainder + divisor * ((remainder < 0) != (divisor < 0));
|
||||
}
|
||||
|
||||
int32_t op_exponent(int32_t base, uint32_t power)
|
||||
{
|
||||
int32_t result = 1;
|
||||
|
||||
for (;;) {
|
||||
if (power % 2)
|
||||
result *= base;
|
||||
power /= 2;
|
||||
if (!power)
|
||||
break;
|
||||
base *= base;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int32_t op_shift_left(int32_t value, int32_t amount)
|
||||
{
|
||||
// Get the easy cases out of the way
|
||||
if (amount == 0)
|
||||
return value;
|
||||
if (value == 0 || amount >= 32)
|
||||
return 0;
|
||||
if (amount < -31)
|
||||
return (value < 0) ? -1 : 0;
|
||||
if (amount < 0)
|
||||
return op_shift_right(value, -amount);
|
||||
|
||||
// Use unsigned to force a bitwise shift
|
||||
// Casting back is OK because the types implement two's complement behavior
|
||||
return (uint32_t)value << amount;
|
||||
}
|
||||
|
||||
int32_t op_shift_right(int32_t value, int32_t amount)
|
||||
{
|
||||
// Repeat the easy cases here to avoid INT_MIN funny business
|
||||
if (amount == 0)
|
||||
return value;
|
||||
if (value == 0 || amount <= -32)
|
||||
return 0;
|
||||
if (amount > 31)
|
||||
return (value < 0) ? -1 : 0;
|
||||
if (amount < 0)
|
||||
return op_shift_left(value, -amount);
|
||||
|
||||
if (value > 0)
|
||||
return (uint32_t)value >> amount;
|
||||
|
||||
// Calculate an OR mask for sign extension
|
||||
// 1->0x80000000, 2->0xC0000000, ..., 31->0xFFFFFFFE
|
||||
uint32_t amount_high_bits = -(UINT32_C(1) << (32 - amount));
|
||||
|
||||
// The C standard leaves shifting right negative values
|
||||
// undefined, so use a left shift manually sign-extended
|
||||
return ((uint32_t)value >> amount) | amount_high_bits;
|
||||
}
|
||||
@@ -18,7 +18,6 @@ and
|
||||
.Xr rgblink 1 .
|
||||
.Em Please note that the specifications may change .
|
||||
This toolchain is in development and new features may require adding more information to the current format, or modifying some fields, which would break compatibility with older versions.
|
||||
.Pp
|
||||
.Sh FILE STRUCTURE
|
||||
The following types are used:
|
||||
.Pp
|
||||
@@ -29,7 +28,6 @@ is an 8‐bit integer.
|
||||
.Ar STRING
|
||||
is a 0‐terminated string of
|
||||
.Ar BYTE .
|
||||
.Pp
|
||||
.Bd -literal
|
||||
; Header
|
||||
|
||||
@@ -43,7 +41,7 @@ LONG NumberOfSections ; The number of sections used in this file.
|
||||
LONG NumberOfNodes ; The number of nodes contained in this file.
|
||||
|
||||
REPT NumberOfNodes ; IMPORTANT NOTE: the nodes are actually written in
|
||||
; **reverse** order, meaningthe node with ID 0 is
|
||||
; **reverse** order, meaning the node with ID 0 is
|
||||
; the last one in the file!
|
||||
|
||||
LONG ParentID ; ID of the parent node, -1 means this is the root.
|
||||
@@ -219,8 +217,7 @@ The
|
||||
In the RGB format, RPN expressions are stored as
|
||||
.Ar BYTE Ns s
|
||||
with some bytes being special prefixes for integers and symbols.
|
||||
.Pp
|
||||
.Bl -column -offset indent ".Sy String" ".Sy String"
|
||||
.Bl -column -offset indent "Sy String" "Sy String"
|
||||
.It Sy Value Ta Sy Meaning
|
||||
.It Li $00 Ta Li + operator
|
||||
.It Li $01 Ta Li - operator
|
||||
@@ -228,6 +225,7 @@ with some bytes being special prefixes for integers and symbols.
|
||||
.It Li $03 Ta Li / operator
|
||||
.It Li $04 Ta Li % operator
|
||||
.It Li $05 Ta Li unary -
|
||||
.It Li $06 Ta Li ** operator
|
||||
.It Li $10 Ta Li | operator
|
||||
.It Li $11 Ta Li & operator
|
||||
.It Li $12 Ta Li ^ operator
|
||||
@@ -259,7 +257,6 @@ integer follows.
|
||||
.It Li $81 Ta Ar LONG
|
||||
symbol ID follows.
|
||||
.El
|
||||
.Pp
|
||||
.Sh SEE ALSO
|
||||
.Xr rgbasm 1 ,
|
||||
.Xr rgblink 1 ,
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
.Nd Rednex Game Boy Development System
|
||||
.Sh EXAMPLES
|
||||
To get a working ROM image from a single assembly source file:
|
||||
.Pp
|
||||
.Bd -literal -offset indent
|
||||
$ rgbasm \-o bar.o foo.asm
|
||||
$ rgblink \-o baz.gb bar.o
|
||||
@@ -26,7 +25,7 @@ $ rgbfix \-v \-p 0 baz.gb
|
||||
.Xr rgbds 5 ,
|
||||
.Xr gbz80 7
|
||||
.Sh HISTORY
|
||||
.Bl -ohang
|
||||
.Bl -item
|
||||
.It
|
||||
1997, Carsten S\(/orensen (AKA SurfSmurf) writes ASMotor as a general-purpose
|
||||
assembler/linker system for DOS/Win32.
|
||||
@@ -37,8 +36,8 @@ assembly/machine code, and releases this version as RGBDS.
|
||||
2009, Vegard Nossum adapts the code to be more UNIX-like and releases this
|
||||
version as rgbds-linux on GitHub.
|
||||
.It
|
||||
2010, Anthony J. Bentley forks that repository. The fork becomes the reference
|
||||
implementation of rgbds.
|
||||
2010, Anthony J. Bentley forks that repository.
|
||||
The fork becomes the reference implementation of rgbds.
|
||||
.It
|
||||
2017, Bentley's repository is moved to a neutral name.
|
||||
It is now maintained by a number of contributors at
|
||||
|
||||
@@ -9,17 +9,24 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "helpers.h"
|
||||
#include "version.h"
|
||||
|
||||
const char *get_package_version_string(void)
|
||||
{
|
||||
static char s[50];
|
||||
|
||||
/* The following conditional should be simplified by the compiler. */
|
||||
// The following conditional should be simplified by the compiler.
|
||||
if (strlen(BUILD_VERSION_STRING) == 0) {
|
||||
snprintf(s, sizeof(s), "v%d.%d.%d", PACKAGE_VERSION_MAJOR,
|
||||
PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCH);
|
||||
return s;
|
||||
// Fallback if version string can't be obtained from Git
|
||||
#ifndef PACKAGE_VERSION_RC
|
||||
return "v" EXPAND_AND_STR(PACKAGE_VERSION_MAJOR)
|
||||
"." EXPAND_AND_STR(PACKAGE_VERSION_MINOR)
|
||||
"." EXPAND_AND_STR(PACKAGE_VERSION_PATCH);
|
||||
#else
|
||||
return "v" EXPAND_AND_STR(PACKAGE_VERSION_MAJOR)
|
||||
"." EXPAND_AND_STR(PACKAGE_VERSION_MINOR)
|
||||
"." EXPAND_AND_STR(PACKAGE_VERSION_PATCH)
|
||||
"-rc" EXPAND_AND_STR(PACKAGE_VERSION_RC);
|
||||
#endif
|
||||
} else {
|
||||
return BUILD_VERSION_STRING;
|
||||
}
|
||||
|
||||
4
test/asm/.gitignore
vendored
4
test/asm/.gitignore
vendored
@@ -1 +1,3 @@
|
||||
quote\"file.*
|
||||
/quote\"file.*
|
||||
/version.asm
|
||||
/version.out
|
||||
|
||||
8
test/asm/align-16.asm
Normal file
8
test/asm/align-16.asm
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
SECTION "Byte", ROM0
|
||||
|
||||
db 2
|
||||
|
||||
SECTION "ROM0", ROM0, ALIGN[16]
|
||||
|
||||
db 1
|
||||
1
test/asm/align-16.out.bin
Normal file
1
test/asm/align-16.out.bin
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
2
test/asm/align-large-ofs.asm
Normal file
2
test/asm/align-large-ofs.asm
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
SECTION "Tesst", ROM0, ALIGN[1,2]
|
||||
3
test/asm/align-large-ofs.err
Normal file
3
test/asm/align-large-ofs.err
Normal file
@@ -0,0 +1,3 @@
|
||||
ERROR: align-large-ofs.asm(2):
|
||||
Alignment offset (2) must be smaller than alignment size (2)
|
||||
error: Assembly aborted (1 errors)!
|
||||
0
test/asm/align-large-ofs.out
Normal file
0
test/asm/align-large-ofs.out
Normal file
2
test/asm/align-large.asm
Normal file
2
test/asm/align-large.asm
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
SECTION "You lost the game", ROM0[17]
|
||||
0
test/asm/align-large.err
Normal file
0
test/asm/align-large.err
Normal file
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user