Add a utility that aids in generating m2c output (#279)

* Add a utility to help generate m2c output

* Make a small change to an OS file which fixes a small issue when using m2ctx

Signed-off-by: Taggerung <tyler.taggerung@gmail.com>
This commit is contained in:
Tyler McGavran
2022-12-09 21:03:30 -05:00
committed by GitHub
parent 3172daa2ea
commit e1976267f3
4 changed files with 196 additions and 49 deletions
+1
View File
@@ -53,6 +53,7 @@ doxygen/doxygen/*
# no context header for now, until it's greenlit for the repo
context_gen_header.h
contexts/*
tools/m2ctx.py
# regardless of the above, the context file itself shouldnt be there
ctx.c
+95 -48
View File
@@ -1,67 +1,114 @@
#include "include/functions.h"
#include "include/libc/math.h"
#include "include/libc/stdio.h"
#include "include/libc/stdarg.h"
#include "include/libc/string.h"
#include "include/libc/stddef.h"
#include "include/libc/stdio.h"
#include "include/libc/stdlib.h"
#include "include/types.h"
#include "include/variables.h"
#include "include/libc/string.h"
#include "include/actor_types.h"
#include "include/common_structs.h"
#include "include/config.h"
#include "include/course_offsets.h"
#include "include/defines.h"
#include "include/functions.h"
#include "include/global.h"
#include "include/kart_attributes.h"
#include "include/macros.h"
#include "include/objects.h"
#include "include/segment_symbols.h"
#include "include/segments.h"
#include "include/PR/rcp.h"
#include "include/PR/libultra.h"
#include "include/PR/os_vi.h"
#include "include/PR/ultratypes.h"
#include "include/PR/os_eeprom.h"
#include "include/PR/os_pi.h"
#include "include/PR/os_exception.h"
#include "include/PR/os_internal.h"
#include "include/PR/gs2dex.h"
#include "include/PR/R4300.h"
#include "include/PR/os_time.h"
#include "include/PR/ucode.h"
#include "include/PR/os_cont.h"
#include "include/PR/gbi.h"
#include "include/sounds.h"
#include "include/trig_tables.h"
#include "include/types.h"
#include "include/ultra64.h"
#include "include/variables.h"
#include "include/vehicles.h"
#include "include/waypoints.h"
#include "include/PR/abi.h"
#include "include/PR/gbi.h"
#include "include/PR/gs2dex.h"
#include "include/PR/gu.h"
#include "include/PR/libaudio.h"
#include "include/PR/os_message.h"
#include "include/PR/os.h"
#include "include/PR/os_tlb.h"
#include "include/PR/sptask.h"
#include "include/PR/os_thread.h"
#include "include/PR/os_rdp.h"
#include "include/PR/os_ai.h"
#include "include/PR/libultra.h"
#include "include/PR/mbi.h"
#include "include/PR/os.h"
#include "include/PR/os_ai.h"
#include "include/PR/os_cache.h"
#include "include/PR/os_misc.h"
#include "include/PR/os_cont.h"
#include "include/PR/os_eeprom.h"
#include "include/PR/os_exception.h"
#include "include/PR/os_internal.h"
#include "include/PR/os_libc.h"
#include "include/global.h"
#include "include/ultra64.h"
#include "include/kart_attributes.h"
#include "include/common_structs.h"
#include "include/segment_symbols.h"
#include "include/macros.h"
#include "include/config.h"
#include "src/memory.h"
#include "src/main.h"
#include "src/synthesis.h"
#include "src/framebuffers.h"
#include "src/os/osint.h"
#include "src/os/hardware.h"
#include "src/os/printf.h"
#include "src/os/new_func.h"
#include "src/os/libaudio_internal.h"
#include "src/os/osContInternal.h"
#include "src/os/controller.h"
#include "src/os/osAi.h"
#include "include/PR/os_message.h"
#include "include/PR/os_misc.h"
#include "include/PR/os_pi.h"
#include "include/PR/os_rdp.h"
#include "include/PR/os_thread.h"
#include "include/PR/os_time.h"
#include "include/PR/os_tlb.h"
#include "include/PR/os_vi.h"
#include "include/PR/R4300.h"
#include "include/PR/rcp.h"
#include "include/PR/sptask.h"
#include "include/PR/ucode.h"
#include "include/PR/ultratypes.h"
#include "src/os/bstring.h"
#include "src/os/controller.h"
#include "src/os/hardware.h"
#include "src/os/libaudio_internal.h"
#include "src/os/libultra_internal.h"
#include "src/os/new_func.h"
#include "src/os/osAi.h"
#include "src/os/osContInternal.h"
#include "src/os/osint.h"
#include "src/os/piint.h"
#include "src/profiler.h"
#include "src/os/printf.h"
#include "src/audio/data.h"
#include "src/audio/effects.h"
#include "src/audio/external.h"
#include "src/audio/heap.h"
#include "src/audio/internal.h"
#include "src/audio/load.h"
#include "src/audio/playback.h"
#include "src/audio/port_eu.h"
#include "src/audio/seqplayer.h"
#include "src/audio/synthesis.h"
#include "src/actors.h"
#include "src/actors_extended.h"
#include "src/camera.h"
#include "src/camera_junk.h"
#include "src/ceremony_and_credits.h"
#include "src/common_textures.h"
#include "src/credits_01.h"
#include "src/data_segment2.h"
#include "src/framebuffers.h"
#include "src/kart_dma.h"
#include "src/main.h"
#include "src/math_util.h"
#include "src/math_util_2.h"
#include "src/memory.h"
#include "src/menus.h"
#include "src/profiler.h"
#include "src/race_logic.h"
#include "src/render_courses.h"
#include "src/skybox_and_splitscreen.h"
#include "src/spawn_players.h"
#include "src/staff_ghosts.h"
#include "src/textures.h"
#include "src/code_800029B0.h"
#include "src/code_80004740.h"
#include "src/code_80005FD0.h"
#include "src/code_8001F980.h"
#include "src/code_80027D00.h"
#include "src/code_8003DC40.h"
#include "src/code_800431B0.h"
#include "src/code_80057C60.h"
#include "src/code_80071F00.h"
#include "src/code_80086E70.h"
#include "src/code_8008C1D0.h"
#include "src/code_80091750.h"
#include "src/code_800AF9B0.h"
#include "src/code_800B45E0.h"
#include "src/code_80280000.h"
#include "src/code_80280650.h"
#include "src/code_80281780.h"
#include "src/internal.h"
#include "src/code_80281C40.h"
+98
View File
@@ -0,0 +1,98 @@
#!/bin/bash
#
# m2c_helper.sh
# Utility to aid in the generation of m2c output for specific functions
#
# example usages:
# m2c_helper.sh code_80091750 func_800AAC18
# m2c_helper.sh audio/load audio_init
#
# Be sure to set DISASSEMBLY_FILE to a filename you are comfortable with.
# Defaulted to function_disassembly.c
#
# It's expected that the utility will be ran twice for any given function.
# The first run will create a context file specific to the target function
# The second run will create the full m2c output for the function,
# assuming the appropriate function specific context file found.
#
# 99.9% of the time you will only use the function specific context file to give names
# to the function arguments. Even then, you don't HAVE to do that. The file can be
# empty if you want.
# As long as its contents are acceptable to m2c, do whatever you want to it.
#
# It may take a while for either run to complete as including all binary data
# from .incbin directives is a bit slow
#
# Parent directory for all the non-matching function assembly files
ASM_DIR="asm/non_matchings"
# Parent directory that function specific contexts will be placed/found in
CONTEXT_DIR="contexts"
# Expand the data/**.s glob into an array.
DATA_FILES=(data/**.s)
# Un-array the file list
DATA_FILES="${DATA_FILES[@]}"
DATA_FILES_EXCLUDE="data/rsp.s"
# Use bash variable expansion to remove each file-to-exclude from the DATA_FILES string
for file in $DATA_FILES_EXCLUDE; do
DATA_FILES="${DATA_FILES//$file/}"
done
# Context for all the non-function-specific stuff (type definitions, function prototypes, etc.)
COMMON_CONTEXT="ctx.c"
# Extra options when generating the function specific context
CONTEXT_M2C_OPTS="--stack-structs --zfill-constants"
# Extra options when generating the function disassembly
DISASSEMBLY_M2C_OPTS="--zfill-constants"
# Directories to search when a .incbin line is encountered in a data file
INCBIN_OPTS="--incbin-dir ./expected/build/us --incbin-dir ./"
# File that the final disassembly output will be placed. Can be named whatever you want
DISASSEMBLY_FILE="function_disassembly.c"
# Directory where the m2c program can be found
M2C_DIR="../mips_to_c"
# Collect the SHA1 sum of the commit m2c is checked out too
pushd "${M2C_DIR}" > /dev/null
M2C_SHA="$(git rev-parse HEAD)"
popd > /dev/null
directory="$1"
function="$2"
make_function_context () {
echo "$(${M2C_DIR}/m2c.py \
${ASM_DIR}/${directory}/${function}.s \
${DATA_FILES} \
${INCBIN_OPTS} \
${CONTEXT_M2C_OPTS} \
--context ${COMMON_CONTEXT})" > "${CONTEXT_DIR}/${directory}/${function}.c" 2>&1
}
make_function_disassembly () {
echo "#ifdef MIPS_TO_C";
echo "//generated by m2c commit ${M2C_SHA} on $(date +%b-%d-%Y)";
${M2C_DIR}/m2c.py \
${ASM_DIR}/${directory}/${function}.s \
${DATA_FILES} \
${INCBIN_OPTS} \
${DISASSEMBLY_M2C_OPTS} \
--context ${COMMON_CONTEXT} --context ${CONTEXT_DIR}/${directory}/${function}.c
echo "#else";
echo "GLOBAL_ASM(\"${ASM_DIR}/${directory}/${function}.s\")";
echo "#endif"
}
# If there isn't already a sub-dir for the specified directory, create one
if [[ ! -d "${CONTEXT_DIR}/${directory}" ]]; then
mkdir -p "${CONTEXT_DIR}/${directory}"
fi
# If there is a function specific context file, create the full disassembly
# Otherwise, create the function specific context file
if [[ -f "${CONTEXT_DIR}/${directory}/${function}.c" ]]; then
echo "$(make_function_disassembly ${directory} ${function})" > "${DISASSEMBLY_FILE}" 2>&1
else
make_function_context ${directory} ${function}
fi
+2 -1
View File
@@ -23,7 +23,8 @@ extern "C" {
* rights reserved under the Copyright Laws of the United States.
*/
#ident "$Revision: 1.4 $"
// Causes issues when using m2ctx, not really necessary for our purposes
//#ident "$Revision: 1.4 $"
extern void bcopy(const void *, void *, size_t);
extern int bcmp(const void *, const void *, int);