main-dmain.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #! /bin/bash -v
  2. # main-dmain.sh
  3. # Usage: ./main-dmain.s
  4. # This version used symval+NNN(%rip) everywhere. If this is what is wanted
  5. # I obviously hope tha the compiler that generates mai.s will stick the
  6. # extra annotation in there!
  7. # Let me comment on these edits. Well firstly if all these work out I
  8. # hope that the compiler can be persuaded to generate the version that
  9. # these turn stuff into!
  10. # References to "symval+nnn" turn into "symval+nnn(%rip)" so as to use
  11. # pc relative addressing and make the code relocatable.
  12. # Ditto symfnc, and property list access would need the same if used.
  13. #
  14. # Comments are introduced by "#" not "/" which makes the code treat them
  15. # as comments on both a PC and a Mac
  16. #
  17. # "cmp lnnn" which compares against a local location turns into
  18. # "cmp lnnn(%rip)" to use pc-relative addressing.
  19. #
  20. # "cmp" turns into "cmpq" in a bunch of places where the Mac assembler
  21. # failed to deduce the appropriate width of operand.
  22. #
  23. # ".quad [a+b]" turns into ".quad (a+b)" and friends again for pc/Mac
  24. # compatibility.
  25. #
  26. # lnnnn => Gnnnn which I find more reaable. I do not use Lnnnn because
  27. # symbols starting with L are reserved as "local" bt at least some assemblers.
  28. #
  29. # For every label that I declare .globl and every label that I set I will
  30. # set user both a version with and without a leading underscore. For
  31. # refernces as between assembly code I will use the versions without the
  32. # underscore, but the ones with are so that C code on Cygwin and Macintosh
  33. # can access the names. The intent here is to create code that can assemble
  34. # and be used by any of those platforms. The key problem that arises
  35. # is that this leaves calls to C code from here (and in particular to
  36. # the C library) done via names with leading underscores. So on Linux I
  37. # need to provide stubs of the form
  38. # int _foo(int x) { return foo(x); }
  39. # for every function called from this assemble-coded part of the kernel.
  40. #
  41. # I want to put data most data that is in main.s into the .data segment.
  42. # the utility fudgedata tries to do that.
  43. # Calling conventions on Linux and Cygwin differ so I MUST start with
  44. # different versions of main... That will mean that the assembly code
  45. # I try to use on Cygwin will tend to expect the Windows API to be used
  46. # for everything. Well this fact should help me in a move towards supporting
  47. # the native Windows version.
  48. # For now at least I will use the same base version for both Linux and
  49. # Macintosh - I may in due course find that I need to split there too.
  50. #
  51. # This script will make win-main.s and linux-main.s (and similarly for dmain)
  52. case `uname -s` in
  53. *Darwin*)
  54. # On the Macintosh I need to use the GNU version of sed.
  55. SED=gsed
  56. ;;
  57. *Cygwin* | *CYGWIN*)
  58. SED=sed
  59. ;;
  60. *)
  61. SED=sed
  62. mc=AMD64_ext
  63. ;;
  64. esac
  65. gcc fudgedata.c -o fudgedata
  66. action() {
  67. $SED -e 's/symval+[0-9]*/&(%rip)/g' \
  68. -e 's/symfnc+[0-9]*/&(%rip)/g' \
  69. -e 's/mov l[0-9]*/&(%rip)/g' \
  70. -e 's+^/+#+g' \
  71. -e 's/cmp l[0-9]*/&(%rip)/g' \
  72. -e 's/cmp \$/cmpq $/g' \
  73. -e 's/\[/(/g' \
  74. -e 's/\]/)/g' \
  75. -e 's/l\([0-9][0-9][0-9][0-9]\)/G\1/g' \
  76. -e 's/[a-zA-Z_][a-zA-Z_0-9]*:/&\n_&/g' \
  77. -e 's/ \.globl .*$/&\n@@@&/g' \
  78. -e 's/@@@ .globl / .globl _/g' \
  79. -e 's/call [a-z]/call _&/g; s/_call /_/g' \
  80. -e 's/\.quad \(.*\)$\n#/.QUAD \1\n#/g' \
  81. < ../psl/dist/kernel/$1/main.s > mainx.s
  82. ./fudgedata mainx.s $2.s
  83. # dmain.s is simpler.
  84. $SED -e 's/\[/(/g' \
  85. -e 's/\]/)/g' \
  86. -e 's/l\([0-9][0-9][0-9][0-9]\)/G\1/g' \
  87. -e 's/[a-zA-Z_][a-zA-Z_0-9]*:/&\n_&/g' \
  88. -e 's/ \.globl .*$/&\n@@@&/g' \
  89. -e 's/@@@ .globl / .globl _/g' \
  90. -e 's/\.comm[ ]*\(.*\)$/.comm _\1/' \
  91. -e 's/.quad stack/.quad _stack/g' \
  92. -e 's/.quad argumentblock/.quad _argumentblock/g' \
  93. -e 's/.quad tokenbuffer/.quad _tokenbuffer/g' \
  94. -e 's/.quad bndstk/.quad _bndstk/g' \
  95. -e 's/.quad catchstack/.quad _catchstack/g' \
  96. -e 's/.quad hashtable/.quad _hashtable/g' \
  97. -e 's/.quad onewordbuffer/.quad _onewordbuffer/g' \
  98. -e 's/.quad saveargc/.quad _saveargc/g' \
  99. -e 's/.quad saveargv/.quad _saveargv/g' \
  100. -e 's/.quad datebuffer/.quad _datebuffer/g' \
  101. < ../psl/dist/kernel/$1/dmain.s > $3.s
  102. }
  103. action mingw-w64 win-main win-dmain
  104. action AMD64_ext linux-main linux-dmain
  105. # end of file