Patches "-mpreferred-stack-boundary" for gcc-4.1.0

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Patches "-mpreferred-stack-boundary" for gcc-4.1.0

Post by hlide »

Wintermute PMs me :
A couple of devkitPSP users have been asking about getting a version of the toolchain which includes your preferred stack boundary patch. I noticed it's gone from the latest psp svn patches & I've been having a bit of trouble getting it to apply to 4.1.2.
Sure, there is some substantial changes between 4.0.x and 4.1.x to handle, but once the right patches are created, it is easy to add the option "-mpreferred-stack-boundary=n" now.

I decided to make it publicly here in case some other people may be interested.

We need to patch three files :
- mips.c.patch

Code: Select all

Index: mips.c
===================================================================
--- mips.c	(revision 13)
+++ mips.c	(working copy)
@@ -612,6 +612,8 @@
    should arrange to call mips32 hard floating point code.  */
 int mips16_hard_float;
 
+unsigned int mips_preferred_stack_boundary;
+unsigned int mips_preferred_stack_align;
 /* The architecture selected by -mipsN.  */
 static const struct mips_cpu_info *mips_isa_info;
 
@@ -5057,6 +5059,21 @@
       mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
     }
 
+  /* Validate -mpreferred-stack-boundary= value, or provide default.
+     The default of 128-bit is for newABI else 64-bit.  */
+  mips_preferred_stack_boundary = (TARGET_NEWABI ? 128 : 64);
+  mips_preferred_stack_align = (TARGET_NEWABI ? 16 : 8);
+  if (mips_preferred_stack_boundary_string)
+    {
+      i = atoi (mips_preferred_stack_boundary_string);
+      if &#40;i < 2 || i > 12&#41;
+	error &#40;"-mpreferred-stack-boundary=%d is not between 2 and 12", i&#41;;
+      else
+        &#123;
+          mips_preferred_stack_align = &#40;1 << i&#41;;
+          mips_preferred_stack_boundary = mips_preferred_stack_align * 8;
+        &#125;
+    &#125;
   /* Thread-local relocation operators.  */
   mips_lo_relocs&#91;SYMBOL_TLSGD&#93; = "%tlsgd&#40;";
   mips_lo_relocs&#91;SYMBOL_TLSLDM&#93; = "%tlsldm&#40;";
- mips.h.patch

Code: Select all

Index&#58; mips.h
===================================================================
--- mips.h	&#40;revision 13&#41;
+++ mips.h	&#40;working copy&#41;
@@ -1953,7 +1953,7 @@
    `current_function_outgoing_args_size'.  */
 #define OUTGOING_REG_PARM_STACK_SPACE
 
-#define STACK_BOUNDARY &#40;TARGET_NEWABI ? 128 &#58; 64&#41;
+#define STACK_BOUNDARY &#40;mips_preferred_stack_boundary&#41;
 
 #define RETURN_POPS_ARGS&#40;FUNDECL,FUNTYPE,SIZE&#41; 0
 
@@ -2109,7 +2109,7 @@
 /* Treat LOC as a byte offset from the stack pointer and round it up
    to the next fully-aligned offset.  */
 #define MIPS_STACK_ALIGN&#40;LOC&#41; \
-  &#40;TARGET_NEWABI ? &#40;&#40;LOC&#41; + 15&#41; & -16 &#58; &#40;&#40;LOC&#41; + 7&#41; & -8&#41;
+  &#40;&#40;LOC&#41; + &#40;mips_preferred_stack_align - 1&#41; & -&#40;mips_preferred_stack_align&#41;&#41;
 
 
 /* Implement `va_start' for varargs and stdarg.  */
@@ -2812,6 +2812,9 @@
 #endif
 #endif
 
+extern unsigned int mips_preferred_stack_boundary;
+extern unsigned int mips_preferred_stack_align;
+extern const char *mips_preferred_stack_boundary_string;
 #ifndef HAVE_AS_TLS
 #define HAVE_AS_TLS 0
 #endif
- mips.opt.patch (new file in 4.1.0)

Code: Select all

Index&#58; mips.opt
===================================================================
--- mips.opt	&#40;revision 13&#41;
+++ mips.opt	&#40;working copy&#41;
@@ -216,3 +216,7 @@
 mxgot
 Target Report Var&#40;TARGET_XGOT&#41;
 Lift restrictions on GOT size
+
+mpreferred-stack-boundary=
+Target RejectNegative Joined Var&#40;mips_preferred_stack_boundary_string&#41;
+Attempt to keep stack aligned to this power of 2
I successfully tested them with psp-gcc 4.1.0 (cygwin - psptoolchain).
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

If you provide a single patch against current SVN I could put that in the standard toolchain for you.
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

jimparis wrote:If you provide a single patch against current SVN I could put that in the standard toolchain for you.
this one ?

Code: Select all

Index&#58; gcc/config/mips/mips.c
===================================================================
--- gcc/config/mips/mips.c	&#40;revision 13&#41;
+++ gcc/config/mips/mips.c	&#40;working copy&#41;
@@ -612,6 +612,8 @@
    should arrange to call mips32 hard floating point code.  */
 int mips16_hard_float;
 
+unsigned int mips_preferred_stack_boundary;
+unsigned int mips_preferred_stack_align;
 /* The architecture selected by -mipsN.  */
 static const struct mips_cpu_info *mips_isa_info;
 
@@ -5057,6 +5059,21 @@
       mips_lo_relocs&#91;SYMBOL_GOTOFF_LOADGP&#93; = "%lo&#40;%neg&#40;%gp_rel&#40;";
     &#125;
 
+  /* Validate -mpreferred-stack-boundary= value, or provide default.
+     The default of 128-bit is for newABI else 64-bit.  */
+  mips_preferred_stack_boundary = &#40;TARGET_NEWABI ? 128 &#58; 64&#41;;
+  mips_preferred_stack_align = &#40;TARGET_NEWABI ? 16 &#58; 8&#41;;
+  if &#40;mips_preferred_stack_boundary_string&#41;
+    &#123;
+      i = atoi &#40;mips_preferred_stack_boundary_string&#41;;
+      if &#40;i < 4 || i > 12&#41;
+	error &#40;"-mpreferred-stack-boundary=%d is not between 4 and 12", i&#41;;
+      else
+        &#123;
+          mips_preferred_stack_align = &#40;1 << i&#41;;
+          mips_preferred_stack_boundary = mips_preferred_stack_align * 8;
+        &#125;
+    &#125;
   /* Thread-local relocation operators.  */
   mips_lo_relocs&#91;SYMBOL_TLSGD&#93; = "%tlsgd&#40;";
   mips_lo_relocs&#91;SYMBOL_TLSLDM&#93; = "%tlsldm&#40;";
Index&#58; gcc/config/mips/mips.h
===================================================================
--- gcc/config/mips/mips.h	&#40;revision 13&#41;
+++ gcc/config/mips/mips.h	&#40;working copy&#41;
@@ -1953,7 +1953,7 @@
    `current_function_outgoing_args_size'.  */
 #define OUTGOING_REG_PARM_STACK_SPACE
 
-#define STACK_BOUNDARY &#40;TARGET_NEWABI ? 128 &#58; 64&#41;
+#define STACK_BOUNDARY &#40;mips_preferred_stack_boundary&#41;
 
 #define RETURN_POPS_ARGS&#40;FUNDECL,FUNTYPE,SIZE&#41; 0
 
@@ -2109,7 +2109,7 @@
 /* Treat LOC as a byte offset from the stack pointer and round it up
    to the next fully-aligned offset.  */
 #define MIPS_STACK_ALIGN&#40;LOC&#41; \
-  &#40;TARGET_NEWABI ? &#40;&#40;LOC&#41; + 15&#41; & -16 &#58; &#40;&#40;LOC&#41; + 7&#41; & -8&#41;
+  &#40;&#40;LOC&#41; + &#40;mips_preferred_stack_align - 1&#41; & -&#40;mips_preferred_stack_align&#41;&#41;
 
 
 /* Implement `va_start' for varargs and stdarg.  */
@@ -2812,6 +2812,9 @@
 #endif
 #endif
 
+extern unsigned int mips_preferred_stack_boundary;
+extern unsigned int mips_preferred_stack_align;
+extern const char *mips_preferred_stack_boundary_string;
 #ifndef HAVE_AS_TLS
 #define HAVE_AS_TLS 0
 #endif
Index&#58; gcc/config/mips/mips.opt
===================================================================
--- gcc/config/mips/mips.opt	&#40;revision 13&#41;
+++ gcc/config/mips/mips.opt	&#40;working copy&#41;
@@ -216,3 +216,7 @@
 mxgot
 Target Report Var&#40;TARGET_XGOT&#41;
 Lift restrictions on GOT size
+
+mpreferred-stack-boundary=
+Target RejectNegative Joined Var&#40;mips_preferred_stack_boundary_string&#41;
+Attempt to keep stack aligned to this power of 2
I'm using TortoiseSVN to create this patch. I don't know how you make yours (supposedly you want to have it in gcc-4.1.0-PSP.patch). I can also email you this patch (apparently this patch has some page jump characters I cannot remove without breaking the patch) if you want something more solid (if so, just PM me where I must send it)
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

Well, I'd need something that I can apply with "patch" directly to a checked out copy of svn://svn.pspdev.org/psp/trunk/psptoolchain. There is no "Ygcc/config/mips/mips.c " in the repository (and it's definitely not revision 13, we're at 2366 or so).

Yeah, it would probably patch the file gcc-4.1.0-PSP.patch.
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

jimparis wrote:Well, I'd need something that I can apply with "patch" directly to a checked out copy of svn://svn.pspdev.org/psp/trunk/psptoolchain. There is no "Ygcc/config/mips/mips.c " in the repository (and it's definitely not revision 13, we're at 2366 or so).

Yeah, it would probably patch the file gcc-4.1.0-PSP.patch.
directly to modify this patch to include mine may be hazardous.

the only thing i can see is to have two directories on a top directory : one with the original gcc (without any PSP patches) named gcc-4.1.0 and one named gcc-psp with gcc-4.1.0-PSP.patch AND my patch applied. Once done, create a newer gcc-4.1.0-PSP.patch from diff of the tops of those directories, probably with : "diff -burN gcc-4.1.0 gcc-psp" on the top directory of both gcc directories.

I'll try it when i'm back at home tonight.
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

Yep that sounds about right. It's a pain but that's why I asked you to do it instead of me :) (plus I'm not set up to test that it still works correctly at the moment)
Heimdall
Posts: 245
Joined: Thu Nov 10, 2005 1:29 am
Location: Netherlands
Contact:

Post by Heimdall »

By the time of writing of this post i'm uploading a SDK build with your patch. Since I haven't tested the new feature I've created a folder called experimental at:

http://www.box.net/shared/syudygao00

Please see if the compiler works as expected (it builds OK all the templates) but they do not use the new compiler option.

If everything is alright I'll move the SDK to the parent folder bumping the version from 0.6 to 0.6.1.
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

First test : is the new option not rejected by gcc ? FAILURE
------ Rebuild All started: Project: hurrican, Configuration: Debug Win32 ------
Performing Makefile project actions
d:/dev/pspsdk/bin/rm -f hurrican.prx hurrican.elf main.o PARAM.SFO EBOOT.PBP EBOOT.PBP
psp-g++ -I. -Id:/dev/pspsdk/psp/sdk/include -O3 -G0 -Wall -mpreferred-stack-boundary=12 -fno-exceptions -fno-rtti -D_PSP_FW_VERSION=150 -c -o main.o main.cpp
cc1plus.exe: error: unrecognized command line option "-mpreferred-stack-boundary=12"
make: *** [main.o] Error 1
Build log was saved at "file://d:\dev\hurrican\dists\psp\Debug\BuildLog.htm"
hurrican - 1 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
I also used -v to check if the right gcc was called and it was the case.
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

jimparis wrote:Yep that sounds about right. It's a pain but that's why I asked you to do it instead of me :) (plus I'm not set up to test that it still works correctly at the moment)
new gcc-4.1.0.patch at http://hlide.free.fr/download/PSP/gcc-4.1.0-PSP.patch.

EDIT: i applied this patch on a non psp gcc-4.1.0, then copied the three files mips.c/h/opt on my psptoolchain\build\gcc-4.1.0\build-psp\gcc\config\mips directory so i can build it quickly (just removing mips.o is enough to make it recompile).

Once installed, i tried -mpreferred-stack-boundary=12 and get the stack base in functions prologs and epilogs to be 4096-byte aligned, which is correct.
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

Committed to revision 2367, thanks!
Heimdall
Posts: 245
Joined: Thu Nov 10, 2005 1:29 am
Location: Netherlands
Contact:

Post by Heimdall »

My bad, I forgot to make clean ;) before!

0.6.2 includes the official patch and accepts the parameter.
Post Reply