raatt.pas 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  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,
  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. { Opcode ? }
  303. If is_asmopcode(upper(actasmpattern)) then
  304. Begin
  305. uppervar(actasmpattern);
  306. exit;
  307. end;
  308. { End of assemblerblock ? }
  309. if upper(actasmpattern) = 'END' then
  310. begin
  311. actasmtoken:=AS_END;
  312. exit;
  313. end;
  314. message1(asmr_e_unknown_opcode,actasmpattern);
  315. actasmtoken:=AS_NONE;
  316. end
  317. else { else firsttoken }
  318. { Here we must handle all possible cases }
  319. begin
  320. case c of
  321. '.' : { possiblities : - local label reference , such as in jmp @local1 }
  322. { - field of object/record }
  323. { - directive. }
  324. begin
  325. if (prevasmtoken in [AS_ID,AS_RPAREN]) then
  326. begin
  327. c:=current_scanner.asmgetchar;
  328. actasmtoken:=AS_DOT;
  329. exit;
  330. end;
  331. actasmpattern:=c;
  332. c:=current_scanner.asmgetchar;
  333. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  334. begin
  335. actasmpattern:=actasmpattern + c;
  336. c:=current_scanner.asmgetchar;
  337. end;
  338. if is_asmdirective(actasmpattern) then
  339. exit;
  340. if is_targetdirective(actasmpattern) then
  341. begin
  342. actasmtoken:=AS_TARGET_DIRECTIVE;
  343. exit;
  344. end;
  345. { local label references and directives }
  346. { are case sensitive }
  347. actasmtoken:=AS_ID;
  348. exit;
  349. end;
  350. { identifier, register, prefix or directive }
  351. '_','A'..'Z','a'..'z':
  352. begin
  353. len:=0;
  354. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  355. begin
  356. inc(len);
  357. actasmpattern[len]:=c;
  358. c:=current_scanner.asmgetchar;
  359. end;
  360. actasmpattern[0]:=chr(len);
  361. uppervar(actasmpattern);
  362. {$ifdef x86}
  363. { only x86 architectures have instruction prefixes }
  364. { Opcode, can only be when the previous was a prefix }
  365. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  366. Begin
  367. uppervar(actasmpattern);
  368. exit;
  369. end;
  370. {$endif x86}
  371. { check for end which is a reserved word unlike the opcodes }
  372. if actasmpattern = 'END' then
  373. Begin
  374. actasmtoken:=AS_END;
  375. exit;
  376. end;
  377. if actasmpattern = 'TYPE' then
  378. Begin
  379. actasmtoken:=AS_TYPE;
  380. exit;
  381. end;
  382. if actasmpattern = 'SIZEOF' then
  383. Begin
  384. actasmtoken:=AS_SIZEOF;
  385. exit;
  386. end;
  387. if actasmpattern = 'VMTOFFSET' then
  388. Begin
  389. actasmtoken:=AS_VMTOFFSET;
  390. exit;
  391. end;
  392. if is_register(actasmpattern) then
  393. begin
  394. actasmtoken:=AS_REGISTER;
  395. exit;
  396. end;
  397. { if next is a '.' and this is a unitsym then we also need to
  398. parse the identifier }
  399. if (c='.') then
  400. begin
  401. searchsym(actasmpattern,srsym,srsymtable);
  402. if assigned(srsym) and
  403. (srsym.typ=unitsym) and
  404. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  405. srsym.owner.iscurrentunit then
  406. begin
  407. actasmpattern:=actasmpattern+c;
  408. c:=current_scanner.asmgetchar;
  409. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  410. begin
  411. actasmpattern:=actasmpattern + upcase(c);
  412. c:=current_scanner.asmgetchar;
  413. end;
  414. end;
  415. end;
  416. actasmtoken:=AS_ID;
  417. exit;
  418. end;
  419. '%' : { register or modulo }
  420. handlepercent;
  421. '1'..'9': { integer number }
  422. begin
  423. len:=0;
  424. while c in ['0'..'9'] do
  425. Begin
  426. inc(len);
  427. actasmpattern[len]:=c;
  428. c:=current_scanner.asmgetchar;
  429. end;
  430. actasmpattern[0]:=chr(len);
  431. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  432. actasmtoken:=AS_INTNUM;
  433. exit;
  434. end;
  435. '0' : { octal,hexa,real or binary number. }
  436. begin
  437. actasmpattern:=c;
  438. c:=current_scanner.asmgetchar;
  439. case upcase(c) of
  440. 'B': { binary }
  441. Begin
  442. c:=current_scanner.asmgetchar;
  443. while c in ['0','1'] do
  444. Begin
  445. actasmpattern:=actasmpattern + c;
  446. c:=current_scanner.asmgetchar;
  447. end;
  448. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  449. actasmtoken:=AS_INTNUM;
  450. exit;
  451. end;
  452. 'D': { real }
  453. Begin
  454. c:=current_scanner.asmgetchar;
  455. { get ridd of the 0d }
  456. if (c in ['+','-']) then
  457. begin
  458. actasmpattern:=c;
  459. c:=current_scanner.asmgetchar;
  460. end
  461. else
  462. actasmpattern:='';
  463. while c in ['0'..'9'] do
  464. Begin
  465. actasmpattern:=actasmpattern + c;
  466. c:=current_scanner.asmgetchar;
  467. end;
  468. if c='.' then
  469. begin
  470. actasmpattern:=actasmpattern + c;
  471. c:=current_scanner.asmgetchar;
  472. while c in ['0'..'9'] do
  473. Begin
  474. actasmpattern:=actasmpattern + c;
  475. c:=current_scanner.asmgetchar;
  476. end;
  477. if upcase(c) = 'E' then
  478. begin
  479. actasmpattern:=actasmpattern + c;
  480. c:=current_scanner.asmgetchar;
  481. if (c in ['+','-']) then
  482. begin
  483. actasmpattern:=actasmpattern + c;
  484. c:=current_scanner.asmgetchar;
  485. end;
  486. while c in ['0'..'9'] do
  487. Begin
  488. actasmpattern:=actasmpattern + c;
  489. c:=current_scanner.asmgetchar;
  490. end;
  491. end;
  492. actasmtoken:=AS_REALNUM;
  493. exit;
  494. end
  495. else
  496. begin
  497. Message1(asmr_e_invalid_float_const,actasmpattern+c);
  498. actasmtoken:=AS_NONE;
  499. end;
  500. end;
  501. 'X': { hexadecimal }
  502. Begin
  503. c:=current_scanner.asmgetchar;
  504. while c in ['0'..'9','a'..'f','A'..'F'] do
  505. Begin
  506. actasmpattern:=actasmpattern + c;
  507. c:=current_scanner.asmgetchar;
  508. end;
  509. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  510. actasmtoken:=AS_INTNUM;
  511. exit;
  512. end;
  513. '1'..'7': { octal }
  514. begin
  515. actasmpattern:=actasmpattern + c;
  516. while c in ['0'..'7'] do
  517. Begin
  518. actasmpattern:=actasmpattern + c;
  519. c:=current_scanner.asmgetchar;
  520. end;
  521. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  522. actasmtoken:=AS_INTNUM;
  523. exit;
  524. end;
  525. else { octal number zero value...}
  526. Begin
  527. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  528. actasmtoken:=AS_INTNUM;
  529. exit;
  530. end;
  531. end; { end case }
  532. end;
  533. '&' :
  534. begin
  535. c:=current_scanner.asmgetchar;
  536. actasmtoken:=AS_AND;
  537. end;
  538. '''' : { char }
  539. begin
  540. actasmpattern:='';
  541. repeat
  542. c:=current_scanner.asmgetchar;
  543. case c of
  544. '\' :
  545. begin
  546. { copy also the next char so \" is parsed correctly }
  547. actasmpattern:=actasmpattern+c;
  548. c:=current_scanner.asmgetchar;
  549. actasmpattern:=actasmpattern+c;
  550. end;
  551. '''' :
  552. begin
  553. c:=current_scanner.asmgetchar;
  554. break;
  555. end;
  556. #10,#13:
  557. Message(scan_f_string_exceeds_line);
  558. else
  559. actasmpattern:=actasmpattern+c;
  560. end;
  561. until false;
  562. actasmpattern:=EscapeToPascal(actasmpattern);
  563. actasmtoken:=AS_STRING;
  564. exit;
  565. end;
  566. '"' : { string }
  567. begin
  568. actasmpattern:='';
  569. repeat
  570. c:=current_scanner.asmgetchar;
  571. case c of
  572. '\' :
  573. begin
  574. { copy also the next char so \" is parsed correctly }
  575. actasmpattern:=actasmpattern+c;
  576. c:=current_scanner.asmgetchar;
  577. actasmpattern:=actasmpattern+c;
  578. end;
  579. '"' :
  580. begin
  581. c:=current_scanner.asmgetchar;
  582. break;
  583. end;
  584. #10,#13:
  585. Message(scan_f_string_exceeds_line);
  586. else
  587. actasmpattern:=actasmpattern+c;
  588. end;
  589. until false;
  590. actasmpattern:=EscapeToPascal(actasmpattern);
  591. actasmtoken:=AS_STRING;
  592. exit;
  593. end;
  594. '$' :
  595. begin
  596. handledollar;
  597. exit;
  598. end;
  599. '#' :
  600. begin
  601. actasmtoken:=AS_HASH;
  602. c:=current_scanner.asmgetchar;
  603. exit;
  604. end;
  605. '[' :
  606. begin
  607. actasmtoken:=AS_LBRACKET;
  608. c:=current_scanner.asmgetchar;
  609. exit;
  610. end;
  611. ']' :
  612. begin
  613. actasmtoken:=AS_RBRACKET;
  614. c:=current_scanner.asmgetchar;
  615. exit;
  616. end;
  617. '{' :
  618. begin
  619. {$ifdef arm}
  620. // the arm assembler uses { ... } for register sets
  621. // but compiler directives {$... } are still allowed
  622. c:=current_scanner.asmgetchar;
  623. if c<>'$' then
  624. actasmtoken:=AS_LSBRACKET
  625. else
  626. begin
  627. current_scanner.skipcomment(false);
  628. GetToken;
  629. end;
  630. {$else arm}
  631. current_scanner.skipcomment(true);
  632. GetToken;
  633. {$endif arm}
  634. exit;
  635. end;
  636. {$ifdef arm}
  637. '}' :
  638. begin
  639. actasmtoken:=AS_RSBRACKET;
  640. c:=current_scanner.asmgetchar;
  641. exit;
  642. end;
  643. '=' :
  644. begin
  645. actasmtoken:=AS_EQUAL;
  646. c:=current_scanner.asmgetchar;
  647. exit;
  648. end;
  649. {$endif arm}
  650. ',' :
  651. begin
  652. actasmtoken:=AS_COMMA;
  653. c:=current_scanner.asmgetchar;
  654. exit;
  655. end;
  656. '<' :
  657. begin
  658. actasmtoken:=AS_SHL;
  659. c:=current_scanner.asmgetchar;
  660. if c = '<' then
  661. c:=current_scanner.asmgetchar;
  662. exit;
  663. end;
  664. '>' :
  665. begin
  666. actasmtoken:=AS_SHL;
  667. c:=current_scanner.asmgetchar;
  668. if c = '>' then
  669. c:=current_scanner.asmgetchar;
  670. exit;
  671. end;
  672. '|' :
  673. begin
  674. actasmtoken:=AS_OR;
  675. c:=current_scanner.asmgetchar;
  676. exit;
  677. end;
  678. '^' :
  679. begin
  680. actasmtoken:=AS_XOR;
  681. c:=current_scanner.asmgetchar;
  682. exit;
  683. end;
  684. '(' :
  685. begin
  686. c:=current_scanner.asmgetchar;
  687. if c='*' then
  688. begin
  689. current_scanner.skipoldtpcomment(true);
  690. GetToken;
  691. end
  692. else
  693. actasmtoken:=AS_LPAREN;
  694. exit;
  695. end;
  696. ')' :
  697. begin
  698. actasmtoken:=AS_RPAREN;
  699. c:=current_scanner.asmgetchar;
  700. exit;
  701. end;
  702. ':' :
  703. begin
  704. actasmtoken:=AS_COLON;
  705. c:=current_scanner.asmgetchar;
  706. exit;
  707. end;
  708. '+' :
  709. begin
  710. actasmtoken:=AS_PLUS;
  711. c:=current_scanner.asmgetchar;
  712. exit;
  713. end;
  714. '-' :
  715. begin
  716. actasmtoken:=AS_MINUS;
  717. c:=current_scanner.asmgetchar;
  718. exit;
  719. end;
  720. '*' :
  721. begin
  722. actasmtoken:=AS_STAR;
  723. c:=current_scanner.asmgetchar;
  724. exit;
  725. end;
  726. '/' :
  727. begin
  728. c:=current_scanner.asmgetchar;
  729. if c='/' then
  730. begin
  731. current_scanner.skipdelphicomment;
  732. GetToken;
  733. end
  734. else
  735. actasmtoken:=AS_SLASH;
  736. exit;
  737. end;
  738. '!' :
  739. begin
  740. actasmtoken:=AS_NOT;
  741. c:=current_scanner.asmgetchar;
  742. exit;
  743. end;
  744. '@' :
  745. begin
  746. actasmtoken:=AS_AT;
  747. c:=current_scanner.asmgetchar;
  748. exit;
  749. end;
  750. #13,#10:
  751. begin
  752. current_scanner.linebreak;
  753. c:=current_scanner.asmgetchar;
  754. firsttoken:=TRUE;
  755. actasmtoken:=AS_SEPARATOR;
  756. exit;
  757. end;
  758. ';' :
  759. begin
  760. c:=current_scanner.asmgetchar;
  761. firsttoken:=TRUE;
  762. actasmtoken:=AS_SEPARATOR;
  763. exit;
  764. end;
  765. else
  766. current_scanner.illegal_char(c);
  767. end;
  768. end;
  769. end;
  770. function tattreader.consume(t : tasmtoken):boolean;
  771. begin
  772. Consume:=true;
  773. if t<>actasmtoken then
  774. begin
  775. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  776. Consume:=false;
  777. end;
  778. repeat
  779. gettoken;
  780. until actasmtoken<>AS_NONE;
  781. end;
  782. procedure tattreader.RecoverConsume(allowcomma:boolean);
  783. begin
  784. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  785. begin
  786. if allowcomma and (actasmtoken=AS_COMMA) then
  787. break;
  788. Consume(actasmtoken);
  789. end;
  790. end;
  791. Procedure tattreader.BuildConstant(constsize: byte);
  792. var
  793. asmsymtyp : TAsmSymType;
  794. asmsym,
  795. expr: string;
  796. value : tcgint;
  797. Begin
  798. Repeat
  799. Case actasmtoken of
  800. AS_STRING:
  801. Begin
  802. expr:=actasmpattern;
  803. if length(expr) > 1 then
  804. Message(asmr_e_string_not_allowed_as_const);
  805. Consume(AS_STRING);
  806. Case actasmtoken of
  807. AS_COMMA: Consume(AS_COMMA);
  808. AS_END,
  809. AS_SEPARATOR: ;
  810. else
  811. Message(asmr_e_invalid_string_expression);
  812. end; { end case }
  813. ConcatString(curlist,expr);
  814. end;
  815. AS_INTNUM,
  816. AS_PLUS,
  817. AS_MINUS,
  818. AS_LPAREN,
  819. AS_TYPE,
  820. AS_SIZEOF,
  821. AS_NOT,
  822. AS_VMTOFFSET,
  823. AS_ID :
  824. Begin
  825. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  826. if asmsym<>'' then
  827. begin
  828. if constsize<>sizeof(pint) then
  829. Message(asmr_w_32bit_const_for_address);
  830. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value)
  831. end
  832. else
  833. ConcatConstant(curlist,value,constsize);
  834. end;
  835. AS_COMMA:
  836. Consume(AS_COMMA);
  837. AS_END,
  838. AS_SEPARATOR:
  839. break;
  840. else
  841. begin
  842. Message(asmr_e_syn_constant);
  843. RecoverConsume(false);
  844. end
  845. end; { end case }
  846. Until false;
  847. end;
  848. Procedure tattreader.BuildRealConstant(typ : tfloattype);
  849. var
  850. expr : string;
  851. r : bestreal;
  852. code : integer;
  853. negativ : boolean;
  854. errorflag: boolean;
  855. Begin
  856. errorflag:=FALSE;
  857. Repeat
  858. negativ:=false;
  859. expr:='';
  860. if actasmtoken=AS_PLUS then
  861. Consume(AS_PLUS)
  862. else
  863. if actasmtoken=AS_MINUS then
  864. begin
  865. negativ:=true;
  866. consume(AS_MINUS);
  867. end;
  868. Case actasmtoken of
  869. AS_INTNUM:
  870. Begin
  871. expr:=actasmpattern;
  872. Consume(AS_INTNUM);
  873. if negativ then
  874. expr:='-'+expr;
  875. val(expr,r,code);
  876. if code<>0 then
  877. Begin
  878. r:=0;
  879. Message(asmr_e_invalid_float_expr);
  880. End;
  881. ConcatRealConstant(curlist,r,typ);
  882. end;
  883. AS_REALNUM:
  884. Begin
  885. expr:=actasmpattern;
  886. Consume(AS_REALNUM);
  887. { in ATT syntax you have 0d in front of the real }
  888. { should this be forced ? yes i think so, as to }
  889. { conform to gas as much as possible. }
  890. if (expr[1]='0') and (upper(expr[2])='D') then
  891. Delete(expr,1,2);
  892. if negativ then
  893. expr:='-'+expr;
  894. val(expr,r,code);
  895. if code<>0 then
  896. Begin
  897. r:=0;
  898. Message(asmr_e_invalid_float_expr);
  899. End;
  900. ConcatRealConstant(curlist,r,typ);
  901. end;
  902. AS_COMMA:
  903. begin
  904. Consume(AS_COMMA);
  905. end;
  906. AS_END,
  907. AS_SEPARATOR:
  908. begin
  909. break;
  910. end;
  911. else
  912. Begin
  913. Consume(actasmtoken);
  914. if not errorflag then
  915. Message(asmr_e_invalid_float_expr);
  916. errorflag:=TRUE;
  917. end;
  918. end;
  919. Until false;
  920. end;
  921. Procedure tattreader.BuildStringConstant(asciiz: boolean);
  922. var
  923. expr: string;
  924. errorflag : boolean;
  925. Begin
  926. errorflag:=FALSE;
  927. Repeat
  928. Case actasmtoken of
  929. AS_STRING:
  930. Begin
  931. expr:=actasmpattern;
  932. if asciiz then
  933. expr:=expr+#0;
  934. ConcatString(curlist,expr);
  935. Consume(AS_STRING);
  936. end;
  937. AS_COMMA:
  938. begin
  939. Consume(AS_COMMA);
  940. end;
  941. AS_END,
  942. AS_SEPARATOR:
  943. begin
  944. break;
  945. end;
  946. else
  947. Begin
  948. Consume(actasmtoken);
  949. if not errorflag then
  950. Message(asmr_e_invalid_string_expression);
  951. errorflag:=TRUE;
  952. end;
  953. end;
  954. Until false;
  955. end;
  956. Function tattreader.Assemble: tlinkedlist;
  957. Var
  958. hl : tasmlabel;
  959. commname,
  960. symname,
  961. symval : string;
  962. lasTSec : TAsmSectiontype;
  963. l1,
  964. l2,
  965. symofs : tcgint;
  966. symtyp : TAsmsymtype;
  967. Begin
  968. Message1(asmr_d_start_reading,'GNU AS');
  969. firsttoken:=TRUE;
  970. { sets up all opcode and register tables in uppercase }
  971. if not _asmsorted then
  972. Begin
  973. SetupTables;
  974. _asmsorted:=TRUE;
  975. end;
  976. curlist:=TAsmList.Create;
  977. lasTSec:=sec_code;
  978. { start tokenizer }
  979. gettoken;
  980. { main loop }
  981. repeat
  982. case actasmtoken of
  983. AS_LLABEL:
  984. Begin
  985. if CreateLocalLabel(actasmpattern,hl,true) then
  986. ConcatLabel(curlist,hl);
  987. Consume(AS_LLABEL);
  988. end;
  989. AS_LABEL:
  990. Begin
  991. if SearchLabel(upper(actasmpattern),hl,true) then
  992. begin
  993. if hl.is_public then
  994. ConcatPublic(curlist,actasmpattern);
  995. ConcatLabel(curlist,hl);
  996. end
  997. else
  998. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  999. Consume(AS_LABEL);
  1000. end;
  1001. AS_DW:
  1002. Begin
  1003. Consume(AS_DW);
  1004. BuildConstant(2);
  1005. end;
  1006. AS_DATA:
  1007. Begin
  1008. new_section(curList,sec_data,lower(current_procinfo.procdef.mangledname),0);
  1009. lasTSec:=sec_data;
  1010. Consume(AS_DATA);
  1011. end;
  1012. AS_TEXT:
  1013. Begin
  1014. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1015. lasTSec:=sec_code;
  1016. Consume(AS_TEXT);
  1017. end;
  1018. AS_INIT:
  1019. Begin
  1020. new_section(curList,sec_init,lower(current_procinfo.procdef.mangledname),0);
  1021. lasTSec:=sec_init;
  1022. Consume(AS_INIT);
  1023. end;
  1024. AS_FINI:
  1025. Begin
  1026. new_section(curList,sec_fini,lower(current_procinfo.procdef.mangledname),0);
  1027. lasTSec:=sec_fini;
  1028. Consume(AS_FINI);
  1029. end;
  1030. AS_DB:
  1031. Begin
  1032. Consume(AS_DB);
  1033. BuildConstant(1);
  1034. end;
  1035. AS_DD:
  1036. Begin
  1037. Consume(AS_DD);
  1038. BuildConstant(4);
  1039. end;
  1040. AS_DQ:
  1041. Begin
  1042. Consume(AS_DQ);
  1043. {$ifdef cpu64bitaddr}
  1044. BuildConstant(8);
  1045. {$else cpu64bitaddr}
  1046. BuildRealConstant(s64comp);
  1047. {$endif cpu64bitaddr}
  1048. end;
  1049. AS_SINGLE:
  1050. Begin
  1051. Consume(AS_SINGLE);
  1052. BuildRealConstant(s32real);
  1053. end;
  1054. AS_DOUBLE:
  1055. Begin
  1056. Consume(AS_DOUBLE);
  1057. BuildRealConstant(s64real);
  1058. end;
  1059. AS_EXTENDED:
  1060. Begin
  1061. Consume(AS_EXTENDED);
  1062. BuildRealConstant(s80real);
  1063. end;
  1064. AS_CEXTENDED:
  1065. Begin
  1066. Consume(AS_CEXTENDED);
  1067. BuildRealConstant(sc80real);
  1068. end;
  1069. AS_GLOBAL:
  1070. Begin
  1071. Consume(AS_GLOBAL);
  1072. if actasmtoken=AS_ID then
  1073. ConcatPublic(curlist,actasmpattern);
  1074. Consume(AS_ID);
  1075. if actasmtoken<>AS_SEPARATOR then
  1076. Consume(AS_SEPARATOR);
  1077. end;
  1078. AS_ALIGN:
  1079. Begin
  1080. Consume(AS_ALIGN);
  1081. l1:=BuildConstExpression(false,false);
  1082. if (target_info.system in [system_i386_GO32V2]) then
  1083. begin
  1084. l2:=1;
  1085. if (l1>=0) and (l1<=16) then
  1086. while (l1>0) do
  1087. begin
  1088. l2:=2*l2;
  1089. dec(l1);
  1090. end;
  1091. l1:=l2;
  1092. end;
  1093. ConcatAlign(curlist,l1);
  1094. Message(asmr_n_align_is_target_specific);
  1095. if actasmtoken<>AS_SEPARATOR then
  1096. Consume(AS_SEPARATOR);
  1097. end;
  1098. AS_BALIGN:
  1099. Begin
  1100. Consume(AS_BALIGN);
  1101. ConcatAlign(curlist,BuildConstExpression(false,false));
  1102. if actasmtoken<>AS_SEPARATOR then
  1103. Consume(AS_SEPARATOR);
  1104. end;
  1105. AS_P2ALIGN:
  1106. Begin
  1107. Consume(AS_P2ALIGN);
  1108. l1:=BuildConstExpression(false,false);
  1109. l2:=1;
  1110. if (l1>=0) and (l1<=16) then
  1111. while (l1>0) do
  1112. begin
  1113. l2:=2*l2;
  1114. dec(l1);
  1115. end;
  1116. l1:=l2;
  1117. ConcatAlign(curlist,l1);
  1118. if actasmtoken<>AS_SEPARATOR then
  1119. Consume(AS_SEPARATOR);
  1120. end;
  1121. AS_ASCIIZ:
  1122. Begin
  1123. Consume(AS_ASCIIZ);
  1124. BuildStringConstant(TRUE);
  1125. end;
  1126. AS_ASCII:
  1127. Begin
  1128. Consume(AS_ASCII);
  1129. BuildStringConstant(FALSE);
  1130. end;
  1131. AS_LCOMM:
  1132. Begin
  1133. Consume(AS_LCOMM);
  1134. commname:=actasmpattern;
  1135. Consume(AS_ID);
  1136. Consume(AS_COMMA);
  1137. symofs:=BuildConstExpression(false,false);
  1138. curList.concat(Tai_datablock.Create(commname,symofs,carraydef.getreusable(u8inttype,symofs)));
  1139. if actasmtoken<>AS_SEPARATOR then
  1140. Consume(AS_SEPARATOR);
  1141. end;
  1142. AS_COMM:
  1143. Begin
  1144. Consume(AS_COMM);
  1145. commname:=actasmpattern;
  1146. Consume(AS_ID);
  1147. Consume(AS_COMMA);
  1148. symofs:=BuildConstExpression(false,false);
  1149. curList.concat(Tai_datablock.Create_global(commname,symofs,carraydef.getreusable(u8inttype,symofs)));
  1150. if actasmtoken<>AS_SEPARATOR then
  1151. Consume(AS_SEPARATOR);
  1152. end;
  1153. AS_OPCODE:
  1154. Begin
  1155. HandleOpCode;
  1156. end;
  1157. AS_SEPARATOR:
  1158. Begin
  1159. Consume(AS_SEPARATOR);
  1160. end;
  1161. AS_RVA:
  1162. begin
  1163. { .rva generally applies to systems with COFF output format,
  1164. not just Windows. }
  1165. if not (target_info.system in systems_all_windows) then
  1166. Message1(asmr_e_unsupported_directive,token2str[AS_RVA]);
  1167. Consume(AS_RVA);
  1168. BuildRva;
  1169. end;
  1170. AS_SET:
  1171. begin
  1172. Consume(AS_SET);
  1173. BuildConstSymbolExpression(true,false,false, symofs,symname,symtyp);
  1174. Consume(AS_COMMA);
  1175. BuildConstSymbolExpression(true,false,false, symofs,symval,symtyp);
  1176. curList.concat(tai_symbolpair.create(spk_set,symname,symval));
  1177. end;
  1178. AS_WEAK:
  1179. begin
  1180. Consume(AS_WEAK);
  1181. BuildConstSymbolExpression(true,false,false, l1,symname,symtyp);
  1182. { ideally, we should look up the symbol here (or later) and
  1183. depending on whether it's external or net, generate
  1184. asd_weak_reference or asd_weak_definition -- this is different
  1185. on some targets) }
  1186. curList.concat(tai_directive.create(asd_weak_definition,symname));
  1187. end;
  1188. AS_SECTION:
  1189. begin
  1190. Consume(AS_SECTION);
  1191. new_section(curlist, sec_user, actasmpattern, 0);
  1192. //curList.concat(tai_section.create(sec_user, actasmpattern, 0));
  1193. consume(AS_STRING);
  1194. end;
  1195. AS_TARGET_DIRECTIVE:
  1196. HandleTargetDirective;
  1197. AS_END:
  1198. begin
  1199. break; { end assembly block }
  1200. end;
  1201. else
  1202. Begin
  1203. Message(asmr_e_syntax_error);
  1204. RecoverConsume(false);
  1205. end;
  1206. end;
  1207. until false;
  1208. { check that all referenced local labels are defined }
  1209. checklocallabels;
  1210. { are we back in the code section? }
  1211. if lasTSec<>sec_code then
  1212. begin
  1213. Message(asmr_w_assembler_code_not_returned_to_text);
  1214. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1215. end;
  1216. { Return the list in an asmnode }
  1217. assemble:=curlist;
  1218. Message1(asmr_d_finish_reading,'GNU AS');
  1219. end;
  1220. {*****************************************************************************
  1221. Parsing Helpers
  1222. *****************************************************************************}
  1223. Procedure tattreader.BuildRecordOffsetSize(const expr: string;var offset:tcgint;var size:tcgint; var mangledname: string; needvmtofs: boolean);
  1224. { Description: This routine builds up a record offset after a AS_DOT }
  1225. { token is encountered. }
  1226. { On entry actasmtoken should be equal to AS_DOT }
  1227. var
  1228. s : string;
  1229. hastypecast: boolean;
  1230. Begin
  1231. offset:=0;
  1232. size:=0;
  1233. s:=expr;
  1234. while (actasmtoken=AS_DOT) do
  1235. begin
  1236. Consume(AS_DOT);
  1237. { a record field could have the same name as a register }
  1238. if actasmtoken in [AS_ID,AS_REGISTER] then
  1239. begin
  1240. s:=s+'.'+actasmpattern;
  1241. consume(actasmtoken)
  1242. end
  1243. else
  1244. begin
  1245. Consume(AS_ID);
  1246. RecoverConsume(true);
  1247. break;
  1248. end;
  1249. end;
  1250. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs,hastypecast) then
  1251. Message(asmr_e_building_record_offset);
  1252. end;
  1253. procedure tattreader.BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:tcgint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  1254. var
  1255. hssymtyp : TAsmSymType;
  1256. hs,tempstr,expr,mangledname : string;
  1257. parenlevel : longint;
  1258. l,k : tcgint;
  1259. errorflag : boolean;
  1260. prevtok : tasmtoken;
  1261. sym : tsym;
  1262. srsymtable : TSymtable;
  1263. hl : tasmlabel;
  1264. Begin
  1265. asmsym:='';
  1266. asmsymtyp:=AT_DATA;
  1267. value:=0;
  1268. errorflag:=FALSE;
  1269. tempstr:='';
  1270. expr:='';
  1271. parenlevel:=0;
  1272. Repeat
  1273. Case actasmtoken of
  1274. AS_LPAREN:
  1275. Begin
  1276. { Exit if ref? }
  1277. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  1278. break;
  1279. Consume(AS_LPAREN);
  1280. expr:=expr + '(';
  1281. inc(parenlevel);
  1282. end;
  1283. AS_RBRACKET:
  1284. begin
  1285. if betweenbracket then
  1286. break;
  1287. { write error only once. }
  1288. if not errorflag then
  1289. Message(asmr_e_invalid_constant_expression);
  1290. { consume tokens until we find COMMA or SEPARATOR }
  1291. Consume(actasmtoken);
  1292. errorflag:=TRUE;
  1293. end;
  1294. AS_RPAREN:
  1295. Begin
  1296. { end of ref ? }
  1297. if (parenlevel=0) and betweenbracket then
  1298. break;
  1299. Consume(AS_RPAREN);
  1300. expr:=expr + ')';
  1301. dec(parenlevel);
  1302. end;
  1303. AS_SHL:
  1304. Begin
  1305. Consume(AS_SHL);
  1306. expr:=expr + '<';
  1307. end;
  1308. AS_SHR:
  1309. Begin
  1310. Consume(AS_SHR);
  1311. expr:=expr + '>';
  1312. end;
  1313. AS_SLASH:
  1314. Begin
  1315. Consume(AS_SLASH);
  1316. expr:=expr + '/';
  1317. end;
  1318. AS_MOD:
  1319. Begin
  1320. Consume(AS_MOD);
  1321. expr:=expr + '%';
  1322. end;
  1323. AS_STAR:
  1324. Begin
  1325. Consume(AS_STAR);
  1326. expr:=expr + '*';
  1327. end;
  1328. AS_PLUS:
  1329. Begin
  1330. Consume(AS_PLUS);
  1331. expr:=expr + '+';
  1332. end;
  1333. AS_MINUS:
  1334. Begin
  1335. Consume(AS_MINUS);
  1336. expr:=expr + '-';
  1337. end;
  1338. AS_AND:
  1339. Begin
  1340. Consume(AS_AND);
  1341. expr:=expr + '&';
  1342. end;
  1343. AS_NOT:
  1344. Begin
  1345. Consume(AS_NOT);
  1346. expr:=expr + '~';
  1347. end;
  1348. AS_XOR:
  1349. Begin
  1350. Consume(AS_XOR);
  1351. expr:=expr + '^';
  1352. end;
  1353. AS_OR:
  1354. Begin
  1355. Consume(AS_OR);
  1356. expr:=expr + '|';
  1357. end;
  1358. AS_INTNUM:
  1359. Begin
  1360. expr:=expr + actasmpattern;
  1361. Consume(AS_INTNUM);
  1362. end;
  1363. AS_DOLLAR:
  1364. begin
  1365. Consume(AS_DOLLAR);
  1366. if actasmtoken<>AS_ID then
  1367. Message(asmr_e_dollar_without_identifier);
  1368. end;
  1369. AS_STRING:
  1370. Begin
  1371. l:=0;
  1372. case Length(actasmpattern) of
  1373. 1 :
  1374. l:=ord(actasmpattern[1]);
  1375. 2 :
  1376. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1377. 3 :
  1378. l:=ord(actasmpattern[3]) +
  1379. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1380. 4 :
  1381. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1382. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1383. else
  1384. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1385. end;
  1386. str(l, tempstr);
  1387. expr:=expr + tempstr;
  1388. Consume(AS_STRING);
  1389. end;
  1390. AS_SIZEOF,
  1391. AS_TYPE:
  1392. begin
  1393. l:=0;
  1394. Consume(actasmtoken);
  1395. if actasmtoken<>AS_ID then
  1396. Message(asmr_e_type_without_identifier)
  1397. else
  1398. begin
  1399. tempstr:=actasmpattern;
  1400. Consume(AS_ID);
  1401. if actasmtoken=AS_DOT then
  1402. begin
  1403. BuildRecordOffsetSize(tempstr,k,l,mangledname,false);
  1404. if mangledname<>'' then
  1405. Message(asmr_e_wrong_sym_type);
  1406. end
  1407. else
  1408. begin
  1409. searchsym(tempstr,sym,srsymtable);
  1410. if assigned(sym) then
  1411. begin
  1412. case sym.typ of
  1413. staticvarsym,
  1414. localvarsym,
  1415. paravarsym :
  1416. l:=tabstractvarsym(sym).getsize;
  1417. typesym :
  1418. l:=ttypesym(sym).typedef.size;
  1419. else
  1420. Message(asmr_e_wrong_sym_type);
  1421. end;
  1422. end
  1423. else
  1424. Message1(sym_e_unknown_id,tempstr);
  1425. end;
  1426. end;
  1427. str(l, tempstr);
  1428. expr:=expr + tempstr;
  1429. end;
  1430. AS_VMTOFFSET:
  1431. begin
  1432. Consume(actasmtoken);
  1433. if actasmtoken<>AS_ID then
  1434. Message(asmr_e_type_without_identifier)
  1435. else
  1436. begin
  1437. tempstr:=actasmpattern;
  1438. consume(AS_ID);
  1439. BuildRecordOffsetSize(tempstr,k,l,mangledname,true);
  1440. if (mangledname <> '') then
  1441. Message(asmr_e_wrong_sym_type);
  1442. str(k,tempstr);
  1443. expr := expr + tempstr;
  1444. end
  1445. end;
  1446. AS_ID:
  1447. Begin
  1448. hs:='';
  1449. hssymtyp:=AT_DATA;
  1450. tempstr:=actasmpattern;
  1451. prevtok:=prevasmtoken;
  1452. consume(AS_ID);
  1453. if SearchIConstant(tempstr,l) then
  1454. begin
  1455. str(l, tempstr);
  1456. expr:=expr + tempstr;
  1457. end
  1458. else
  1459. begin
  1460. if is_locallabel(tempstr) then
  1461. begin
  1462. CreateLocalLabel(tempstr,hl,false);
  1463. hs:=hl.name;
  1464. hssymtyp:=AT_LABEL;
  1465. end
  1466. else
  1467. if SearchLabel(tempstr,hl,false) then
  1468. begin
  1469. hs:=hl.name;
  1470. hssymtyp:=AT_FUNCTION;
  1471. end
  1472. else
  1473. begin
  1474. searchsym(tempstr,sym,srsymtable);
  1475. if assigned(sym) then
  1476. begin
  1477. case sym.typ of
  1478. staticvarsym :
  1479. begin
  1480. { we always assume in asm statements that }
  1481. { that the variable is valid. }
  1482. tabstractvarsym(sym).varstate:=vs_readwritten;
  1483. inc(tabstractvarsym(sym).refs);
  1484. { variable can't be placed in a register }
  1485. tabstractvarsym(sym).varregable:=vr_none;
  1486. { and anything may happen with its address }
  1487. tabstractvarsym(sym).addr_taken:=true;
  1488. hs:=tstaticvarsym(sym).mangledname;
  1489. end;
  1490. localvarsym,
  1491. paravarsym :
  1492. Message(asmr_e_no_local_or_para_allowed);
  1493. procsym :
  1494. begin
  1495. if Tprocsym(sym).ProcdefList.Count>1 then
  1496. Message(asmr_w_calling_overload_func);
  1497. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1498. hssymtyp:=AT_FUNCTION;
  1499. end;
  1500. typesym :
  1501. begin
  1502. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1503. Message(asmr_e_wrong_sym_type);
  1504. end;
  1505. else
  1506. Message(asmr_e_wrong_sym_type);
  1507. end;
  1508. end
  1509. else
  1510. Message1(sym_e_unknown_id,tempstr);
  1511. end;
  1512. { symbol found? }
  1513. if hs<>'' then
  1514. begin
  1515. if needofs and (prevtok<>AS_DOLLAR) then
  1516. Message(asmr_e_need_dollar);
  1517. if asmsym='' then
  1518. begin
  1519. asmsym:=hs;
  1520. asmsymtyp:=hssymtyp;
  1521. end
  1522. else
  1523. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1524. if (expr='') or (expr[length(expr)]='+') then
  1525. begin
  1526. { don't remove the + if there could be a record field }
  1527. if actasmtoken<>AS_DOT then
  1528. delete(expr,length(expr),1);
  1529. end
  1530. else
  1531. Message(asmr_e_only_add_relocatable_symbol);
  1532. end;
  1533. if actasmtoken=AS_DOT then
  1534. begin
  1535. BuildRecordOffsetSize(tempstr,l,k,hs,false);
  1536. if (hs<>'') then
  1537. hssymtyp:=AT_FUNCTION
  1538. else
  1539. begin
  1540. str(l, tempstr);
  1541. expr:=expr + tempstr;
  1542. end
  1543. end
  1544. else
  1545. begin
  1546. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1547. delete(expr,length(expr),1);
  1548. end;
  1549. end;
  1550. { check if there are wrong operator used like / or mod etc. }
  1551. if (hs<>'') and
  1552. not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,
  1553. AS_LPAREN,AS_RPAREN,AS_RBRACKET,AS_END]) then
  1554. Message(asmr_e_only_add_relocatable_symbol);
  1555. end;
  1556. AS_END,
  1557. AS_SEPARATOR,
  1558. AS_COMMA:
  1559. break;
  1560. else
  1561. Begin
  1562. { write error only once. }
  1563. if not errorflag then
  1564. Message(asmr_e_invalid_constant_expression);
  1565. { consume tokens until we find COMMA or SEPARATOR }
  1566. Consume(actasmtoken);
  1567. errorflag:=TRUE;
  1568. end;
  1569. end;
  1570. Until false;
  1571. { calculate expression }
  1572. if not ErrorFlag then
  1573. value:=CalculateExpression(expr)
  1574. else
  1575. value:=0;
  1576. end;
  1577. function tattreader.BuildConstExpression(allowref,betweenbracket:boolean): tcgint;
  1578. var
  1579. l : tcgint;
  1580. hs : string;
  1581. hssymtyp : TAsmSymType;
  1582. begin
  1583. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs,hssymtyp);
  1584. if hs<>'' then
  1585. Message(asmr_e_relocatable_symbol_not_allowed);
  1586. BuildConstExpression:=l;
  1587. end;
  1588. Procedure tattreader.BuildConstantOperand(oper : toperand);
  1589. var
  1590. l : tcgint;
  1591. tempstr : string;
  1592. tempsymtyp : TAsmSymType;
  1593. begin
  1594. BuildConstSymbolExpression(false,false,true,l,tempstr,tempsymtyp);
  1595. if tempstr<>'' then
  1596. begin
  1597. oper.opr.typ:=OPR_SYMBOL;
  1598. oper.opr.symofs:=l;
  1599. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1600. end
  1601. else
  1602. begin
  1603. oper.opr.typ:=OPR_CONSTANT;
  1604. oper.opr.val:=aint(l);
  1605. end;
  1606. end;
  1607. procedure tattreader.BuildRva;
  1608. var
  1609. asmsymtyp : TAsmSymType;
  1610. asmsym: string;
  1611. value : tcgint;
  1612. ai:tai_const;
  1613. begin
  1614. repeat
  1615. case actasmtoken of
  1616. AS_INTNUM,
  1617. AS_PLUS,
  1618. AS_MINUS,
  1619. AS_LPAREN,
  1620. AS_ID :
  1621. Begin
  1622. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  1623. if asmsym<>'' then
  1624. begin
  1625. ai:=tai_const.create_type_sym(aitconst_rva_symbol,current_asmdata.RefAsmSymbol(asmsym,asmsymtyp));
  1626. ai.value:=value;
  1627. curlist.concat(ai);
  1628. end
  1629. else
  1630. Message(asmr_e_invalid_symbol_ref);
  1631. end;
  1632. AS_COMMA:
  1633. Consume(AS_COMMA);
  1634. AS_END,
  1635. AS_SEPARATOR:
  1636. break;
  1637. else
  1638. begin
  1639. Message(asmr_e_syn_constant);
  1640. RecoverConsume(false);
  1641. end
  1642. end; { end case }
  1643. until false;
  1644. end;
  1645. end.