asmutils.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. aasmbase,
  22. aasmdata;
  23. function emit_ansistring_const(list:TAsmList;data:PChar;len:LongInt;NewSection:Boolean=True):TAsmLabel;
  24. function emit_unicodestring_const(list:TAsmList;data:Pointer;Winlike:Boolean):TAsmLabel;
  25. implementation
  26. uses
  27. globals,
  28. globtype,
  29. systems,
  30. verbose,
  31. aasmtai,
  32. widestr,
  33. symdef;
  34. function emit_ansistring_const(list:TAsmList;data:PChar;len:LongInt;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_pint(-1));
  49. list.concat(tai_const.create_pint(len));
  50. { make sure the string doesn't get dead stripped if the header is referenced }
  51. if target_info.system in systems_darwin then
  52. list.concat(tai_directive.create(asd_reference,result.name));
  53. list.concat(tai_label.create(result));
  54. { and vice versa }
  55. if target_info.system in systems_darwin then
  56. list.concat(tai_directive.create(asd_reference,referencelab.name));
  57. getmem(s,len+1);
  58. move(data^,s^,len);
  59. s[len]:=#0;
  60. list.concat(tai_string.create_pchar(s,len+1)); { terminating zero included }
  61. end;
  62. function emit_unicodestring_const(list:TAsmList;data:Pointer;Winlike:Boolean):TAsmLabel;
  63. var
  64. referencelab: TAsmLabel;
  65. i, strlength: SizeInt;
  66. begin
  67. current_asmdata.getdatalabel(result);
  68. new_section(list,sec_rodata,result.name,const_align(sizeof(pint)));
  69. referencelab := nil;
  70. if target_info.system in systems_darwin then
  71. begin
  72. current_asmdata.getdatalabel(referencelab);
  73. list.concat(tai_label.create(referencelab));
  74. end;
  75. strlength := getlengthwidestring(pcompilerwidestring(data));
  76. if Winlike then
  77. list.concat(Tai_const.Create_32bit(strlength*cwidechartype.size))
  78. else
  79. begin
  80. list.concat(Tai_const.Create_pint(-1));
  81. list.concat(Tai_const.Create_pint(strlength*cwidechartype.size));
  82. end;
  83. { make sure the string doesn't get dead stripped if the header is referenced }
  84. if (target_info.system in systems_darwin) then
  85. list.concat(tai_directive.create(asd_reference,result.name));
  86. list.concat(Tai_label.Create(result));
  87. { ... and vice versa }
  88. if (target_info.system in systems_darwin) then
  89. list.concat(tai_directive.create(asd_reference,referencelab.name));
  90. if cwidechartype.size = 2 then
  91. begin
  92. for i:=0 to strlength-1 do
  93. list.concat(Tai_const.Create_16bit(pcompilerwidestring(data)^.data[i]));
  94. { ending #0 }
  95. list.concat(Tai_const.Create_16bit(0));
  96. end
  97. else
  98. InternalError(200904271); { codegeneration for other sizes must be written }
  99. end;
  100. end.