2
0

asmutils.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. {
  2. Copyright (c) 1998-2006 by Florian Klaempfl
  3. This unit contains utility functions for assembler output
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit asmutils;
  18. interface
  19. {$i fpcdefs.inc}
  20. uses
  21. globtype,
  22. aasmbase,
  23. aasmdata;
  24. function emit_ansistring_const(list:TAsmList;data:PChar;len:LongInt;encoding:tstringencoding;NewSection:Boolean=True):TAsmLabel;
  25. function emit_unicodestring_const(list:TAsmList;data:Pointer;encoding:tstringencoding;Winlike:Boolean):TAsmLabel;
  26. implementation
  27. uses
  28. globals,
  29. systems,
  30. verbose,
  31. aasmtai,
  32. widestr,
  33. symdef;
  34. function emit_ansistring_const(list:TAsmList;data:PChar;len:LongInt;encoding:tstringencoding;NewSection:Boolean): TAsmLabel;
  35. var
  36. referencelab: TAsmLabel;
  37. s: PChar;
  38. begin
  39. current_asmdata.getdatalabel(result);
  40. if NewSection then
  41. new_section(list,sec_rodata,result.name,const_align(sizeof(pint)));
  42. referencelab := nil;
  43. if target_info.system in systems_darwin then
  44. begin
  45. current_asmdata.getdatalabel(referencelab);
  46. list.concat(tai_label.create(referencelab));
  47. end;
  48. list.concat(tai_const.create_16bit(encoding));
  49. list.concat(tai_const.create_16bit(1));
  50. {$ifdef cpu64bitaddr}
  51. { dummy for alignment }
  52. list.concat(tai_const.create_32bit(0));
  53. {$endif cpu64bitaddr}
  54. list.concat(tai_const.create_pint(-1));
  55. list.concat(tai_const.create_pint(len));
  56. { make sure the string doesn't get dead stripped if the header is referenced }
  57. if target_info.system in systems_darwin then
  58. list.concat(tai_directive.create(asd_reference,result.name));
  59. list.concat(tai_label.create(result));
  60. { and vice versa }
  61. if target_info.system in systems_darwin then
  62. list.concat(tai_directive.create(asd_reference,referencelab.name));
  63. getmem(s,len+1);
  64. move(data^,s^,len);
  65. s[len]:=#0;
  66. list.concat(tai_string.create_pchar(s,len+1)); { terminating zero included }
  67. end;
  68. function emit_unicodestring_const(list:TAsmList;data:Pointer;encoding:tstringencoding;Winlike:Boolean):TAsmLabel;
  69. var
  70. referencelab: TAsmLabel;
  71. i, strlength: SizeInt;
  72. begin
  73. current_asmdata.getdatalabel(result);
  74. new_section(list,sec_rodata,result.name,const_align(sizeof(pint)));
  75. referencelab := nil;
  76. if target_info.system in systems_darwin then
  77. begin
  78. current_asmdata.getdatalabel(referencelab);
  79. list.concat(tai_label.create(referencelab));
  80. end;
  81. strlength := getlengthwidestring(pcompilerwidestring(data));
  82. if Winlike then
  83. list.concat(Tai_const.Create_32bit(strlength*cwidechartype.size))
  84. else
  85. begin
  86. list.concat(tai_const.create_16bit(encoding));
  87. list.concat(tai_const.create_16bit(2));
  88. {$ifdef cpu64bitaddr}
  89. { dummy for alignment }
  90. list.concat(Tai_const.Create_32bit(0));
  91. {$endif cpu64bitaddr}
  92. list.concat(Tai_const.Create_pint(-1));
  93. list.concat(Tai_const.Create_pint(strlength));
  94. end;
  95. { make sure the string doesn't get dead stripped if the header is referenced }
  96. if (target_info.system in systems_darwin) then
  97. list.concat(tai_directive.create(asd_reference,result.name));
  98. list.concat(Tai_label.Create(result));
  99. { ... and vice versa }
  100. if (target_info.system in systems_darwin) then
  101. list.concat(tai_directive.create(asd_reference,referencelab.name));
  102. if cwidechartype.size = 2 then
  103. begin
  104. for i:=0 to strlength-1 do
  105. list.concat(Tai_const.Create_16bit(pcompilerwidestring(data)^.data[i]));
  106. { ending #0 }
  107. list.concat(Tai_const.Create_16bit(0));
  108. end
  109. else
  110. InternalError(200904271); { codegeneration for other sizes must be written }
  111. end;
  112. end.