This is the mail archive of the newlib@sources.redhat.com mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Commandline Support for the H8300 Simulator.


Venky,

  You are adding a flag to configure.host that will always be
on, yet you state that this is only for the simulator.  I don't
see the point of the flag if you are not going to optionally turn
it on or off.  What is your intention?  The current code in the
sys directory is sim-dependent at the moment.  I would like to
see in the future that this code be migrated to libgloss and
a special sim package built.

-- Jeff J.

D.Venkatasubramanian, Noida wrote:
Hi All,

I am submitting a patch for adding support for the processing of commandline arguments on the H8300 Simulator.

A brief description of each of the changes follows:

(File : h8300_h_patch.txt)
1) Added a pseudo opcode O_SYS_CMDLINE which will act as a trap identifier in h8300.h.


(File : crt0_S_patch.txt)
2) An extra instruction enclosed between a #ifdef __SIMULATOR_ARGV_SUPPORT__ / #endif directives, which simulates a trap instruction.
This instruction is a jsr to 0xcc, one of the locations in the magic vector used by H8300.


(File : configure_host_patch.txt)
3) Added a macro -DSIMULATOR_ARGV_SUPPORT. The configure.host file ensures that this macro is activated when our target is a simulator.


(File : inst_h_patch.txt)
4) Added a new char ** variable which will hold the address of the Commandline arguments which the common files of the simulator would provide.


(File : compile_c_patch.txt)
This requires a lengthy explanation
5) This macro sets a variable addr_cmdline to point to the location of the 8-bit high memory area as defined in the linker scripts. If the linker scripts change, then so would this require a change.


 #define SET_CMDLINE_LOCATION \
   if (h8300smode) \
     addr_cmdline = 0xffff00L; \
   else if (h8300hmode) \
     addr_cmdline = 0x2ff00L; \
   else \
     addr_cmdline = 0xff00L;

6) 0xcc is a magic vector location used to simulate a trap instruction on H8300.
Q : Why I chose 0xcc?
A : A patch for File I/O implementation is pending approval, and those require 0xc5 - 0xcb, hence the 0xcc, once that patch is approved, the Oxc4 would be rendered useless and we could then use it instead of 0xcc. (Whether or not we want to change to 0xc4 then is a matter of trivial opinion.)


 		      switch (dst->src.literal)
  			{
 			case 0xc4:
  			  dst->opcode = O (O_SYSCALL, SB);
 			  break;
 			case 0xcc:
 			  dst->opcode = O (O_SYS_CMDLINE, SB);
 			  break;
  			}
 		      /* End of Processing for system calls.  */

7) Setting pointer to Commandline arguments provided by the common simulator support.
/* Command Line support. */
if (argv != NULL)
{
ptr_CommandLine = argv;
}
8) The code starting with this in sim_resume handles the actual simulation of the system call.


case O (O_SYS_CMDLINE, SB):

I have added a lot of comments to the code.

Briefly, this saves the commandline arguments to the 8-bit memory area, and sets up an array which contains the addresses in the 8-bit area, where
each argument starts. Once that is done, these have to be saved in simulator memory consecutively.
As I need memory which is non volatile, I steal some memory from the stack and move the stack pointer such that I have enough memory to store all
these addresses. As teh stack pointer holds the new
value at the end, this memory is rendered non-volatile.


At this point, I store the addresses in the array containing the address values for the commandline arguments to this memory.

Nick Clifton had pointed out that at the beginning, there should be another value, a pointer to the beginning of argv. But the H8300 compiler seems not to require this. I set the no. of arguments to Register 0, address of the first of the address on the non-volatile memory to Register 1, and set the new stack pointer to the top of this non-volatile memory. (Slightly controversial stuff, that extra
pointer to argv that Nick had specified, but adding
that seems to break the support, the last argument
gets left out.)


Tested on H8300-hms and H8300-elf and does not seem
to break anything. Also I am able to print the commandline arguments correctly.


I used a simple test case :
int main(int argc, char *argv[])
{
        int i;
        int j = 0;

printf ("Argc = %d\n", argc);

        for (i = 0; i < argc; i++)
                printf ("%s\n", argv[i]);
        return 0;
}

Could someone review this patch and point out any issues. Thought, this would be very useful feature combined with
the File I/O feature for which I have already submitted
a patch.


I think I will end this long description here.

Thank You Nick Clifton and Kazu Hirata and all those that were helpful in the explanation of ABI and other issues.

Thanks and Regards,

Venky

PS : There are two files complete_patch.txt and complete_ChangeLog.txt which contain all the patches
and ChangeLogs in single files respectively.


Changelog Entries

In include/opcode (File : binutils_ChangeLog.txt)


2003-02-22 D.Venkatasubramanian <dvenkat at noida dot hcltech dot com>


	* h8300.h: Added a pseudo opcodes for Commandline
	processing.

In newlib/ (File : newlib_ChangeLog.txt)

2003-02-22 D.Venkatasubramanian <dvenkat at noida dot hcltech dot com>

* libc/include/sys/h8300hms/crt0.S: Added an extra instruction enclosed by SIMULATOR_ARGV_SUPPORT macro for Commandline support when target is simulator.
* configure.host: Added -DSIMULATOR_ARGV_SUPPORT for
Commandline Support.


In sim/h8300/ (File : sim_ChangeLog.txt)

2003-02-22 D.Venkatasubramanian <dvenkat at noida dot hcltech dot com>

* compile.c: Added #define SET_CMDLINE_LOCATION to set the location of 8-bit (256 locations) where the
Command Line arguments would be stored.
(decode): Added a TRAP to 0xcc for Commandline processing using pseudo opcode O_SYS_CMDLINE.
(sim_resume): Added handling of O_SYS_CMDLINE Trap.
(sim_create_inferior): Setting a pointer to Commandline Args array.
* inst.h: Added a new variable ptr_CommandLine for
storing pointer to Commandline array.




------------------------------------------------------------------------

2003-02-22 D.Venkatasubramanian <dvenkat at noida dot hcltech dot com>

* compile.c: Added #define SET_CMDLINE_LOCATION to set the location of 8-bit (256 locations) where the
Command Line arguments would be stored.
(decode): Added a TRAP to 0xcc for Commandline processing using pseudo opcode O_SYS_CMDLINE.
(sim_resume): Added handling of O_SYS_CMDLINE Trap.
(sim_create_inferior): Setting a pointer to Commandline Args array.
* inst.h: Added a new variable ptr_CommandLine for
storing pointer to Commandline array.



------------------------------------------------------------------------


2003-02-22 D.Venkatasubramanian <dvenkat at noida dot hcltech dot com>

* libc/include/sys/h8300hms/crt0.S: Added an extra instruction enclosed by SIMULATOR_ARGV_SUPPORT macro for Commandline support when target is simulator.
* configure.host: Added -DSIMULATOR_ARGV_SUPPORT for
Commandline Support.




------------------------------------------------------------------------

*** sim/h8300/inst.h.original Wed Feb 19 15:28:24 2003
--- sim/h8300/inst.h.modified Sat Feb 22 19:50:51 2003
*************** enum h8300_sim_state {
*** 66,71 ****
--- 66,74 ----
SIM_STATE_RUNNING, SIM_STATE_EXITED, SIM_STATE_SIGNALLED, SIM_STATE_STOPPED
};
+ /* For Command Line. */
+ char **ptr_CommandLine; /* Pointer to command Line Arguments. */
+ typedef struct
{
enum h8300_sim_state state;



------------------------------------------------------------------------


*** include/opcode/h8300.h.original Wed Feb 19 16:09:36 2003
--- include/opcode/h8300.h.modified Sat Feb 22 19:52:15 2003
*************** struct h8_opcode *** 311,316 ****
--- 311,319 ----
#define O_SYS_STAT 106
#define O_SYS_FSTAT 107
/* End of System Call specific Changes. */
+ /* For Command Line Processing. */
+ #define O_SYS_CMDLINE 120
+ #define SB 0
#define SW 1
#define SL 2



------------------------------------------------------------------------


*** newlib/libc/sys/h8300hms/crt0.S.original	Wed Feb 19 15:51:31 2003
--- newlib/libc/sys/h8300hms/crt0.S.modified	Sat Feb 22 19:51:35 2003
*************** _start:
*** 16,23 ****
--- 16,29 ----
  #ifdef __ELF__
  	mov.l   #__fini,r0
  	jsr     @_atexit
+ #ifdef SIMULATOR_ARGV_SUPPORT
+         jsr     @0xcc
+ #endif
  	jsr     @__init
  #else
+ #ifdef SIMULATOR_ARGV_SUPPORT
+         jsr     @0xcc
+ #endif
  	jsr	@___main
  #endif
  	jsr	@_main
*************** _start:
*** 45,52 ****
--- 51,64 ----
  #ifdef __ELF__
  	mov.l   #__fini,r0
  	jsr     @_atexit
+ #ifdef SIMULATOR_ARGV_SUPPORT
+         jsr     @0xcc
+ #endif
  	jsr     @__init
  #else
+ #ifdef SIMULATOR_ARGV_SUPPORT
+         jsr     @0xcc
+ #endif
  	jsr	@___main
  #endif
  	jsr	@_main
*************** _start:
*** 74,81 ****
--- 86,99 ----
  #ifdef __ELF__
  	mov.l   #__fini,r0
  	jsr     @_atexit
+ #ifdef SIMULATOR_ARGV_SUPPORT
+         jsr     @0xcc
+ #endif
  	jsr     @__init
  #else
+ #ifdef SIMULATOR_ARGV_SUPPORT
+         jsr     @0xcc
+ #endif
  	jsr	@___main
  #endif
  	jsr	@_main


------------------------------------------------------------------------


*** newlib/configure.host.original	Wed Feb 19 15:53:53 2003
--- newlib/configure.host.modified	Sat Feb 22 19:51:13 2003
*************** case "${host}" in
*** 482,487 ****
--- 482,489 ----
    h8300*-*-*)
  	syscall_dir=syscalls	
  	newlib_cflags="${newlib_cflags} -DSMALL_DTOA -DSMALL_MEMORY"
+ 	# Command Line support for H8300
+ 	newlib_cflags="${newlib_cflags} -DSIMULATOR_ARGV_SUPPORT"
  	;;	
    h8500-*-*)
  	syscall_dir=syscalls	


------------------------------------------------------------------------


*** sim/h8300/compile.c.original Wed Feb 19 15:28:09 2003
--- sim/h8300/compile.c.modified Sat Feb 22 19:50:43 2003
*************** void sim_set_simcache_size PARAMS ((int)
*** 119,124 ****
--- 119,132 ----
#define UEXTSHORT(x) ((x) & 0xffff)
#define SEXTSHORT(x) ((short) (x))
+ #define SET_CMDLINE_LOCATION \
+ if (h8300smode) \
+ addr_cmdline = 0xffff00L; \
+ else if (h8300hmode) \
+ addr_cmdline = 0x2ff00L; \
+ else \
+ addr_cmdline = 0xff00L;
+ static cpu_state_type cpu;
int h8300hmode = 0;
*************** decode (int addr, unsigned char *data, d
*** 447,460 ****
dst->opcode = q->how;
dst->cycles = q->time;
! /* And a jsr to 0xc4 is turned into a magic trap. */
if (dst->opcode == O (O_JSR, SB))
{
! if (dst->src.literal == 0xc4)
{
dst->opcode = O (O_SYSCALL, SB);
}
}
dst->next_pc = addr + len / 2;
--- 455,475 ----
dst->opcode = q->how;
dst->cycles = q->time;
! /* And a jsr to these locations are turned into magic
! traps. */
if (dst->opcode == O (O_JSR, SB))
{
! switch (dst->src.literal)
{
+ case 0xc4:
dst->opcode = O (O_SYSCALL, SB);
+ break;
+ case 0xcc:
+ dst->opcode = O (O_SYS_CMDLINE, SB);
+ break;
}
+ /* End of Processing for system calls. */
}
dst->next_pc = addr + len / 2;
*************** sim_resume (SIM_DESC sd, int step, int s
*** 1389,1394 ****
--- 1404,1562 ----
sim_callback->write_stdout (sim_callback, &c, 1);
}
goto next;
+ + /* Trap for Command Line setup. */
+ case O (O_SYS_CMDLINE, SB):
+ {
+ int i = 0; /* Loop counter. */
+ int j = 0; /* Loop counter. */
+ int ind_arg_len = 0; /* Length of each argument. */
+ int no_of_args = 0; /* The no. or cmdline args. */
+ int current_location = 0; /* Location of string. */
+ int old_sp = 0; /* The Initial Stack Pointer. */
+ int no_of_slots = 0; /* No. of slots required on the stack
+ for storing cmdline args. */
+ int sp_move = 0; /* No. of locations by which the stack needs
+ to grow. */
+ int new_sp = 0; /* The final stack pointer location passed
+ back. */
+ int *argv_ptrs; /* Pointers of argv strings to be stored. */
+ int argv_ptrs_location = 0; /* Location of pointers to cmdline
+ args on the stack. */
+ int char_ptr_size = 0; /* Size of a character pointer on
+ target machine. */
+ int addr_cmdline = 0; /* Memory location where cmdline has
+ to be stored. */
+ int size_cmdline = 0; /* Size of cmdline. */
+ + /* Set the address of 256 free locations where command line is
+ stored. */
+ SET_CMDLINE_LOCATION
+ + cpu.regs[0] = addr_cmdline;
+ + /* Counting the no. of commandline arguments. */
+ for (i = 0; ptr_CommandLine[i] != NULL; i++)
+ continue;
+ + /* No. of arguments in the command line. */
+ no_of_args = i;
+ + /* Current location is just a temporary variable,which we are
+ setting to the point to the start of our commandline string. */
+ current_location = addr_cmdline;
+ + /* Allocating space for storing pointers of the command line
+ arguments. */
+ argv_ptrs = (int *) malloc (sizeof (int) * no_of_args);
+ + /* Setting char_ptr_size to the sizeof (char *) on the different
+ architectures. */
+ if (h8300hmode || h8300smode)
+ {
+ char_ptr_size = 4;
+ }
+ else
+ {
+ char_ptr_size = 2;
+ }
+ + for (i = 0; i < no_of_args; i++)
+ {
+ ind_arg_len = 0;
+ + /* The size of the commandline argument. */
+ ind_arg_len = (strlen (ptr_CommandLine[i]) + 1);
+ + /* The total size of the command line string. */
+ size_cmdline += ind_arg_len;
+ + /* As we have only 256 bytes, we need to provide a graceful
+ exit. Anyways, a program using command line arguments + where we cannot store all the command line arguments
+ given may behave unpredictably. */
+ if (size_cmdline >= 256)
+ {
+ cpu.regs[0] = 0;
+ goto next;
+ }
+ else
+ {
+ /* current_location points to the memory where the next
+ commandline argument is stored. */
+ argv_ptrs[i] = current_location;
+ for (j = 0; j < ind_arg_len; j++)
+ {
+ SET_MEMORY_B ((current_location +
+ (sizeof (char) * j)),
+ *(ptr_CommandLine[i] + + sizeof (char) * j));
+ }
+ + /* Setting current_location to the starting of next
+ argument. */
+ current_location += ind_arg_len;
+ }
+ }
+ + /* This is the original position of the stack pointer. */
+ old_sp = cpu.regs[7];
+ + /* We need space from the stack to store the pointers to argvs. */
+ /* As we will infringe on the stack, we need to shift the stack
+ pointer so that the data is not overwritten. We calculate how
+ much space is required. */
+ sp_move = (no_of_args) * (char_ptr_size);
+ + /* The final position of stack pointer, we have thus taken some
+ space from the stack. */
+ new_sp = old_sp - sp_move;
+ + /* Temporary variable holding value where the argv pointers need
+ to be stored. */
+ argv_ptrs_location = new_sp;
+ + /* The argv pointers are stored at sequential locations. As per
+ the H8300 ABI. */
+ for (i = 0; i < no_of_args; i++)
+ {
+ /* Saving the argv pointer. */
+ if (h8300hmode || h8300smode)
+ {
+ SET_MEMORY_L (argv_ptrs_location, argv_ptrs[i]);
+ }
+ else
+ {
+ SET_MEMORY_W (argv_ptrs_location, argv_ptrs[i]);
+ }
+
+ /* The next location where the pointer to the next argv
+ string has to be stored. */ + argv_ptrs_location += char_ptr_size;
+ }
+ + /* Required by POSIX, Setting 0x0 at the end of the list of argv
+ pointers. */
+ if (h8300hmode || h8300smode)
+ {
+ SET_MEMORY_L (old_sp, 0x0);
+ }
+ else
+ {
+ SET_MEMORY_W (old_sp, 0x0);
+ }
+ + /* Freeing allocated memory. */
+ free (argv_ptrs);
+ + /* The no. of argv arguments are returned in Reg 0. */
+ cpu.regs[0] = no_of_args;
+ /* The Pointer to argv in Register 1. */
+ cpu.regs[1] = new_sp;
+ /* Setting the stack pointer to the new value. */
+ cpu.regs[7] = new_sp;
+ }
+ goto next;
ONOT (O_NOT, rd = ~rd; v = 0;);
OSHIFTS (O_SHLL,
*************** sim_create_inferior (SIM_DESC sd, struct
*** 2231,2236 ****
--- 2399,2411 ----
cpu.pc = bfd_get_start_address (abfd);
else
cpu.pc = 0;
+ + /* Command Line support. */
+ if (argv != NULL)
+ {
+ ptr_CommandLine = argv;
+ }
+ return SIM_RC_OK;
}



------------------------------------------------------------------------


2003-02-22 D.Venkatasubramanian <dvenkat at noida dot hcltech dot com>

	* h8300.h: Added a pseudo opcodes for Commandline
	processing.



------------------------------------------------------------------------

2003-02-22 D.Venkatasubramanian <dvenkat at noida dot hcltech dot com>

	* h8300.h: Added a pseudo opcodes for Commandline
	processing.

2003-02-22 D.Venkatasubramanian <dvenkat at noida dot hcltech dot com>

* libc/include/sys/h8300hms/crt0.S: Added an extra instruction enclosed by SIMULATOR_ARGV_SUPPORT macro for Commandline support when target is simulator.
* configure.host: Added -DSIMULATOR_ARGV_SUPPORT for
Commandline Support.


2003-02-22 D.Venkatasubramanian <dvenkat at noida dot hcltech dot com>

* compile.c: Added #define SET_CMDLINE_LOCATION to set the location of 8-bit (256 locations) where the
Command Line arguments would be stored.
(decode): Added a TRAP to 0xcc for Commandline processing using pseudo opcode O_SYS_CMDLINE.
(sim_resume): Added handling of O_SYS_CMDLINE Trap.
(sim_create_inferior): Setting a pointer to Commandline Args array.
* inst.h: Added a new variable ptr_CommandLine for
storing pointer to Commandline array.



------------------------------------------------------------------------


*** include/opcode/h8300.h.original Wed Feb 19 16:09:36 2003
--- include/opcode/h8300.h.modified Sat Feb 22 19:52:15 2003
*************** struct h8_opcode *** 311,316 ****
--- 311,319 ----
#define O_SYS_STAT 106
#define O_SYS_FSTAT 107
/* End of System Call specific Changes. */
+ /* For Command Line Processing. */
+ #define O_SYS_CMDLINE 120
+ #define SB 0
#define SW 1
#define SL 2
*** newlib/configure.host.original Wed Feb 19 15:53:53 2003
--- newlib/configure.host.modified Sat Feb 22 19:51:13 2003
*************** case "${host}" in
*** 482,487 ****
--- 482,489 ----
h8300*-*-*)
syscall_dir=syscalls
newlib_cflags="${newlib_cflags} -DSMALL_DTOA -DSMALL_MEMORY"
+ # Command Line support for H8300
+ newlib_cflags="${newlib_cflags} -DSIMULATOR_ARGV_SUPPORT"
;;
h8500-*-*)
syscall_dir=syscalls
*** newlib/libc/sys/h8300hms/crt0.S.original Wed Feb 19 15:51:31 2003
--- newlib/libc/sys/h8300hms/crt0.S.modified Sat Feb 22 19:51:35 2003
*************** _start:
*** 16,23 ****
--- 16,29 ----
#ifdef __ELF__
mov.l #__fini,r0
jsr @_atexit
+ #ifdef SIMULATOR_ARGV_SUPPORT
+ jsr @0xcc
+ #endif
jsr @__init
#else
+ #ifdef SIMULATOR_ARGV_SUPPORT
+ jsr @0xcc
+ #endif
jsr @___main
#endif
jsr @_main
*************** _start:
*** 45,52 ****
--- 51,64 ----
#ifdef __ELF__
mov.l #__fini,r0
jsr @_atexit
+ #ifdef SIMULATOR_ARGV_SUPPORT
+ jsr @0xcc
+ #endif
jsr @__init
#else
+ #ifdef SIMULATOR_ARGV_SUPPORT
+ jsr @0xcc
+ #endif
jsr @___main
#endif
jsr @_main
*************** _start:
*** 74,81 ****
--- 86,99 ----
#ifdef __ELF__
mov.l #__fini,r0
jsr @_atexit
+ #ifdef SIMULATOR_ARGV_SUPPORT
+ jsr @0xcc
+ #endif
jsr @__init
#else
+ #ifdef SIMULATOR_ARGV_SUPPORT
+ jsr @0xcc
+ #endif
jsr @___main
#endif
jsr @_main
*** sim/h8300/inst.h.original Wed Feb 19 15:28:24 2003
--- sim/h8300/inst.h.modified Sat Feb 22 19:50:51 2003
*************** enum h8300_sim_state {
*** 66,71 ****
--- 66,74 ----
SIM_STATE_RUNNING, SIM_STATE_EXITED, SIM_STATE_SIGNALLED, SIM_STATE_STOPPED
};
+ /* For Command Line. */
+ char **ptr_CommandLine; /* Pointer to command Line Arguments. */
+ typedef struct
{
enum h8300_sim_state state;
*** sim/h8300/compile.c.original Wed Feb 19 15:28:09 2003
--- sim/h8300/compile.c.modified Sat Feb 22 19:50:43 2003
*************** void sim_set_simcache_size PARAMS ((int)
*** 119,124 ****
--- 119,132 ----
#define UEXTSHORT(x) ((x) & 0xffff)
#define SEXTSHORT(x) ((short) (x))
+ #define SET_CMDLINE_LOCATION \
+ if (h8300smode) \
+ addr_cmdline = 0xffff00L; \
+ else if (h8300hmode) \
+ addr_cmdline = 0x2ff00L; \
+ else \
+ addr_cmdline = 0xff00L;
+ static cpu_state_type cpu;
int h8300hmode = 0;
*************** decode (int addr, unsigned char *data, d
*** 447,460 ****
dst->opcode = q->how;
dst->cycles = q->time;
! /* And a jsr to 0xc4 is turned into a magic trap. */
if (dst->opcode == O (O_JSR, SB))
{
! if (dst->src.literal == 0xc4)
{
dst->opcode = O (O_SYSCALL, SB);
}
}
dst->next_pc = addr + len / 2;
--- 455,475 ----
dst->opcode = q->how;
dst->cycles = q->time;
! /* And a jsr to these locations are turned into magic
! traps. */
if (dst->opcode == O (O_JSR, SB))
{
! switch (dst->src.literal)
{
+ case 0xc4:
dst->opcode = O (O_SYSCALL, SB);
+ break;
+ case 0xcc:
+ dst->opcode = O (O_SYS_CMDLINE, SB);
+ break;
}
+ /* End of Processing for system calls. */
}
dst->next_pc = addr + len / 2;
*************** sim_resume (SIM_DESC sd, int step, int s
*** 1389,1394 ****
--- 1404,1562 ----
sim_callback->write_stdout (sim_callback, &c, 1);
}
goto next;
+ + /* Trap for Command Line setup. */
+ case O (O_SYS_CMDLINE, SB):
+ {
+ int i = 0; /* Loop counter. */
+ int j = 0; /* Loop counter. */
+ int ind_arg_len = 0; /* Length of each argument. */
+ int no_of_args = 0; /* The no. or cmdline args. */
+ int current_location = 0; /* Location of string. */
+ int old_sp = 0; /* The Initial Stack Pointer. */
+ int no_of_slots = 0; /* No. of slots required on the stack
+ for storing cmdline args. */
+ int sp_move = 0; /* No. of locations by which the stack needs
+ to grow. */
+ int new_sp = 0; /* The final stack pointer location passed
+ back. */
+ int *argv_ptrs; /* Pointers of argv strings to be stored. */
+ int argv_ptrs_location = 0; /* Location of pointers to cmdline
+ args on the stack. */
+ int char_ptr_size = 0; /* Size of a character pointer on
+ target machine. */
+ int addr_cmdline = 0; /* Memory location where cmdline has
+ to be stored. */
+ int size_cmdline = 0; /* Size of cmdline. */
+ + /* Set the address of 256 free locations where command line is
+ stored. */
+ SET_CMDLINE_LOCATION
+ + cpu.regs[0] = addr_cmdline;
+ + /* Counting the no. of commandline arguments. */
+ for (i = 0; ptr_CommandLine[i] != NULL; i++)
+ continue;
+ + /* No. of arguments in the command line. */
+ no_of_args = i;
+ + /* Current location is just a temporary variable,which we are
+ setting to the point to the start of our commandline string. */
+ current_location = addr_cmdline;
+ + /* Allocating space for storing pointers of the command line
+ arguments. */
+ argv_ptrs = (int *) malloc (sizeof (int) * no_of_args);
+ + /* Setting char_ptr_size to the sizeof (char *) on the different
+ architectures. */
+ if (h8300hmode || h8300smode)
+ {
+ char_ptr_size = 4;
+ }
+ else
+ {
+ char_ptr_size = 2;
+ }
+ + for (i = 0; i < no_of_args; i++)
+ {
+ ind_arg_len = 0;
+ + /* The size of the commandline argument. */
+ ind_arg_len = (strlen (ptr_CommandLine[i]) + 1);
+ + /* The total size of the command line string. */
+ size_cmdline += ind_arg_len;
+ + /* As we have only 256 bytes, we need to provide a graceful
+ exit. Anyways, a program using command line arguments + where we cannot store all the command line arguments
+ given may behave unpredictably. */
+ if (size_cmdline >= 256)
+ {
+ cpu.regs[0] = 0;
+ goto next;
+ }
+ else
+ {
+ /* current_location points to the memory where the next
+ commandline argument is stored. */
+ argv_ptrs[i] = current_location;
+ for (j = 0; j < ind_arg_len; j++)
+ {
+ SET_MEMORY_B ((current_location +
+ (sizeof (char) * j)),
+ *(ptr_CommandLine[i] + + sizeof (char) * j));
+ }
+ + /* Setting current_location to the starting of next
+ argument. */
+ current_location += ind_arg_len;
+ }
+ }
+ + /* This is the original position of the stack pointer. */
+ old_sp = cpu.regs[7];
+ + /* We need space from the stack to store the pointers to argvs. */
+ /* As we will infringe on the stack, we need to shift the stack
+ pointer so that the data is not overwritten. We calculate how
+ much space is required. */
+ sp_move = (no_of_args) * (char_ptr_size);
+ + /* The final position of stack pointer, we have thus taken some
+ space from the stack. */
+ new_sp = old_sp - sp_move;
+ + /* Temporary variable holding value where the argv pointers need
+ to be stored. */
+ argv_ptrs_location = new_sp;
+ + /* The argv pointers are stored at sequential locations. As per
+ the H8300 ABI. */
+ for (i = 0; i < no_of_args; i++)
+ {
+ /* Saving the argv pointer. */
+ if (h8300hmode || h8300smode)
+ {
+ SET_MEMORY_L (argv_ptrs_location, argv_ptrs[i]);
+ }
+ else
+ {
+ SET_MEMORY_W (argv_ptrs_location, argv_ptrs[i]);
+ }
+
+ /* The next location where the pointer to the next argv
+ string has to be stored. */ + argv_ptrs_location += char_ptr_size;
+ }
+ + /* Required by POSIX, Setting 0x0 at the end of the list of argv
+ pointers. */
+ if (h8300hmode || h8300smode)
+ {
+ SET_MEMORY_L (old_sp, 0x0);
+ }
+ else
+ {
+ SET_MEMORY_W (old_sp, 0x0);
+ }
+ + /* Freeing allocated memory. */
+ free (argv_ptrs);
+ + /* The no. of argv arguments are returned in Reg 0. */
+ cpu.regs[0] = no_of_args;
+ /* The Pointer to argv in Register 1. */
+ cpu.regs[1] = new_sp;
+ /* Setting the stack pointer to the new value. */
+ cpu.regs[7] = new_sp;
+ }
+ goto next;
ONOT (O_NOT, rd = ~rd; v = 0;);
OSHIFTS (O_SHLL,
*************** sim_create_inferior (SIM_DESC sd, struct
*** 2231,2236 ****
--- 2399,2411 ----
cpu.pc = bfd_get_start_address (abfd);
else
cpu.pc = 0;
+ + /* Command Line support. */
+ if (argv != NULL)
+ {
+ ptr_CommandLine = argv;
+ }
+ return SIM_RC_OK;
}



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]