raatt.pas 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Does the parsing for the GAS styled inline assembler.
  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 raatt;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cclasses,
  23. { global }
  24. globtype,
  25. { aasm }
  26. cpubase,cpuinfo,aasmbase,aasmtai,aasmdata,aasmcpu,
  27. { assembler reader }
  28. rabase,
  29. rasm,
  30. rautils,
  31. { symtable }
  32. symconst,
  33. { cg }
  34. cgbase;
  35. type
  36. tasmtoken = (
  37. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  38. AS_REALNUM,AS_COMMA,AS_LPAREN,
  39. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  40. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,AS_DOLLAR,
  41. AS_HASH,AS_LSBRACKET,AS_RSBRACKET,AS_LBRACKET,AS_RBRACKET,
  42. AS_EQUAL,
  43. {------------------ Assembler directives --------------------}
  44. AS_DB,AS_DW,AS_DD,AS_DQ,AS_GLOBAL,
  45. AS_ALIGN,AS_BALIGN,AS_P2ALIGN,AS_ASCII,
  46. AS_ASCIIZ,AS_LCOMM,AS_COMM,AS_SINGLE,AS_DOUBLE,AS_EXTENDED,AS_CEXTENDED,
  47. AS_DATA,AS_TEXT,AS_INIT,AS_FINI,AS_RVA,
  48. AS_SET,AS_WEAK,AS_SECTION,AS_END,
  49. {------------------ Assembler Operators --------------------}
  50. AS_TYPE,AS_SIZEOF,AS_VMTOFFSET,AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR,AS_NOR,AS_AT,
  51. AS_RELTYPE, // common token for relocation types
  52. {------------------ Target-specific directive ---------------}
  53. AS_TARGET_DIRECTIVE
  54. );
  55. tasmkeyword = string[10];
  56. const
  57. { These tokens should be modified accordingly to the modifications }
  58. { in the different enumerations. }
  59. firstdirective = AS_DB;
  60. lastdirective = AS_END;
  61. token2str : array[tasmtoken] of tasmkeyword=(
  62. '','Label','LLabel','string','integer',
  63. 'float',',','(',
  64. ')',':','.','+','-','*',
  65. ';','identifier','register','opcode','/','$',
  66. '#','{','}','[',']',
  67. '=',
  68. '.byte','.word','.long','.quad','.globl',
  69. '.align','.balign','.p2align','.ascii',
  70. '.asciz','.lcomm','.comm','.single','.double','.tfloat','.tcfloat',
  71. '.data','.text','.init','.fini','.rva',
  72. '.set','.weak','.section','END',
  73. 'TYPE','SIZEOF','VMTOFFSET','%','<<','>>','!','&','|','^','~','@','reltype',
  74. 'directive');
  75. type
  76. tattreader = class(tasmreader)
  77. actasmtoken : tasmtoken;
  78. prevasmtoken : tasmtoken;
  79. procedure SetupTables;
  80. procedure BuildConstant(constsize: byte);
  81. procedure BuildConstantOperand(oper : toperand);
  82. procedure BuildRealConstant(typ : tfloattype);
  83. procedure BuildStringConstant(asciiz: boolean);
  84. procedure BuildRva;
  85. procedure BuildRecordOffsetSize(const expr: string;var offset:tcgint;var size:tcgint; var mangledname: string; needvmtofs: boolean);
  86. procedure BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:tcgint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  87. function BuildConstExpression(allowref,betweenbracket:boolean): tcgint;
  88. function Assemble: tlinkedlist;override;
  89. procedure handleopcode;virtual;abstract;
  90. function is_asmopcode(const s: string) : boolean;virtual;abstract;
  91. Function is_asmdirective(const s: string):boolean;
  92. function is_register(const s:string):boolean;virtual;
  93. function is_locallabel(const s: string):boolean;
  94. function is_targetdirective(const s: string): boolean;virtual;
  95. procedure GetToken;
  96. function consume(t : tasmtoken):boolean;
  97. procedure RecoverConsume(allowcomma:boolean);
  98. procedure handlepercent;virtual;
  99. procedure handledollar;virtual;
  100. procedure HandleTargetDirective;virtual;
  101. end;
  102. implementation
  103. uses
  104. { globals }
  105. verbose,systems,
  106. { input }
  107. scanner, pbase,
  108. { symtable }
  109. symbase,symtype,symsym,symdef,symtable,
  110. {$ifdef x86}
  111. rax86,
  112. {$endif x86}
  113. itcpugas,
  114. procinfo;
  115. procedure tattreader.SetupTables;
  116. var
  117. i : tasmop;
  118. Begin
  119. iasmops:=TFPHashList.create;
  120. for i:=firstop to lastop do
  121. iasmops.Add(upper(gas_op2str[i]),Pointer(PtrInt(i)));
  122. end;
  123. function tattreader.is_asmdirective(const s: string):boolean;
  124. var
  125. i : tasmtoken;
  126. hs : string;
  127. Begin
  128. { GNU as is also not casesensitive with this }
  129. hs:=lower(s);
  130. for i:=firstdirective to lastdirective do
  131. if hs=token2str[i] then
  132. begin
  133. actasmtoken:=i;
  134. is_asmdirective:=true;
  135. exit;
  136. end;
  137. is_asmdirective:=false;
  138. end;
  139. function tattreader.is_register(const s:string):boolean;
  140. begin
  141. is_register:=false;
  142. actasmregister:=gas_regnum_search(lower(s));
  143. if actasmregister<>NR_NO then
  144. begin
  145. is_register:=true;
  146. actasmtoken:=AS_REGISTER;
  147. end;
  148. end;
  149. function tattreader.is_locallabel(const s: string):boolean;
  150. begin
  151. is_locallabel:=(length(s)>=2) and (s[1]='.') and (s[2]='L');
  152. end;
  153. procedure tattreader.handledollar;
  154. begin
  155. c:=current_scanner.asmgetchar;
  156. actasmtoken:=AS_DOLLAR;
  157. end;
  158. procedure tattreader.handlepercent;
  159. begin
  160. c:=current_scanner.asmgetchar;
  161. actasmtoken:=AS_MOD;
  162. end;
  163. function tattreader.is_targetdirective(const s: string): boolean;
  164. begin
  165. result:=false;
  166. end;
  167. procedure tattreader.handletargetdirective;
  168. begin
  169. end;
  170. procedure tattreader.GetToken;
  171. var
  172. len : longint;
  173. srsym : tsym;
  174. srsymtable : TSymtable;
  175. begin
  176. c:=scanner.c;
  177. { save old token and reset new token }
  178. prevasmtoken:=actasmtoken;
  179. actasmtoken:=AS_NONE;
  180. { reset }
  181. actasmpattern:='';
  182. { while space and tab , continue scan... }
  183. while c in [' ',#9] do
  184. c:=current_scanner.asmgetchar;
  185. { get token pos }
  186. if not (c in [#10,#13,'{',';','/','(']) then
  187. current_scanner.gettokenpos;
  188. { Local Label, Label, Directive, Prefix or Opcode }
  189. if firsttoken and not(c in [#10,#13,'{',';','/','(']) then
  190. begin
  191. firsttoken:=FALSE;
  192. len:=0;
  193. { directive or local label }
  194. if c = '.' then
  195. begin
  196. inc(len);
  197. actasmpattern[len]:=c;
  198. { Let us point to the next character }
  199. c:=current_scanner.asmgetchar;
  200. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  201. begin
  202. inc(len);
  203. actasmpattern[len]:=c;
  204. c:=current_scanner.asmgetchar;
  205. end;
  206. actasmpattern[0]:=chr(len);
  207. { this is a local label... }
  208. if (c=':') and is_locallabel(actasmpattern) then
  209. Begin
  210. { local variables are case sensitive }
  211. actasmtoken:=AS_LLABEL;
  212. c:=current_scanner.asmgetchar;
  213. firsttoken:=true;
  214. exit;
  215. end
  216. { must be a directive }
  217. else
  218. Begin
  219. { directives are case sensitive!! }
  220. if is_asmdirective(actasmpattern) then
  221. exit;
  222. if is_targetdirective(actasmpattern) then
  223. begin
  224. actasmtoken:=AS_TARGET_DIRECTIVE;
  225. exit;
  226. end;
  227. Message1(asmr_e_not_directive_or_local_symbol,actasmpattern);
  228. end;
  229. end;
  230. { only opcodes and global labels are allowed now. }
  231. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  232. begin
  233. inc(len);
  234. actasmpattern[len]:=c;
  235. c:=current_scanner.asmgetchar;
  236. end;
  237. actasmpattern[0]:=chr(len);
  238. { Label ? }
  239. if c = ':' then
  240. begin
  241. actasmtoken:=AS_LABEL;
  242. { let us point to the next character }
  243. c:=current_scanner.asmgetchar;
  244. firsttoken:=true;
  245. exit;
  246. end;
  247. {$if defined(POWERPC) or defined(POWERPC64)}
  248. { some PowerPC instructions can have the postfix -, + or .
  249. this code could be moved to is_asmopcode but I think
  250. it's better to ifdef it here (FK)
  251. }
  252. case c of
  253. '.', '-', '+':
  254. begin
  255. actasmpattern:=actasmpattern+c;
  256. c:=current_scanner.asmgetchar;
  257. end
  258. end;
  259. {$endif POWERPC}
  260. {$if defined(ARM)}
  261. {
  262. Thumb-2 instructions can have a .W postfix to indicate 32bit instructions,
  263. Also in unified syntax sizes and types are indicated with something like a .<dt> prefix for example
  264. }
  265. case c of
  266. '.':
  267. begin
  268. if len>1 then
  269. begin
  270. while c in ['A'..'Z','a'..'z','0'..'9','_','.'] do
  271. begin
  272. inc(len);
  273. actasmpattern[len]:=c;
  274. c:=current_scanner.asmgetchar;
  275. end;
  276. actasmpattern[0]:=chr(len);
  277. end;
  278. {actasmpattern:=actasmpattern+c;
  279. c:=current_scanner.asmgetchar;
  280. if upcase(c) = 'W' then
  281. begin
  282. actasmpattern:=actasmpattern+c;
  283. c:=current_scanner.asmgetchar;
  284. end
  285. else
  286. internalerror(2010122301);}
  287. end
  288. end;
  289. {$endif ARM}
  290. {$ifdef aarch64}
  291. { b.cond }
  292. case c of
  293. '.':
  294. begin
  295. repeat
  296. actasmpattern:=actasmpattern+c;
  297. c:=current_scanner.asmgetchar;
  298. until not(c in ['a'..'z','A'..'Z']);
  299. end;
  300. end;
  301. {$endif aarch64}
  302. {$if defined(riscv32) or defined(riscv64)}
  303. {
  304. amo* instructions contain a postfix with size, and optionally memory ordering
  305. fence* can contain memory type identifier
  306. floating point instructions contain size, and optionally rounding mode
  307. }
  308. case c of
  309. '.':
  310. begin
  311. repeat
  312. actasmpattern:=actasmpattern+c;
  313. c:=current_scanner.asmgetchar;
  314. until not(c in ['a'..'z','A'..'Z','.']);
  315. end;
  316. end;
  317. {$endif riscv}
  318. { Opcode ? }
  319. If is_asmopcode(upper(actasmpattern)) then
  320. Begin
  321. uppervar(actasmpattern);
  322. exit;
  323. end;
  324. { End of assemblerblock ? }
  325. if upper(actasmpattern) = 'END' then
  326. begin
  327. actasmtoken:=AS_END;
  328. exit;
  329. end;
  330. message1(asmr_e_unknown_opcode,actasmpattern);
  331. actasmtoken:=AS_NONE;
  332. end
  333. else { else firsttoken }
  334. { Here we must handle all possible cases }
  335. begin
  336. case c of
  337. '.' : { possiblities : - local label reference , such as in jmp @local1 }
  338. { - field of object/record }
  339. { - directive. }
  340. begin
  341. if (prevasmtoken in [AS_ID,AS_RPAREN]) then
  342. begin
  343. c:=current_scanner.asmgetchar;
  344. actasmtoken:=AS_DOT;
  345. exit;
  346. end;
  347. actasmpattern:=c;
  348. c:=current_scanner.asmgetchar;
  349. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  350. begin
  351. actasmpattern:=actasmpattern + c;
  352. c:=current_scanner.asmgetchar;
  353. end;
  354. if is_asmdirective(actasmpattern) then
  355. exit;
  356. if is_targetdirective(actasmpattern) then
  357. begin
  358. actasmtoken:=AS_TARGET_DIRECTIVE;
  359. exit;
  360. end;
  361. { local label references and directives }
  362. { are case sensitive }
  363. actasmtoken:=AS_ID;
  364. exit;
  365. end;
  366. { identifier, register, prefix or directive }
  367. '_','A'..'Z','a'..'z':
  368. begin
  369. len:=0;
  370. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  371. begin
  372. inc(len);
  373. actasmpattern[len]:=c;
  374. c:=current_scanner.asmgetchar;
  375. end;
  376. actasmpattern[0]:=chr(len);
  377. uppervar(actasmpattern);
  378. {$ifdef x86}
  379. { only x86 architectures have instruction prefixes }
  380. { Opcode, can only be when the previous was a prefix }
  381. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  382. Begin
  383. uppervar(actasmpattern);
  384. exit;
  385. end;
  386. {$endif x86}
  387. { check for end which is a reserved word unlike the opcodes }
  388. if actasmpattern = 'END' then
  389. Begin
  390. actasmtoken:=AS_END;
  391. exit;
  392. end;
  393. if actasmpattern = 'TYPE' then
  394. Begin
  395. actasmtoken:=AS_TYPE;
  396. exit;
  397. end;
  398. if actasmpattern = 'SIZEOF' then
  399. Begin
  400. actasmtoken:=AS_SIZEOF;
  401. exit;
  402. end;
  403. if actasmpattern = 'VMTOFFSET' then
  404. Begin
  405. actasmtoken:=AS_VMTOFFSET;
  406. exit;
  407. end;
  408. if is_register(actasmpattern) then
  409. begin
  410. actasmtoken:=AS_REGISTER;
  411. exit;
  412. end;
  413. { if next is a '.' and this is a unitsym then we also need to
  414. parse the identifier }
  415. if (c='.') then
  416. begin
  417. searchsym(actasmpattern,srsym,srsymtable);
  418. if assigned(srsym) and
  419. (srsym.typ=unitsym) and
  420. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  421. srsym.owner.iscurrentunit then
  422. begin
  423. actasmpattern:=actasmpattern+c;
  424. c:=current_scanner.asmgetchar;
  425. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  426. begin
  427. actasmpattern:=actasmpattern + upcase(c);
  428. c:=current_scanner.asmgetchar;
  429. end;
  430. end;
  431. end;
  432. actasmtoken:=AS_ID;
  433. exit;
  434. end;
  435. '%' : { register or modulo }
  436. handlepercent;
  437. '1'..'9': { integer number }
  438. begin
  439. len:=0;
  440. while c in ['0'..'9'] do
  441. Begin
  442. inc(len);
  443. actasmpattern[len]:=c;
  444. c:=current_scanner.asmgetchar;
  445. end;
  446. actasmpattern[0]:=chr(len);
  447. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  448. actasmtoken:=AS_INTNUM;
  449. exit;
  450. end;
  451. '0' : { octal,hexa,real or binary number. }
  452. begin
  453. actasmpattern:=c;
  454. c:=current_scanner.asmgetchar;
  455. case upcase(c) of
  456. 'B': { binary }
  457. Begin
  458. c:=current_scanner.asmgetchar;
  459. while c in ['0','1'] do
  460. Begin
  461. actasmpattern:=actasmpattern + c;
  462. c:=current_scanner.asmgetchar;
  463. end;
  464. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  465. actasmtoken:=AS_INTNUM;
  466. exit;
  467. end;
  468. 'D': { real }
  469. Begin
  470. c:=current_scanner.asmgetchar;
  471. { get ridd of the 0d }
  472. if (c in ['+','-']) then
  473. begin
  474. actasmpattern:=c;
  475. c:=current_scanner.asmgetchar;
  476. end
  477. else
  478. actasmpattern:='';
  479. while c in ['0'..'9'] do
  480. Begin
  481. actasmpattern:=actasmpattern + c;
  482. c:=current_scanner.asmgetchar;
  483. end;
  484. if c='.' then
  485. begin
  486. actasmpattern:=actasmpattern + c;
  487. c:=current_scanner.asmgetchar;
  488. while c in ['0'..'9'] do
  489. Begin
  490. actasmpattern:=actasmpattern + c;
  491. c:=current_scanner.asmgetchar;
  492. end;
  493. if upcase(c) = 'E' then
  494. begin
  495. actasmpattern:=actasmpattern + c;
  496. c:=current_scanner.asmgetchar;
  497. if (c in ['+','-']) then
  498. begin
  499. actasmpattern:=actasmpattern + c;
  500. c:=current_scanner.asmgetchar;
  501. end;
  502. while c in ['0'..'9'] do
  503. Begin
  504. actasmpattern:=actasmpattern + c;
  505. c:=current_scanner.asmgetchar;
  506. end;
  507. end;
  508. actasmtoken:=AS_REALNUM;
  509. exit;
  510. end
  511. else
  512. begin
  513. Message1(asmr_e_invalid_float_const,actasmpattern+c);
  514. actasmtoken:=AS_NONE;
  515. end;
  516. end;
  517. 'X': { hexadecimal }
  518. Begin
  519. c:=current_scanner.asmgetchar;
  520. while c in ['0'..'9','a'..'f','A'..'F'] do
  521. Begin
  522. actasmpattern:=actasmpattern + c;
  523. c:=current_scanner.asmgetchar;
  524. end;
  525. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  526. actasmtoken:=AS_INTNUM;
  527. exit;
  528. end;
  529. '1'..'7': { octal }
  530. begin
  531. actasmpattern:=actasmpattern + c;
  532. while c in ['0'..'7'] do
  533. Begin
  534. actasmpattern:=actasmpattern + c;
  535. c:=current_scanner.asmgetchar;
  536. end;
  537. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  538. actasmtoken:=AS_INTNUM;
  539. exit;
  540. end;
  541. else { octal number zero value...}
  542. Begin
  543. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  544. actasmtoken:=AS_INTNUM;
  545. exit;
  546. end;
  547. end; { end case }
  548. end;
  549. '&' :
  550. begin
  551. c:=current_scanner.asmgetchar;
  552. actasmtoken:=AS_AND;
  553. end;
  554. '''' : { char }
  555. begin
  556. actasmpattern:='';
  557. repeat
  558. c:=current_scanner.asmgetchar;
  559. case c of
  560. '\' :
  561. begin
  562. { copy also the next char so \" is parsed correctly }
  563. actasmpattern:=actasmpattern+c;
  564. c:=current_scanner.asmgetchar;
  565. actasmpattern:=actasmpattern+c;
  566. end;
  567. '''' :
  568. begin
  569. c:=current_scanner.asmgetchar;
  570. break;
  571. end;
  572. #10,#13:
  573. Message(scan_f_string_exceeds_line);
  574. else
  575. actasmpattern:=actasmpattern+c;
  576. end;
  577. until false;
  578. actasmpattern:=EscapeToPascal(actasmpattern);
  579. actasmtoken:=AS_STRING;
  580. exit;
  581. end;
  582. '"' : { string }
  583. begin
  584. actasmpattern:='';
  585. repeat
  586. c:=current_scanner.asmgetchar;
  587. case c of
  588. '\' :
  589. begin
  590. { copy also the next char so \" is parsed correctly }
  591. actasmpattern:=actasmpattern+c;
  592. c:=current_scanner.asmgetchar;
  593. actasmpattern:=actasmpattern+c;
  594. end;
  595. '"' :
  596. begin
  597. c:=current_scanner.asmgetchar;
  598. break;
  599. end;
  600. #10,#13:
  601. Message(scan_f_string_exceeds_line);
  602. else
  603. actasmpattern:=actasmpattern+c;
  604. end;
  605. until false;
  606. actasmpattern:=EscapeToPascal(actasmpattern);
  607. actasmtoken:=AS_STRING;
  608. exit;
  609. end;
  610. '$' :
  611. begin
  612. handledollar;
  613. exit;
  614. end;
  615. '#' :
  616. begin
  617. actasmtoken:=AS_HASH;
  618. c:=current_scanner.asmgetchar;
  619. exit;
  620. end;
  621. '[' :
  622. begin
  623. actasmtoken:=AS_LBRACKET;
  624. c:=current_scanner.asmgetchar;
  625. exit;
  626. end;
  627. ']' :
  628. begin
  629. actasmtoken:=AS_RBRACKET;
  630. c:=current_scanner.asmgetchar;
  631. exit;
  632. end;
  633. '{' :
  634. begin
  635. {$ifdef arm}
  636. // the arm assembler uses { ... } for register sets
  637. // but compiler directives {$... } are still allowed
  638. c:=current_scanner.asmgetchar;
  639. if c<>'$' then
  640. actasmtoken:=AS_LSBRACKET
  641. else
  642. begin
  643. current_scanner.skipcomment(false);
  644. GetToken;
  645. end;
  646. {$else arm}
  647. current_scanner.skipcomment(true);
  648. GetToken;
  649. {$endif arm}
  650. exit;
  651. end;
  652. {$ifdef arm}
  653. '}' :
  654. begin
  655. actasmtoken:=AS_RSBRACKET;
  656. c:=current_scanner.asmgetchar;
  657. exit;
  658. end;
  659. '=' :
  660. begin
  661. actasmtoken:=AS_EQUAL;
  662. c:=current_scanner.asmgetchar;
  663. exit;
  664. end;
  665. {$endif arm}
  666. ',' :
  667. begin
  668. actasmtoken:=AS_COMMA;
  669. c:=current_scanner.asmgetchar;
  670. exit;
  671. end;
  672. '<' :
  673. begin
  674. actasmtoken:=AS_SHL;
  675. c:=current_scanner.asmgetchar;
  676. if c = '<' then
  677. c:=current_scanner.asmgetchar;
  678. exit;
  679. end;
  680. '>' :
  681. begin
  682. actasmtoken:=AS_SHL;
  683. c:=current_scanner.asmgetchar;
  684. if c = '>' then
  685. c:=current_scanner.asmgetchar;
  686. exit;
  687. end;
  688. '|' :
  689. begin
  690. actasmtoken:=AS_OR;
  691. c:=current_scanner.asmgetchar;
  692. exit;
  693. end;
  694. '^' :
  695. begin
  696. actasmtoken:=AS_XOR;
  697. c:=current_scanner.asmgetchar;
  698. exit;
  699. end;
  700. '(' :
  701. begin
  702. c:=current_scanner.asmgetchar;
  703. if c='*' then
  704. begin
  705. current_scanner.skipoldtpcomment(true);
  706. GetToken;
  707. end
  708. else
  709. actasmtoken:=AS_LPAREN;
  710. exit;
  711. end;
  712. ')' :
  713. begin
  714. actasmtoken:=AS_RPAREN;
  715. c:=current_scanner.asmgetchar;
  716. exit;
  717. end;
  718. ':' :
  719. begin
  720. actasmtoken:=AS_COLON;
  721. c:=current_scanner.asmgetchar;
  722. exit;
  723. end;
  724. '+' :
  725. begin
  726. actasmtoken:=AS_PLUS;
  727. c:=current_scanner.asmgetchar;
  728. exit;
  729. end;
  730. '-' :
  731. begin
  732. actasmtoken:=AS_MINUS;
  733. c:=current_scanner.asmgetchar;
  734. exit;
  735. end;
  736. '*' :
  737. begin
  738. actasmtoken:=AS_STAR;
  739. c:=current_scanner.asmgetchar;
  740. exit;
  741. end;
  742. '/' :
  743. begin
  744. c:=current_scanner.asmgetchar;
  745. if c='/' then
  746. begin
  747. current_scanner.skipdelphicomment;
  748. GetToken;
  749. end
  750. else
  751. actasmtoken:=AS_SLASH;
  752. exit;
  753. end;
  754. '!', '~' :
  755. begin
  756. actasmtoken:=AS_NOT;
  757. c:=current_scanner.asmgetchar;
  758. exit;
  759. end;
  760. '@' :
  761. begin
  762. actasmtoken:=AS_AT;
  763. c:=current_scanner.asmgetchar;
  764. exit;
  765. end;
  766. #13,#10:
  767. begin
  768. current_scanner.linebreak;
  769. c:=current_scanner.asmgetchar;
  770. firsttoken:=TRUE;
  771. actasmtoken:=AS_SEPARATOR;
  772. exit;
  773. end;
  774. ';' :
  775. begin
  776. c:=current_scanner.asmgetchar;
  777. firsttoken:=TRUE;
  778. actasmtoken:=AS_SEPARATOR;
  779. exit;
  780. end;
  781. else
  782. current_scanner.illegal_char(c);
  783. end;
  784. end;
  785. end;
  786. function tattreader.consume(t : tasmtoken):boolean;
  787. begin
  788. Consume:=true;
  789. if t<>actasmtoken then
  790. begin
  791. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  792. Consume:=false;
  793. end;
  794. repeat
  795. gettoken;
  796. until actasmtoken<>AS_NONE;
  797. end;
  798. procedure tattreader.RecoverConsume(allowcomma:boolean);
  799. begin
  800. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  801. begin
  802. if allowcomma and (actasmtoken=AS_COMMA) then
  803. break;
  804. Consume(actasmtoken);
  805. end;
  806. end;
  807. Procedure tattreader.BuildConstant(constsize: byte);
  808. var
  809. asmsymtyp : TAsmSymType;
  810. asmsym,
  811. expr: string;
  812. value : tcgint;
  813. Begin
  814. Repeat
  815. Case actasmtoken of
  816. AS_STRING:
  817. Begin
  818. expr:=actasmpattern;
  819. if length(expr) > 1 then
  820. Message(asmr_e_string_not_allowed_as_const);
  821. Consume(AS_STRING);
  822. Case actasmtoken of
  823. AS_COMMA: Consume(AS_COMMA);
  824. AS_END,
  825. AS_SEPARATOR: ;
  826. else
  827. Message(asmr_e_invalid_string_expression);
  828. end; { end case }
  829. ConcatString(curlist,expr);
  830. end;
  831. AS_INTNUM,
  832. AS_PLUS,
  833. AS_MINUS,
  834. AS_LPAREN,
  835. AS_TYPE,
  836. AS_SIZEOF,
  837. AS_NOT,
  838. AS_VMTOFFSET,
  839. AS_ID :
  840. Begin
  841. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  842. if asmsym<>'' then
  843. begin
  844. if constsize<>sizeof(pint) then
  845. Message(asmr_w_32bit_const_for_address);
  846. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value,constsize,true)
  847. end
  848. else
  849. ConcatConstant(curlist,value,constsize);
  850. end;
  851. AS_COMMA:
  852. Consume(AS_COMMA);
  853. AS_END,
  854. AS_SEPARATOR:
  855. break;
  856. else
  857. begin
  858. Message(asmr_e_syn_constant);
  859. RecoverConsume(false);
  860. end
  861. end; { end case }
  862. Until false;
  863. end;
  864. Procedure tattreader.BuildRealConstant(typ : tfloattype);
  865. var
  866. expr : string;
  867. r : bestreal;
  868. code : integer;
  869. negativ : boolean;
  870. errorflag: boolean;
  871. Begin
  872. errorflag:=FALSE;
  873. Repeat
  874. negativ:=false;
  875. expr:='';
  876. if actasmtoken=AS_PLUS then
  877. Consume(AS_PLUS)
  878. else
  879. if actasmtoken=AS_MINUS then
  880. begin
  881. negativ:=true;
  882. consume(AS_MINUS);
  883. end;
  884. Case actasmtoken of
  885. AS_INTNUM:
  886. Begin
  887. expr:=actasmpattern;
  888. Consume(AS_INTNUM);
  889. if negativ then
  890. expr:='-'+expr;
  891. val(expr,r,code);
  892. if code<>0 then
  893. Begin
  894. r:=0;
  895. Message(asmr_e_invalid_float_expr);
  896. End;
  897. ConcatRealConstant(curlist,r,typ);
  898. end;
  899. AS_REALNUM:
  900. Begin
  901. expr:=actasmpattern;
  902. Consume(AS_REALNUM);
  903. { in ATT syntax you have 0d in front of the real }
  904. { should this be forced ? yes i think so, as to }
  905. { conform to gas as much as possible. }
  906. if (expr[1]='0') and (upper(expr[2])='D') then
  907. Delete(expr,1,2);
  908. if negativ then
  909. expr:='-'+expr;
  910. val(expr,r,code);
  911. if code<>0 then
  912. Begin
  913. r:=0;
  914. Message(asmr_e_invalid_float_expr);
  915. End;
  916. ConcatRealConstant(curlist,r,typ);
  917. end;
  918. AS_COMMA:
  919. begin
  920. Consume(AS_COMMA);
  921. end;
  922. AS_END,
  923. AS_SEPARATOR:
  924. begin
  925. break;
  926. end;
  927. else
  928. Begin
  929. Consume(actasmtoken);
  930. if not errorflag then
  931. Message(asmr_e_invalid_float_expr);
  932. errorflag:=TRUE;
  933. end;
  934. end;
  935. Until false;
  936. end;
  937. Procedure tattreader.BuildStringConstant(asciiz: boolean);
  938. var
  939. expr: string;
  940. errorflag : boolean;
  941. Begin
  942. errorflag:=FALSE;
  943. Repeat
  944. Case actasmtoken of
  945. AS_STRING:
  946. Begin
  947. expr:=actasmpattern;
  948. if asciiz then
  949. expr:=expr+#0;
  950. ConcatString(curlist,expr);
  951. Consume(AS_STRING);
  952. end;
  953. AS_COMMA:
  954. begin
  955. Consume(AS_COMMA);
  956. end;
  957. AS_END,
  958. AS_SEPARATOR:
  959. begin
  960. break;
  961. end;
  962. else
  963. Begin
  964. Consume(actasmtoken);
  965. if not errorflag then
  966. Message(asmr_e_invalid_string_expression);
  967. errorflag:=TRUE;
  968. end;
  969. end;
  970. Until false;
  971. end;
  972. Function tattreader.Assemble: tlinkedlist;
  973. Var
  974. hl : tasmlabel;
  975. commname,
  976. symname,
  977. symval ,sectionname: string;
  978. lasTSec : TAsmSectiontype;
  979. l1,
  980. l2,
  981. symofs : tcgint;
  982. symtyp : TAsmsymtype;
  983. section : tai_section;
  984. secflags : TSectionFlags;
  985. secprogbits : TSectionProgbits;
  986. Begin
  987. Message1(asmr_d_start_reading,'GNU AS');
  988. firsttoken:=TRUE;
  989. { sets up all opcode and register tables in uppercase }
  990. if not _asmsorted then
  991. Begin
  992. SetupTables;
  993. _asmsorted:=TRUE;
  994. end;
  995. curlist:=TAsmList.Create;
  996. { we might need to know which parameters are passed in registers }
  997. if not parse_generic then
  998. current_procinfo.generate_parameter_info;
  999. lasTSec:=sec_code;
  1000. { start tokenizer }
  1001. gettoken;
  1002. { main loop }
  1003. repeat
  1004. case actasmtoken of
  1005. AS_LLABEL:
  1006. Begin
  1007. if CreateLocalLabel(actasmpattern,hl,true) then
  1008. ConcatLabel(curlist,hl);
  1009. Consume(AS_LLABEL);
  1010. end;
  1011. AS_LABEL:
  1012. Begin
  1013. if SearchLabel(upper(actasmpattern),hl,true) then
  1014. begin
  1015. if hl.is_public then
  1016. ConcatPublic(curlist,actasmpattern);
  1017. ConcatLabel(curlist,hl);
  1018. end
  1019. else
  1020. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1021. Consume(AS_LABEL);
  1022. end;
  1023. AS_DW:
  1024. Begin
  1025. Consume(AS_DW);
  1026. BuildConstant(2);
  1027. end;
  1028. AS_DATA:
  1029. Begin
  1030. new_section(curList,sec_data,lower(current_procinfo.procdef.mangledname),0);
  1031. lasTSec:=sec_data;
  1032. Consume(AS_DATA);
  1033. end;
  1034. AS_TEXT:
  1035. Begin
  1036. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1037. lasTSec:=sec_code;
  1038. Consume(AS_TEXT);
  1039. end;
  1040. AS_INIT:
  1041. Begin
  1042. new_section(curList,sec_init,lower(current_procinfo.procdef.mangledname),0);
  1043. lasTSec:=sec_init;
  1044. Consume(AS_INIT);
  1045. end;
  1046. AS_FINI:
  1047. Begin
  1048. new_section(curList,sec_fini,lower(current_procinfo.procdef.mangledname),0);
  1049. lasTSec:=sec_fini;
  1050. Consume(AS_FINI);
  1051. end;
  1052. AS_DB:
  1053. Begin
  1054. Consume(AS_DB);
  1055. BuildConstant(1);
  1056. end;
  1057. AS_DD:
  1058. Begin
  1059. Consume(AS_DD);
  1060. BuildConstant(4);
  1061. end;
  1062. AS_DQ:
  1063. Begin
  1064. Consume(AS_DQ);
  1065. {$ifdef cpu64bitaddr}
  1066. BuildConstant(8);
  1067. {$else cpu64bitaddr}
  1068. BuildRealConstant(s64comp);
  1069. {$endif cpu64bitaddr}
  1070. end;
  1071. AS_SINGLE:
  1072. Begin
  1073. Consume(AS_SINGLE);
  1074. BuildRealConstant(s32real);
  1075. end;
  1076. AS_DOUBLE:
  1077. Begin
  1078. Consume(AS_DOUBLE);
  1079. BuildRealConstant(s64real);
  1080. end;
  1081. AS_EXTENDED:
  1082. Begin
  1083. Consume(AS_EXTENDED);
  1084. BuildRealConstant(s80real);
  1085. end;
  1086. AS_CEXTENDED:
  1087. Begin
  1088. Consume(AS_CEXTENDED);
  1089. BuildRealConstant(sc80real);
  1090. end;
  1091. AS_GLOBAL:
  1092. Begin
  1093. Consume(AS_GLOBAL);
  1094. if actasmtoken=AS_ID then
  1095. ConcatPublic(curlist,actasmpattern);
  1096. Consume(AS_ID);
  1097. if actasmtoken<>AS_SEPARATOR then
  1098. Consume(AS_SEPARATOR);
  1099. end;
  1100. AS_ALIGN:
  1101. Begin
  1102. Consume(AS_ALIGN);
  1103. l1:=BuildConstExpression(false,false);
  1104. if (target_info.system in [system_i386_GO32V2]) then
  1105. begin
  1106. l2:=1;
  1107. if (l1>=0) and (l1<=16) then
  1108. while (l1>0) do
  1109. begin
  1110. l2:=2*l2;
  1111. dec(l1);
  1112. end;
  1113. l1:=l2;
  1114. end;
  1115. ConcatAlign(curlist,l1);
  1116. Message(asmr_n_align_is_target_specific);
  1117. if actasmtoken<>AS_SEPARATOR then
  1118. Consume(AS_SEPARATOR);
  1119. end;
  1120. AS_BALIGN:
  1121. Begin
  1122. Consume(AS_BALIGN);
  1123. ConcatAlign(curlist,BuildConstExpression(false,false));
  1124. if actasmtoken<>AS_SEPARATOR then
  1125. Consume(AS_SEPARATOR);
  1126. end;
  1127. AS_P2ALIGN:
  1128. Begin
  1129. Consume(AS_P2ALIGN);
  1130. l1:=BuildConstExpression(false,false);
  1131. l2:=1;
  1132. if (l1>=0) and (l1<=16) then
  1133. while (l1>0) do
  1134. begin
  1135. l2:=2*l2;
  1136. dec(l1);
  1137. end;
  1138. l1:=l2;
  1139. ConcatAlign(curlist,l1);
  1140. if actasmtoken<>AS_SEPARATOR then
  1141. Consume(AS_SEPARATOR);
  1142. end;
  1143. AS_ASCIIZ:
  1144. Begin
  1145. Consume(AS_ASCIIZ);
  1146. BuildStringConstant(TRUE);
  1147. end;
  1148. AS_ASCII:
  1149. Begin
  1150. Consume(AS_ASCII);
  1151. BuildStringConstant(FALSE);
  1152. end;
  1153. AS_LCOMM:
  1154. Begin
  1155. Consume(AS_LCOMM);
  1156. commname:=actasmpattern;
  1157. Consume(AS_ID);
  1158. Consume(AS_COMMA);
  1159. symofs:=BuildConstExpression(false,false);
  1160. curList.concat(Tai_datablock.Create(commname,symofs,carraydef.getreusable(u8inttype,symofs),AT_DATA));
  1161. if actasmtoken<>AS_SEPARATOR then
  1162. Consume(AS_SEPARATOR);
  1163. end;
  1164. AS_COMM:
  1165. Begin
  1166. Consume(AS_COMM);
  1167. commname:=actasmpattern;
  1168. Consume(AS_ID);
  1169. Consume(AS_COMMA);
  1170. symofs:=BuildConstExpression(false,false);
  1171. curList.concat(Tai_datablock.Create_global(commname,symofs,carraydef.getreusable(u8inttype,symofs),AT_DATA));
  1172. if actasmtoken<>AS_SEPARATOR then
  1173. Consume(AS_SEPARATOR);
  1174. end;
  1175. AS_OPCODE:
  1176. Begin
  1177. HandleOpCode;
  1178. end;
  1179. AS_SEPARATOR:
  1180. Begin
  1181. Consume(AS_SEPARATOR);
  1182. end;
  1183. AS_RVA:
  1184. begin
  1185. { .rva generally applies to systems with COFF output format,
  1186. not just Windows. }
  1187. if not (target_info.system in systems_all_windows) then
  1188. Message1(asmr_e_unsupported_directive,token2str[AS_RVA]);
  1189. Consume(AS_RVA);
  1190. BuildRva;
  1191. end;
  1192. AS_SET:
  1193. begin
  1194. Consume(AS_SET);
  1195. BuildConstSymbolExpression(true,false,false, symofs,symname,symtyp);
  1196. Consume(AS_COMMA);
  1197. BuildConstSymbolExpression(true,false,false, symofs,symval,symtyp);
  1198. curList.concat(tai_symbolpair.create(spk_set,symname,symval));
  1199. end;
  1200. AS_WEAK:
  1201. begin
  1202. Consume(AS_WEAK);
  1203. BuildConstSymbolExpression(true,false,false, l1,symname,symtyp);
  1204. { ideally, we should look up the symbol here (or later) and
  1205. depending on whether it's external or net, generate
  1206. asd_weak_reference or asd_weak_definition -- this is different
  1207. on some targets) }
  1208. curList.concat(tai_directive.create(asd_weak_definition,symname));
  1209. end;
  1210. AS_SECTION:
  1211. begin
  1212. Consume(AS_SECTION);
  1213. sectionname:=actasmpattern;
  1214. secflags:=[];
  1215. secprogbits:=SPB_None;
  1216. Consume(AS_STRING);
  1217. if actasmtoken=AS_COMMA then
  1218. begin
  1219. Consume(AS_COMMA);
  1220. if actasmtoken=AS_STRING then
  1221. begin
  1222. case actasmpattern of
  1223. 'a':
  1224. Include(secflags,SF_A);
  1225. 'w':
  1226. Include(secflags,SF_W);
  1227. 'x':
  1228. Include(secflags,SF_X);
  1229. '':
  1230. ;
  1231. else
  1232. Message(asmr_e_syntax_error);
  1233. end;
  1234. Consume(AS_STRING);
  1235. if actasmtoken=AS_COMMA then
  1236. begin
  1237. Consume(AS_COMMA);
  1238. if (actasmtoken=AS_MOD) or (actasmtoken=AS_AT) then
  1239. begin
  1240. Consume(actasmtoken);
  1241. if actasmtoken=AS_ID then
  1242. begin
  1243. case actasmpattern of
  1244. 'PROGBITS':
  1245. secprogbits:=SPB_PROGBITS;
  1246. 'NOBITS':
  1247. secprogbits:=SPB_NOBITS;
  1248. 'NOTE':
  1249. secprogbits:=SPB_NOTE;
  1250. else
  1251. Message(asmr_e_syntax_error);
  1252. end;
  1253. Consume(AS_ID);
  1254. end
  1255. else
  1256. Message(asmr_e_syntax_error);
  1257. end
  1258. else
  1259. Message(asmr_e_syntax_error);
  1260. end;
  1261. end
  1262. else
  1263. Message(asmr_e_syntax_error);
  1264. end;
  1265. //curList.concat(tai_section.create(sec_user, actasmpattern, 0));
  1266. section:=new_section(curlist, sec_user, sectionname, 0);
  1267. section.secflags:=secflags;
  1268. section.secprogbits:=secprogbits;
  1269. end;
  1270. AS_TARGET_DIRECTIVE:
  1271. HandleTargetDirective;
  1272. AS_END:
  1273. begin
  1274. break; { end assembly block }
  1275. end;
  1276. else
  1277. Begin
  1278. Message(asmr_e_syntax_error);
  1279. RecoverConsume(false);
  1280. end;
  1281. end;
  1282. until false;
  1283. { check that all referenced local labels are defined }
  1284. checklocallabels;
  1285. { are we back in the code section? }
  1286. if lasTSec<>sec_code then
  1287. begin
  1288. Message(asmr_w_assembler_code_not_returned_to_text);
  1289. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1290. end;
  1291. { Return the list in an asmnode }
  1292. assemble:=curlist;
  1293. Message1(asmr_d_finish_reading,'GNU AS');
  1294. end;
  1295. {*****************************************************************************
  1296. Parsing Helpers
  1297. *****************************************************************************}
  1298. Procedure tattreader.BuildRecordOffsetSize(const expr: string;var offset:tcgint;var size:tcgint; var mangledname: string; needvmtofs: boolean);
  1299. { Description: This routine builds up a record offset after a AS_DOT }
  1300. { token is encountered. }
  1301. { On entry actasmtoken should be equal to AS_DOT }
  1302. var
  1303. s : string;
  1304. hastypecast: boolean;
  1305. Begin
  1306. offset:=0;
  1307. size:=0;
  1308. s:=expr;
  1309. while (actasmtoken=AS_DOT) do
  1310. begin
  1311. Consume(AS_DOT);
  1312. { a record field could have the same name as a register }
  1313. if actasmtoken in [AS_ID,AS_REGISTER] then
  1314. begin
  1315. s:=s+'.'+actasmpattern;
  1316. consume(actasmtoken)
  1317. end
  1318. else
  1319. begin
  1320. Consume(AS_ID);
  1321. RecoverConsume(true);
  1322. break;
  1323. end;
  1324. end;
  1325. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs,hastypecast) then
  1326. Message(asmr_e_building_record_offset);
  1327. end;
  1328. procedure tattreader.BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:tcgint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  1329. var
  1330. hssymtyp : TAsmSymType;
  1331. hs,tempstr,expr,mangledname : string;
  1332. parenlevel : longint;
  1333. l,k : tcgint;
  1334. errorflag : boolean;
  1335. prevtok : tasmtoken;
  1336. sym : tsym;
  1337. srsymtable : TSymtable;
  1338. hl : tasmlabel;
  1339. Begin
  1340. asmsym:='';
  1341. asmsymtyp:=AT_DATA;
  1342. value:=0;
  1343. errorflag:=FALSE;
  1344. tempstr:='';
  1345. expr:='';
  1346. parenlevel:=0;
  1347. Repeat
  1348. Case actasmtoken of
  1349. AS_LPAREN:
  1350. Begin
  1351. { Exit if ref? }
  1352. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  1353. break;
  1354. Consume(AS_LPAREN);
  1355. expr:=expr + '(';
  1356. inc(parenlevel);
  1357. end;
  1358. AS_RBRACKET:
  1359. begin
  1360. if betweenbracket then
  1361. break;
  1362. { write error only once. }
  1363. if not errorflag then
  1364. Message(asmr_e_invalid_constant_expression);
  1365. { consume tokens until we find COMMA or SEPARATOR }
  1366. Consume(actasmtoken);
  1367. errorflag:=TRUE;
  1368. end;
  1369. AS_RPAREN:
  1370. Begin
  1371. { end of ref ? }
  1372. if (parenlevel=0) and betweenbracket then
  1373. break;
  1374. Consume(AS_RPAREN);
  1375. expr:=expr + ')';
  1376. dec(parenlevel);
  1377. end;
  1378. AS_SHL:
  1379. Begin
  1380. Consume(AS_SHL);
  1381. expr:=expr + '<';
  1382. end;
  1383. AS_SHR:
  1384. Begin
  1385. Consume(AS_SHR);
  1386. expr:=expr + '>';
  1387. end;
  1388. AS_SLASH:
  1389. Begin
  1390. Consume(AS_SLASH);
  1391. expr:=expr + '/';
  1392. end;
  1393. AS_MOD:
  1394. Begin
  1395. Consume(AS_MOD);
  1396. expr:=expr + '%';
  1397. end;
  1398. AS_STAR:
  1399. Begin
  1400. Consume(AS_STAR);
  1401. expr:=expr + '*';
  1402. end;
  1403. AS_PLUS:
  1404. Begin
  1405. Consume(AS_PLUS);
  1406. expr:=expr + '+';
  1407. end;
  1408. AS_MINUS:
  1409. Begin
  1410. Consume(AS_MINUS);
  1411. expr:=expr + '-';
  1412. end;
  1413. AS_AND:
  1414. Begin
  1415. Consume(AS_AND);
  1416. expr:=expr + '&';
  1417. end;
  1418. AS_NOT:
  1419. Begin
  1420. Consume(AS_NOT);
  1421. expr:=expr + '~';
  1422. end;
  1423. AS_XOR:
  1424. Begin
  1425. Consume(AS_XOR);
  1426. expr:=expr + '^';
  1427. end;
  1428. AS_OR:
  1429. Begin
  1430. Consume(AS_OR);
  1431. expr:=expr + '|';
  1432. end;
  1433. AS_INTNUM:
  1434. Begin
  1435. expr:=expr + actasmpattern;
  1436. Consume(AS_INTNUM);
  1437. end;
  1438. AS_DOLLAR:
  1439. begin
  1440. Consume(AS_DOLLAR);
  1441. if actasmtoken<>AS_ID then
  1442. Message(asmr_e_dollar_without_identifier);
  1443. end;
  1444. AS_STRING:
  1445. Begin
  1446. l:=0;
  1447. case Length(actasmpattern) of
  1448. 1 :
  1449. l:=ord(actasmpattern[1]);
  1450. 2 :
  1451. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1452. 3 :
  1453. l:=ord(actasmpattern[3]) +
  1454. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1455. 4 :
  1456. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1457. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1458. else
  1459. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1460. end;
  1461. str(l, tempstr);
  1462. expr:=expr + tempstr;
  1463. Consume(AS_STRING);
  1464. end;
  1465. AS_SIZEOF,
  1466. AS_TYPE:
  1467. begin
  1468. l:=0;
  1469. Consume(actasmtoken);
  1470. if actasmtoken<>AS_ID then
  1471. Message(asmr_e_type_without_identifier)
  1472. else
  1473. begin
  1474. tempstr:=actasmpattern;
  1475. Consume(AS_ID);
  1476. if actasmtoken=AS_DOT then
  1477. begin
  1478. BuildRecordOffsetSize(tempstr,k,l,mangledname,false);
  1479. if mangledname<>'' then
  1480. Message(asmr_e_wrong_sym_type);
  1481. end
  1482. else
  1483. begin
  1484. searchsym(tempstr,sym,srsymtable);
  1485. if assigned(sym) then
  1486. begin
  1487. case sym.typ of
  1488. staticvarsym,
  1489. localvarsym,
  1490. paravarsym :
  1491. l:=tabstractvarsym(sym).getsize;
  1492. typesym :
  1493. l:=ttypesym(sym).typedef.size;
  1494. else
  1495. Message(asmr_e_wrong_sym_type);
  1496. end;
  1497. end
  1498. else
  1499. Message1(sym_e_unknown_id,tempstr);
  1500. end;
  1501. end;
  1502. str(l, tempstr);
  1503. expr:=expr + tempstr;
  1504. end;
  1505. AS_VMTOFFSET:
  1506. begin
  1507. Consume(actasmtoken);
  1508. if actasmtoken<>AS_ID then
  1509. Message(asmr_e_type_without_identifier)
  1510. else
  1511. begin
  1512. tempstr:=actasmpattern;
  1513. consume(AS_ID);
  1514. BuildRecordOffsetSize(tempstr,k,l,mangledname,true);
  1515. if (mangledname <> '') then
  1516. Message(asmr_e_wrong_sym_type);
  1517. str(k,tempstr);
  1518. expr := expr + tempstr;
  1519. end
  1520. end;
  1521. AS_ID:
  1522. Begin
  1523. hs:='';
  1524. hssymtyp:=AT_DATA;
  1525. tempstr:=actasmpattern;
  1526. prevtok:=prevasmtoken;
  1527. consume(AS_ID);
  1528. if SearchIConstant(tempstr,l) then
  1529. begin
  1530. str(l, tempstr);
  1531. expr:=expr + tempstr;
  1532. end
  1533. else
  1534. begin
  1535. if is_locallabel(tempstr) then
  1536. begin
  1537. CreateLocalLabel(tempstr,hl,false);
  1538. hs:=hl.name;
  1539. hssymtyp:=AT_LABEL;
  1540. end
  1541. else
  1542. if SearchLabel(tempstr,hl,false) then
  1543. begin
  1544. hs:=hl.name;
  1545. hssymtyp:=AT_FUNCTION;
  1546. end
  1547. else
  1548. begin
  1549. searchsym(tempstr,sym,srsymtable);
  1550. if assigned(sym) then
  1551. begin
  1552. case sym.typ of
  1553. staticvarsym :
  1554. begin
  1555. { we always assume in asm statements that }
  1556. { that the variable is valid. }
  1557. tabstractvarsym(sym).varstate:=vs_readwritten;
  1558. inc(tabstractvarsym(sym).refs);
  1559. { variable can't be placed in a register }
  1560. tabstractvarsym(sym).varregable:=vr_none;
  1561. { and anything may happen with its address }
  1562. tabstractvarsym(sym).addr_taken:=true;
  1563. hs:=tstaticvarsym(sym).mangledname;
  1564. end;
  1565. localvarsym,
  1566. paravarsym :
  1567. Message(asmr_e_no_local_or_para_allowed);
  1568. procsym :
  1569. begin
  1570. if Tprocsym(sym).ProcdefList.Count>1 then
  1571. Message(asmr_w_calling_overload_func);
  1572. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1573. hssymtyp:=AT_FUNCTION;
  1574. end;
  1575. typesym :
  1576. begin
  1577. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1578. Message(asmr_e_wrong_sym_type);
  1579. end;
  1580. else
  1581. Message(asmr_e_wrong_sym_type);
  1582. end;
  1583. end
  1584. else
  1585. Message1(sym_e_unknown_id,tempstr);
  1586. end;
  1587. { symbol found? }
  1588. if hs<>'' then
  1589. begin
  1590. if needofs and (prevtok<>AS_DOLLAR) then
  1591. Message(asmr_e_need_dollar);
  1592. if asmsym='' then
  1593. begin
  1594. asmsym:=hs;
  1595. asmsymtyp:=hssymtyp;
  1596. end
  1597. else
  1598. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1599. if (expr='') or (expr[length(expr)]='+') then
  1600. begin
  1601. { don't remove the + if there could be a record field }
  1602. if actasmtoken<>AS_DOT then
  1603. delete(expr,length(expr),1);
  1604. end
  1605. else
  1606. Message(asmr_e_only_add_relocatable_symbol);
  1607. end;
  1608. if actasmtoken=AS_DOT then
  1609. begin
  1610. BuildRecordOffsetSize(tempstr,l,k,hs,false);
  1611. if (hs<>'') then
  1612. hssymtyp:=AT_FUNCTION
  1613. else
  1614. begin
  1615. str(l, tempstr);
  1616. expr:=expr + tempstr;
  1617. end
  1618. end
  1619. else
  1620. begin
  1621. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1622. delete(expr,length(expr),1);
  1623. end;
  1624. end;
  1625. { check if there are wrong operator used like / or mod etc. }
  1626. if (hs<>'') and
  1627. not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,
  1628. AS_LPAREN,AS_RPAREN,AS_RBRACKET,AS_END]) then
  1629. Message(asmr_e_only_add_relocatable_symbol);
  1630. end;
  1631. AS_END,
  1632. AS_SEPARATOR,
  1633. AS_COMMA:
  1634. break;
  1635. else
  1636. Begin
  1637. { write error only once. }
  1638. if not errorflag then
  1639. Message(asmr_e_invalid_constant_expression);
  1640. { consume tokens until we find COMMA or SEPARATOR }
  1641. Consume(actasmtoken);
  1642. errorflag:=TRUE;
  1643. end;
  1644. end;
  1645. Until false;
  1646. { calculate expression }
  1647. if not ErrorFlag then
  1648. value:=CalculateExpression(expr)
  1649. else
  1650. value:=0;
  1651. end;
  1652. function tattreader.BuildConstExpression(allowref,betweenbracket:boolean): tcgint;
  1653. var
  1654. l : tcgint;
  1655. hs : string;
  1656. hssymtyp : TAsmSymType;
  1657. begin
  1658. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs,hssymtyp);
  1659. if hs<>'' then
  1660. Message(asmr_e_relocatable_symbol_not_allowed);
  1661. BuildConstExpression:=l;
  1662. end;
  1663. Procedure tattreader.BuildConstantOperand(oper : toperand);
  1664. var
  1665. l : tcgint;
  1666. tempstr : string;
  1667. tempsymtyp : TAsmSymType;
  1668. begin
  1669. BuildConstSymbolExpression(false,false,true,l,tempstr,tempsymtyp);
  1670. if tempstr<>'' then
  1671. begin
  1672. oper.opr.typ:=OPR_SYMBOL;
  1673. oper.opr.symofs:=l;
  1674. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1675. end
  1676. else
  1677. begin
  1678. oper.opr.typ:=OPR_CONSTANT;
  1679. { cast properly to avoid a range check error }
  1680. {$if defined(AVR) or defined(i8086)}
  1681. oper.opr.val:=longint(l);
  1682. {$else}
  1683. oper.opr.val:=aint(l);
  1684. {$endif}
  1685. end;
  1686. end;
  1687. procedure tattreader.BuildRva;
  1688. var
  1689. asmsymtyp : TAsmSymType;
  1690. asmsym: string;
  1691. value : tcgint;
  1692. ai:tai_const;
  1693. begin
  1694. repeat
  1695. case actasmtoken of
  1696. AS_INTNUM,
  1697. AS_PLUS,
  1698. AS_MINUS,
  1699. AS_LPAREN,
  1700. AS_ID :
  1701. Begin
  1702. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  1703. if asmsym<>'' then
  1704. begin
  1705. ai:=tai_const.create_type_sym(aitconst_rva_symbol,current_asmdata.RefAsmSymbol(asmsym,asmsymtyp));
  1706. ai.value:=value;
  1707. curlist.concat(ai);
  1708. end
  1709. else
  1710. Message(asmr_e_invalid_symbol_ref);
  1711. end;
  1712. AS_COMMA:
  1713. Consume(AS_COMMA);
  1714. AS_END,
  1715. AS_SEPARATOR:
  1716. break;
  1717. else
  1718. begin
  1719. Message(asmr_e_syn_constant);
  1720. RecoverConsume(false);
  1721. end
  1722. end; { end case }
  1723. until false;
  1724. end;
  1725. end.