About Me
Welcome to my blog! This is where I collect my observations and notes on programming and technology. The main subjects range from implementation details to broader ideas about programming.
Main Topics
- Engineering Projects: Exploring implementation details and how technical systems work.
- C/C++: Notes on language features and programming techniques.
- The Programmer’s Perspective: Ideas about developing a career and a way of thinking as a programmer.
For more, visit the categories page.
Contact
If you have questions or would like to discuss something, please get in touch through the About page.
Thank you for reading and for your support. I hope these notes help you on your own technical journey!
Running NEMU Through Make in Batch Mode
The earlier notes on getopt_long examined how command-line arguments reach the program. In monitor.c, parse_args handles the different options, including the batch option.
As an experiment, changing the value used by the switch directly to 'b' makes NEMU enter batch mode immediately. Having confirmed the control flow, we can instead pass the option normally with make run ARGS=-b.
How Batch Mode Works
In sdb.c, sdb_set_batch_mode sets is_batch_mode to true. Then sdb_mainloop checks that flag to decide whether to call cmd_c directly with a NULL argument.
The notes on cmd_c explain how the arguments supplied along this call chain affect the number of instructions NEMU executes.
Passing Command-Line Arguments to NEMU
The main NEMU Makefile does not define ARGS in the place I first looked. It appears in scripts/native.mk. Running make run ARGS=-b passes the option through this variable and enables batch mode.
How ARGS Flows Through scripts/native.mk
The run recipe invokes git_commit for tracking changes; that part is unrelated to argument handling. The command assembled in NEMU_EXEC includes ARGS.
The makefile also appends to ARGS in two places. The later addition involving ARGS_DIFF is particularly relevant.
Appending Arguments
These notes originally focused on the case where ARGS is created by make run ARGS=-b, and on how the first override assignment interacts with it. An override assignment can also define a previously undefined variable; it is not restricted to variables supplied on the command line.
The Meaning of override
Ordinary assignments in a makefile do not override a variable value supplied on the command line. The override keyword allows the makefile to modify that value anyway. Here the assignments append flags, preserving the caller’s arguments while adding the flags the project needs.
I did not locate the definition of ARGS_DIFF in the ysyx Makefile, the NEMU Makefile, or the three .mk files under NEMU’s scripts directory that I examined.
-include
The leading minus sign means a missing included makefile does not cause an immediate error; Make continues processing the remaining rules.
Argument Passing in Abstract Machine
Combining the documented Abstract Machine build process with the NEMU makefile analysis makes the path clearer. The Abstract Machine Makefile defines settings such as ARCH and compiler options. riscv32-nemu.mk adds several more settings.
In nemu.mk, the run recipe passes ARGS using NEMUFLAGS. Since NEMUFLAGS already contains logging options, appending -b enables batch mode while preserving those options.
Other Details in the Abstract Machine Makefiles
Files with the .mk suffix split the build rules into smaller pieces. Different makefiles include the pieces appropriate to their platform.
The Makefile under AM_HOME explicitly defines the working directory and several file locations. It uses AS, CC, CXX, LD, and AR to select the tools for the build stages. CFLAGS, CXXFLAGS, ASFLAGS, and LDFLAGS pass options to the C compiler, C++ compiler, assembler, and linker respectively.
When building the cpu-tests programs, WORK_DIR is the test directory and DST_DIR is its build subdirectory. Compilation recipes turn .c, .cc, and other source files into .o object files without linking them yet.
mkdir -p ensures that the output directory exists. -std=gnu11 selects GNU C11, -c requests compilation without linking, and -o supplies the object-file name. The project also uses C++17.
The IMAGE rules are the next important part: $(IMAGE).elf depends on the object files and LIBS. For understanding the runtime, these libraries matter, and they are not interchangeable with ordinary GNU/Linux libraries.
Pay attention to Make’s assignment operators when following LIBS. := evaluates the right-hand side immediately, while = defers expansion until the variable is used. The original notes described an immediately evaluated assignment as lazy; those two behaviors should be distinguished.
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. All the images used in the blog are my original works or AI works, if you want to take it,don't hesitate. Thank you !