# ==========  RUNTIME SUPPORT CODE - xv6 USER_MODE PROGRAMS  ========== 
#
# This file contains functions needed by
#    KPLSupport.s
#    BasicSupport.s
#    UserSystem package
#    KPL Programs running in user mode
#
#  This file contains "_entry", where execution begins.
#
# Harry Porter  -  9 October 2023   -  Created from code from previous .s files
#

	.sourcefile	"UserRuntime.s", "Support for xv6 User-Mode Programs - Harry Porter"

	.import		_P_UserSystem_KPLSystemInitialize
	.export		_KPLSysInit

	.import		_kplEntry				# From compiled "main" function
	.import		exit					# From KPLLRuntime.s
	.import		_putString				# .

	.begin		executable


#########################  _entry  ##########################
# 
# Prepare for KPL program execution.
#
# The stack pointer will be initialized by the kernel before execution begins.
#
# Call the "_kplEntry" function, which will call "main". "_kplEntry" is a function
# generated by the compiler for any package containing the "main" function.
#
# CONCERNING STACK USAGE: "_entry" allocates exactly 32 bytes onto the stack.
#        A return address of 0 is pushed as the "return address", indicating the
#        bottom of the stack for any user-mode debugging.
#
#############################################################

_entry:
	.export		_entry
	.function	"_entry",line=0,framesize=32
	.local		0,"arg_1",line=0,type="I"
	.local		8,"arg_2",line=0,type="I"
	.local		16,"arg_3",line=0,type="I"
	movi		gp,0x8_0000_8000	# Initialize the Global Pointer register
	movi		tp,0			# Set "tp", the Thread Pointer register to NULL
	addi		sp,sp,-32		# Allocate frame (argSaveArea=3*8) with
	stored		24(sp),r0		# . return address of zero, to indicate no caller
	call		_kplEntry		# Call "_kplEntry" with args (r1,r2,r3)
	movi		r1,0			# Call exit(0)
	call		exit			#
	.endfunction



#####################  _KPLSysInit  ####################
#
#           _KPLSysInit - UPCALL TO "KPLSystemInitialize"
# 
# This function is passed nothing and returns nothing.  It is called during
# startup to initialize the heap, as necessary.
#
# This function is called from code generated by the compiler.
# Here we are equating this to the name of a function written in KPL so
# the KPL function will be invoked to do the work.
#
# Standard KPL calling conventions: Yes
# Passed: nothing
# Returns: nothing
#
#############################################################

_KPLSysInit:	.equ	_P_UserSystem_KPLSystemInitialize

