crt1.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017,2018,2019,2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. * Copyright © 2019,2020 Danny Milosavljevic <dannym@scratchpost.org>
  5. *
  6. * This file is part of GNU Mes.
  7. *
  8. * GNU Mes is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * GNU Mes is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <mes/lib-mini.h>
  22. //int main (int argc, char *argv[], char *envp[]);
  23. /* Note: GCC automatically emits a preable in order to set up the
  24. frame pointer: "push {fp}" "add fp, sp, 0"
  25. */
  26. // *INDENT-OFF*
  27. #if !__TINYC__
  28. void
  29. _start ()
  30. {
  31. asm (
  32. "mov r0,#0\n\t"
  33. "mov %0,r0\n"
  34. : "=r" (__stdin)
  35. : //no inputs ""
  36. );
  37. asm (
  38. "mov r0,#1\n\t"
  39. "mov %0,r0\n"
  40. : "=r" (__stdout)
  41. : //no inputs ""
  42. );
  43. asm (
  44. "mov r0,#2\n\t"
  45. "mov %0,r0\n"
  46. : "=r" (__stderr)
  47. : //no inputs ""
  48. );
  49. /* environ = argv + argc + 1 */
  50. asm (
  51. "ldr r0,[fp,#4]\n\t" /* r0 = argc */
  52. "add r1,fp,#8\n\t" /* r1 = &argv[0] */
  53. "add r2,r0,#1\n\t" /* r2 = r0 + 1 */
  54. "lsl r2,#2\n\t" /* r2 = (r0 + 1) << 2 */
  55. "add r2,r2,r1\n\t" /* r2 = ((r0 + 1) << 2) + r1 */
  56. "push {r2}\n\t" /* envp */
  57. "push {r1}\n\t" /* argv */
  58. "push {r0}\n\t" /* argc */
  59. "mov %0,r2\n\t"
  60. : "=r" (environ)
  61. : //no inputs ""
  62. );
  63. asm (
  64. "ldr r0,[sp]\n\t" /* argc */
  65. "ldr r1,[sp, #4]\n\t" /* argv */
  66. "ldr r2,[sp, #8]\n\t" /* envp */
  67. "bl main\n\t"
  68. "mov r7, #1\n\t"
  69. "swi #0\n\t"
  70. "wfi \n\t"
  71. );
  72. }
  73. #else //__TINYC__
  74. void
  75. _start ()
  76. {
  77. #if 0
  78. __asm__ (".int 0xe320f000\n"); //nop {0}
  79. __asm__ (".int 0xe320f000\n"); //nop {0}
  80. #endif
  81. int *in = &__stdin;
  82. int *out = &__stdout;
  83. int *err = &__stderr;
  84. char ***env = &environ;
  85. #if 0
  86. __asm__ (".int 0xe320f000\n"); //nop {0}
  87. __asm__ (".int 0xe320f000\n"); //nop {0}
  88. __asm__ (".int 0xe320f000\n"); //nop {0}
  89. *in = 0x22;
  90. __asm__ (".int 0xe320f000\n"); //nop {0}
  91. *out = 0x33;
  92. __asm__ (".int 0xe320f000\n"); //nop {0}
  93. *err = 0x44;
  94. __asm__ (".int 0xe320f000\n"); //nop {0}
  95. *env = 0x55;
  96. __asm__ (".int 0xe320f000\n"); //nop {0}
  97. #endif
  98. /* environ = argv + argc + 1 */
  99. __asm__ (".int 0xe59b000c\n"); //ldr r0, [fp, #12]
  100. __asm__ (".int 0xe28b1010\n"); //add r1, fp, #16
  101. __asm__ (".int 0xe2802001\n"); //add r2, r0, #1
  102. __asm__ (".int 0xe1a02102\n"); //lsl r2, r2, #2
  103. __asm__ (".int 0xe0822001\n"); //add r2, r2, r1
  104. // setup argc, argv, envp parameters on stack
  105. __asm__ (".int 0xe52d2004\n"); //push {r2} ; (str r2, [sp, #-4]!)
  106. __asm__ (".int 0xe52d1004\n"); //push {r1} ; (str r1, [sp, #-4]!)
  107. __asm__ (".int 0xe52d0004\n"); //push {r0} ; (str r0, [sp, #-4]!)
  108. __asm__ (".int 0xe1a02002\n"); //mov r2, r2
  109. // *in = 0;
  110. __asm__ (".int 0xe3a01000\n"); //mov r1, #0
  111. __asm__ (".int 0xe51b0004\n"); //ldr r0, [fp, #-4]
  112. __asm__ (".int 0xe5801000\n"); //str r1, [r0]
  113. __asm__ (".int 0xe320f000\n"); //nop {0}
  114. // *out = 1;
  115. __asm__ (".int 0xe3a01001\n"); //mov r1, #1
  116. __asm__ (".int 0xe51b0008\n"); //ldr r0, [fp, #-8]
  117. __asm__ (".int 0xe5801000\n"); //str r1, [r0]
  118. __asm__ (".int 0xe320f000\n"); //nop {0}
  119. // *err = 2;
  120. __asm__ (".int 0xe3a01002\n"); //mov r1, #2
  121. __asm__ (".int 0xe51b000c\n"); //ldr r0, [fp, #-12]
  122. __asm__ (".int 0xe5801000\n"); //str r1, [r0]
  123. __asm__ (".int 0xe320f000\n"); //nop {0}
  124. // *env = [sp, #8]
  125. __asm__ (".int 0xe59d1008\n"); //ldr r1, [sp, #8]
  126. __asm__ (".int 0xe51b0010\n"); //ldr r0, [fp, #-16]
  127. __asm__ (".int 0xe5801000\n"); //str r1, [r0]
  128. __asm__ (".int 0xe320f000\n"); //nop {0}
  129. // setup argc, argv, envp parameters in registers
  130. __asm__ (".int 0xe59d0000\n"); //ldr r0, [sp]
  131. __asm__ (".int 0xe59d1004\n"); //ldr r1, [sp, #4]
  132. __asm__ (".int 0xe59d2008\n"); //ldr r2, [sp, #8]
  133. main ();
  134. __asm__ (".int 0xe3a07001\n"); //mov r7, #1
  135. __asm__ (".int 0xef000000\n"); //svc 0x00000000
  136. __asm__ (".int 0xe320f003\n"); //wfi
  137. __asm__ (".int 0xe320f000\n"); //nop {0}
  138. }
  139. #endif //__TINYC__