ra386att.pas 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084
  1. {
  2. $Id$
  3. Copyright (c) 1997-99 by Carl Eric Codere and Peter Vreman
  4. Does the parsing for the AT&T styled inline assembler.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef TP}
  19. {$N+,E+}
  20. {$endif TP}
  21. Unit Ra386att;
  22. Interface
  23. uses
  24. tree;
  25. function assemble: ptree;
  26. Implementation
  27. Uses
  28. globtype,
  29. strings,cobjects,systems,verbose,globals,
  30. files,aasm,types,symconst,symtable,scanner,cpubase,
  31. {$ifdef NEWCG}
  32. cgbase,
  33. {$else}
  34. hcodegen,
  35. {$endif}
  36. rautils,ra386;
  37. type
  38. tasmtoken = (
  39. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  40. AS_REALNUM,AS_COMMA,AS_LPAREN,
  41. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  42. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,AS_DOLLAR,
  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,
  47. AS_DATA,AS_TEXT,AS_END,
  48. {------------------ Assembler Operators --------------------}
  49. AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR,AS_NOR);
  50. tasmkeyword = string[8];
  51. const
  52. { These tokens should be modified accordingly to the modifications }
  53. { in the different enumerations. }
  54. firstdirective = AS_DB;
  55. lastdirective = AS_END;
  56. _count_asmprefixes = 5;
  57. _count_asmspecialops = 25;
  58. _count_asmoverrides = 3;
  59. token2str : array[tasmtoken] of string[10]=(
  60. '','Label','LLabel','string','integer',
  61. 'float',',','(',
  62. ')',':','.','+','-','*',
  63. ';','identifier','register','opcode','/','$',
  64. '.byte','.word','.long','.quad','.globl',
  65. '.align','.balign','.p2align','.ascii',
  66. '.asciz','.lcomm','.comm','.single','.double','.tfloat',
  67. '.data','.text','END',
  68. '%','<<','>>','!','&','|','^','~');
  69. const
  70. newline = #10;
  71. firsttoken : boolean = TRUE;
  72. charcount : byte = 0;
  73. var
  74. _asmsorted,
  75. inexpression : boolean;
  76. curlist : paasmoutput;
  77. c : char;
  78. actasmtoken : tasmtoken;
  79. prevasmtoken : tasmtoken;
  80. actasmpattern : string;
  81. actopcode : tasmop;
  82. actasmregister : tregister;
  83. actopsize : topsize;
  84. actcondition : tasmcond;
  85. iasmops : ^op2strtable;
  86. iasmregs : ^reg2strtable;
  87. Procedure SetupTables;
  88. { creates uppercased symbol tables for speed access }
  89. var
  90. i : tasmop;
  91. j : tregister;
  92. Begin
  93. { opcodes }
  94. new(iasmops);
  95. for i:=firstop to lastop do
  96. iasmops^[i] := upper(att_op2str[i]);
  97. { registers }
  98. new(iasmregs);
  99. for j:=firstreg to lastreg do
  100. iasmregs^[j] := upper(att_reg2str[j]);
  101. end;
  102. {---------------------------------------------------------------------}
  103. { Routines for the tokenizing }
  104. {---------------------------------------------------------------------}
  105. function is_asmopcode(const s: string):boolean;
  106. const
  107. { We need first to check the long prefixes, else we get probs
  108. with things like movsbl }
  109. att_sizesuffixstr : array[0..8] of string[2] = (
  110. '','BW','BL','WL','B','W','L','Q','T'
  111. );
  112. att_sizesuffix : array[0..8] of topsize = (
  113. S_NO,S_BW,S_BL,S_WL,S_B,S_W,S_L,S_IQ,S_FX
  114. );
  115. var
  116. i : tasmop;
  117. cond : string[4];
  118. cnd : tasmcond;
  119. len,
  120. j,
  121. sufidx : longint;
  122. hid : string;
  123. Begin
  124. is_asmopcode:=FALSE;
  125. actopcode:=A_None;
  126. actcondition:=C_None;
  127. actopsize:=S_NO;
  128. { search for all possible suffixes }
  129. for sufidx:=low(att_sizesuffixstr) to high(att_sizesuffixstr) do
  130. begin
  131. len:=length(s)-length(att_sizesuffixstr[sufidx]);
  132. if copy(s,len+1,length(att_sizesuffixstr[sufidx]))=att_sizesuffixstr[sufidx] then
  133. begin
  134. { here we search the entire table... }
  135. hid:=copy(s,1,len);
  136. for i:=firstop to lastop do
  137. if (length(hid) > 0) and (hid=iasmops^[i]) then
  138. begin
  139. actopsize:=att_sizesuffix[sufidx];
  140. actopcode:=i;
  141. actasmtoken:=AS_OPCODE;
  142. is_asmopcode:=TRUE;
  143. exit;
  144. end;
  145. { not found, check condition opcodes }
  146. j:=0;
  147. while (j<CondAsmOps) do
  148. begin
  149. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  150. begin
  151. cond:=Copy(s,Length(CondAsmOpStr[j])+1,len-Length(CondAsmOpStr[j]));
  152. if cond<>'' then
  153. begin
  154. for cnd:=low(TasmCond) to high(TasmCond) do
  155. if Cond=Upper(cond2str[cnd]) then
  156. begin
  157. actopcode:=CondASmOp[j];
  158. actopsize:=att_sizesuffix[sufidx];
  159. actcondition:=cnd;
  160. actasmtoken:=AS_OPCODE;
  161. is_asmopcode:=TRUE;
  162. exit;
  163. end;
  164. end;
  165. end;
  166. inc(j);
  167. end;
  168. end;
  169. end;
  170. end;
  171. Function is_asmdirective(const s: string):boolean;
  172. var
  173. i : tasmtoken;
  174. hs : string;
  175. Begin
  176. { GNU as is also not casesensitive with this }
  177. hs:=lower(s);
  178. for i:=firstdirective to lastdirective do
  179. if hs=token2str[i] then
  180. begin
  181. actasmtoken:=i;
  182. is_asmdirective:=true;
  183. exit;
  184. end;
  185. is_asmdirective:=false;
  186. end;
  187. Function is_register(const s: string):boolean;
  188. Var
  189. i : tregister;
  190. Begin
  191. actasmregister:=R_NO;
  192. for i:=firstreg to lastreg do
  193. if s=iasmregs^[i] then
  194. begin
  195. actasmtoken:=AS_REGISTER;
  196. actasmregister:=i;
  197. is_register:=true;
  198. exit;
  199. end;
  200. is_register:=false;
  201. end;
  202. Function is_locallabel(const s: string):boolean;
  203. begin
  204. is_locallabel:=(length(s)>=2) and (s[1]='.') and (s[2]='L');
  205. end;
  206. Procedure GetToken;
  207. var
  208. forcelabel: boolean;
  209. errorflag : boolean;
  210. len : longint;
  211. begin
  212. { save old token and reset new token }
  213. prevasmtoken:=actasmtoken;
  214. actasmtoken:=AS_NONE;
  215. { reset }
  216. errorflag:=FALSE;
  217. forcelabel:=FALSE;
  218. actasmpattern:='';
  219. { while space and tab , continue scan... }
  220. while c in [' ',#9] do
  221. c:=current_scanner^.asmgetchar;
  222. { get token pos }
  223. if not (c in [newline,#13,'{',';']) then
  224. current_scanner^.gettokenpos;
  225. { Local Label, Label, Directive, Prefix or Opcode }
  226. if firsttoken and not(c in [newline,#13,'{',';']) then
  227. begin
  228. firsttoken:=FALSE;
  229. len:=0;
  230. { directive or local label }
  231. if c = '.' then
  232. begin
  233. inc(len);
  234. actasmpattern[len]:=c;
  235. { Let us point to the next character }
  236. c:=current_scanner^.asmgetchar;
  237. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  238. begin
  239. inc(len);
  240. actasmpattern[len]:=c;
  241. c:=current_scanner^.asmgetchar;
  242. end;
  243. actasmpattern[0]:=chr(len);
  244. { this is a local label... }
  245. if (c=':') and is_locallabel(actasmpattern) then
  246. Begin
  247. { local variables are case sensitive }
  248. actasmtoken:=AS_LLABEL;
  249. c:=current_scanner^.asmgetchar;
  250. firsttoken:=true;
  251. exit;
  252. end
  253. { must be a directive }
  254. else
  255. Begin
  256. { directives are case sensitive!! }
  257. if is_asmdirective(actasmpattern) then
  258. exit;
  259. Message1(asmr_e_not_directive_or_local_symbol,actasmpattern);
  260. end;
  261. end;
  262. { only opcodes and global labels are allowed now. }
  263. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  264. begin
  265. inc(len);
  266. actasmpattern[len]:=c;
  267. c:=current_scanner^.asmgetchar;
  268. end;
  269. actasmpattern[0]:=chr(len);
  270. { Label ? }
  271. if c = ':' then
  272. begin
  273. actasmtoken:=AS_LABEL;
  274. { let us point to the next character }
  275. c:=current_scanner^.asmgetchar;
  276. firsttoken:=true;
  277. exit;
  278. end;
  279. { Opcode ? }
  280. If is_asmopcode(upper(actasmpattern)) then
  281. Begin
  282. uppervar(actasmpattern);
  283. exit;
  284. end;
  285. { End of assemblerblock ? }
  286. if upper(actasmpattern) = 'END' then
  287. begin
  288. actasmtoken:=AS_END;
  289. exit;
  290. end;
  291. actasmtoken:=AS_NONE;
  292. end
  293. else { else firsttoken }
  294. { Here we must handle all possible cases }
  295. begin
  296. case c of
  297. '.' : { possiblities : - local label reference , such as in jmp @local1 }
  298. { - field of object/record }
  299. { - directive. }
  300. begin
  301. if (prevasmtoken=AS_ID) then
  302. begin
  303. c:=current_scanner^.asmgetchar;
  304. actasmtoken:=AS_DOT;
  305. exit;
  306. end;
  307. actasmpattern:=c;
  308. c:=current_scanner^.asmgetchar;
  309. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  310. begin
  311. actasmpattern:=actasmpattern + c;
  312. c:=current_scanner^.asmgetchar;
  313. end;
  314. if is_asmdirective(actasmpattern) then
  315. exit;
  316. { local label references and directives }
  317. { are case sensitive }
  318. actasmtoken:=AS_ID;
  319. exit;
  320. end;
  321. { identifier, register, prefix or directive }
  322. '_','A'..'Z','a'..'z':
  323. begin
  324. len:=0;
  325. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  326. begin
  327. inc(len);
  328. actasmpattern[len]:=c;
  329. c:=current_scanner^.asmgetchar;
  330. end;
  331. actasmpattern[0]:=chr(len);
  332. uppervar(actasmpattern);
  333. { Opcode, can only be when the previous was a prefix }
  334. If is_prefix(actopcode) and is_asmopcode(upper(actasmpattern)) then
  335. Begin
  336. uppervar(actasmpattern);
  337. exit;
  338. end;
  339. { check for end which is a reserved word unlike the opcodes }
  340. if actasmpattern = 'END' then
  341. Begin
  342. actasmtoken:=AS_END;
  343. exit;
  344. end;
  345. actasmtoken:=AS_ID;
  346. exit;
  347. end;
  348. '%' : { register or modulo }
  349. begin
  350. len:=1;
  351. actasmpattern[len]:='%';
  352. c:=current_scanner^.asmgetchar;
  353. while c in ['a'..'z','A'..'Z','0'..'9'] do
  354. Begin
  355. inc(len);
  356. actasmpattern[len]:=c;
  357. c:=current_scanner^.asmgetchar;
  358. end;
  359. actasmpattern[0]:=chr(len);
  360. uppervar(actasmpattern);
  361. if (actasmpattern = '%ST') and (c='(') then
  362. Begin
  363. actasmpattern:=actasmpattern+c;
  364. c:=current_scanner^.asmgetchar;
  365. if c in ['0'..'9'] then
  366. actasmpattern:=actasmpattern + c
  367. else
  368. Message(asmr_e_invalid_fpu_register);
  369. c:=current_scanner^.asmgetchar;
  370. if c <> ')' then
  371. Message(asmr_e_invalid_fpu_register)
  372. else
  373. Begin
  374. actasmpattern:=actasmpattern + c;
  375. c:=current_scanner^.asmgetchar; { let us point to next character. }
  376. end;
  377. end;
  378. if is_register(actasmpattern) then
  379. exit;
  380. Message(asmr_w_modulo_not_supported);
  381. end;
  382. '1'..'9': { integer number }
  383. begin
  384. len:=0;
  385. while c in ['0'..'9'] do
  386. Begin
  387. inc(len);
  388. actasmpattern[len]:=c;
  389. c:=current_scanner^.asmgetchar;
  390. end;
  391. actasmpattern[0]:=chr(len);
  392. actasmpattern:=tostr(ValDecimal(actasmpattern));
  393. actasmtoken:=AS_INTNUM;
  394. exit;
  395. end;
  396. '0' : { octal,hexa,real or binary number. }
  397. begin
  398. actasmpattern:=c;
  399. c:=current_scanner^.asmgetchar;
  400. case upcase(c) of
  401. 'B': { binary }
  402. Begin
  403. c:=current_scanner^.asmgetchar;
  404. while c in ['0','1'] do
  405. Begin
  406. actasmpattern:=actasmpattern + c;
  407. c:=current_scanner^.asmgetchar;
  408. end;
  409. actasmpattern:=tostr(ValBinary(actasmpattern));
  410. actasmtoken:=AS_INTNUM;
  411. exit;
  412. end;
  413. 'D': { real }
  414. Begin
  415. c:=current_scanner^.asmgetchar;
  416. { get ridd of the 0d }
  417. if (c in ['+','-']) then
  418. begin
  419. actasmpattern:=c;
  420. c:=current_scanner^.asmgetchar;
  421. end
  422. else
  423. actasmpattern:='';
  424. while c in ['0'..'9'] do
  425. Begin
  426. actasmpattern:=actasmpattern + c;
  427. c:=current_scanner^.asmgetchar;
  428. end;
  429. if c='.' then
  430. begin
  431. actasmpattern:=actasmpattern + c;
  432. c:=current_scanner^.asmgetchar;
  433. while c in ['0'..'9'] do
  434. Begin
  435. actasmpattern:=actasmpattern + c;
  436. c:=current_scanner^.asmgetchar;
  437. end;
  438. if upcase(c) = 'E' then
  439. begin
  440. actasmpattern:=actasmpattern + c;
  441. c:=current_scanner^.asmgetchar;
  442. if (c in ['+','-']) then
  443. begin
  444. actasmpattern:=actasmpattern + c;
  445. c:=current_scanner^.asmgetchar;
  446. end;
  447. while c in ['0'..'9'] do
  448. Begin
  449. actasmpattern:=actasmpattern + c;
  450. c:=current_scanner^.asmgetchar;
  451. end;
  452. end;
  453. actasmtoken:=AS_REALNUM;
  454. exit;
  455. end
  456. else
  457. begin
  458. Message1(asmr_e_invalid_float_const,actasmpattern+c);
  459. actasmtoken:=AS_NONE;
  460. end;
  461. end;
  462. 'X': { hexadecimal }
  463. Begin
  464. c:=current_scanner^.asmgetchar;
  465. while c in ['0'..'9','a'..'f','A'..'F'] do
  466. Begin
  467. actasmpattern:=actasmpattern + c;
  468. c:=current_scanner^.asmgetchar;
  469. end;
  470. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  471. actasmtoken:=AS_INTNUM;
  472. exit;
  473. end;
  474. '1'..'7': { octal }
  475. begin
  476. actasmpattern:=actasmpattern + c;
  477. while c in ['0'..'7'] do
  478. Begin
  479. actasmpattern:=actasmpattern + c;
  480. c:=current_scanner^.asmgetchar;
  481. end;
  482. actasmpattern:=tostr(ValOctal(actasmpattern));
  483. actasmtoken:=AS_INTNUM;
  484. exit;
  485. end;
  486. else { octal number zero value...}
  487. Begin
  488. actasmpattern:=tostr(ValOctal(actasmpattern));
  489. actasmtoken:=AS_INTNUM;
  490. exit;
  491. end;
  492. end; { end case }
  493. end;
  494. '&' :
  495. begin
  496. c:=current_scanner^.asmgetchar;
  497. actasmtoken:=AS_AND;
  498. end;
  499. '''' : { char }
  500. begin
  501. actasmpattern:='';
  502. repeat
  503. c:=current_scanner^.asmgetchar;
  504. case c of
  505. '\' :
  506. begin
  507. { copy also the next char so \" is parsed correctly }
  508. c:=current_scanner^.asmgetchar;
  509. actasmpattern:=actasmpattern+c;
  510. end;
  511. '''' :
  512. begin
  513. c:=current_scanner^.asmgetchar;
  514. break;
  515. end;
  516. newline:
  517. Message(scan_f_string_exceeds_line);
  518. else
  519. actasmpattern:=actasmpattern+c;
  520. end;
  521. until false;
  522. actasmpattern:=EscapeToPascal(actasmpattern);
  523. actasmtoken:=AS_STRING;
  524. exit;
  525. end;
  526. '"' : { string }
  527. begin
  528. actasmpattern:='';
  529. repeat
  530. c:=current_scanner^.asmgetchar;
  531. case c of
  532. '\' :
  533. begin
  534. { copy also the next char so \" is parsed correctly }
  535. c:=current_scanner^.asmgetchar;
  536. actasmpattern:=actasmpattern+c;
  537. end;
  538. '"' :
  539. begin
  540. c:=current_scanner^.asmgetchar;
  541. break;
  542. end;
  543. newline:
  544. Message(scan_f_string_exceeds_line);
  545. else
  546. actasmpattern:=actasmpattern+c;
  547. end;
  548. until false;
  549. actasmpattern:=EscapeToPascal(actasmpattern);
  550. actasmtoken:=AS_STRING;
  551. exit;
  552. end;
  553. '$' :
  554. begin
  555. actasmtoken:=AS_DOLLAR;
  556. c:=current_scanner^.asmgetchar;
  557. exit;
  558. end;
  559. ',' :
  560. begin
  561. actasmtoken:=AS_COMMA;
  562. c:=current_scanner^.asmgetchar;
  563. exit;
  564. end;
  565. '<' :
  566. begin
  567. actasmtoken:=AS_SHL;
  568. c:=current_scanner^.asmgetchar;
  569. if c = '<' then
  570. c:=current_scanner^.asmgetchar;
  571. exit;
  572. end;
  573. '>' :
  574. begin
  575. actasmtoken:=AS_SHL;
  576. c:=current_scanner^.asmgetchar;
  577. if c = '>' then
  578. c:=current_scanner^.asmgetchar;
  579. exit;
  580. end;
  581. '|' :
  582. begin
  583. actasmtoken:=AS_OR;
  584. c:=current_scanner^.asmgetchar;
  585. exit;
  586. end;
  587. '^' :
  588. begin
  589. actasmtoken:=AS_XOR;
  590. c:=current_scanner^.asmgetchar;
  591. exit;
  592. end;
  593. '!' :
  594. begin
  595. Message(asmr_e_nor_not_supported);
  596. c:=current_scanner^.asmgetchar;
  597. actasmtoken:=AS_NONE;
  598. exit;
  599. end;
  600. '(' :
  601. begin
  602. actasmtoken:=AS_LPAREN;
  603. c:=current_scanner^.asmgetchar;
  604. exit;
  605. end;
  606. ')' :
  607. begin
  608. actasmtoken:=AS_RPAREN;
  609. c:=current_scanner^.asmgetchar;
  610. exit;
  611. end;
  612. ':' :
  613. begin
  614. actasmtoken:=AS_COLON;
  615. c:=current_scanner^.asmgetchar;
  616. exit;
  617. end;
  618. '+' :
  619. begin
  620. actasmtoken:=AS_PLUS;
  621. c:=current_scanner^.asmgetchar;
  622. exit;
  623. end;
  624. '-' :
  625. begin
  626. actasmtoken:=AS_MINUS;
  627. c:=current_scanner^.asmgetchar;
  628. exit;
  629. end;
  630. '*' :
  631. begin
  632. actasmtoken:=AS_STAR;
  633. c:=current_scanner^.asmgetchar;
  634. exit;
  635. end;
  636. '/' :
  637. begin
  638. c:=current_scanner^.asmgetchar;
  639. actasmtoken:=AS_SLASH;
  640. exit;
  641. end;
  642. '{',#13,newline,';' :
  643. begin
  644. { the comment is read by asmgetchar }
  645. c:=current_scanner^.asmgetchar;
  646. firsttoken:=TRUE;
  647. actasmtoken:=AS_SEPARATOR;
  648. exit;
  649. end;
  650. else
  651. Begin
  652. Message(scan_f_illegal_char);
  653. end;
  654. end;
  655. end;
  656. end;
  657. function consume(t : tasmtoken):boolean;
  658. begin
  659. Consume:=true;
  660. if t<>actasmtoken then
  661. begin
  662. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  663. Consume:=false;
  664. end;
  665. repeat
  666. gettoken;
  667. until actasmtoken<>AS_NONE;
  668. end;
  669. procedure RecoverConsume(allowcomma:boolean);
  670. begin
  671. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  672. begin
  673. if allowcomma and (actasmtoken=AS_COMMA) then
  674. break;
  675. Consume(actasmtoken);
  676. end;
  677. end;
  678. {*****************************************************************************
  679. Parsing Helpers
  680. *****************************************************************************}
  681. Procedure BuildRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  682. { Description: This routine builds up a record offset after a AS_DOT }
  683. { token is encountered. }
  684. { On entry actasmtoken should be equal to AS_DOT }
  685. var
  686. s : string;
  687. Begin
  688. offset:=0;
  689. size:=0;
  690. s:=expr;
  691. while (actasmtoken=AS_DOT) do
  692. begin
  693. Consume(AS_DOT);
  694. if actasmtoken=AS_ID then
  695. s:=s+'.'+actasmpattern;
  696. if not Consume(AS_ID) then
  697. begin
  698. RecoverConsume(true);
  699. break;
  700. end;
  701. end;
  702. if not GetRecordOffsetSize(s,offset,size) then
  703. Message(asmr_e_building_record_offset);
  704. end;
  705. Procedure BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:longint;var asmsym:string);
  706. var
  707. hs,tempstr,expr : string;
  708. parenlevel,l,k : longint;
  709. errorflag : boolean;
  710. prevtok : tasmtoken;
  711. sym : psym;
  712. hl : PAsmLabel;
  713. Begin
  714. asmsym:='';
  715. value:=0;
  716. errorflag:=FALSE;
  717. tempstr:='';
  718. expr:='';
  719. inexpression:=TRUE;
  720. parenlevel:=0;
  721. Repeat
  722. Case actasmtoken of
  723. AS_LPAREN:
  724. Begin
  725. { Exit if ref? }
  726. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  727. break;
  728. Consume(AS_LPAREN);
  729. expr:=expr + '(';
  730. inc(parenlevel);
  731. end;
  732. AS_RPAREN:
  733. Begin
  734. { end of ref ? }
  735. if (parenlevel=0) and betweenbracket then
  736. break;
  737. Consume(AS_RPAREN);
  738. expr:=expr + ')';
  739. dec(parenlevel);
  740. end;
  741. AS_SHL:
  742. Begin
  743. Consume(AS_SHL);
  744. expr:=expr + '<';
  745. end;
  746. AS_SHR:
  747. Begin
  748. Consume(AS_SHR);
  749. expr:=expr + '>';
  750. end;
  751. AS_SLASH:
  752. Begin
  753. Consume(AS_SLASH);
  754. expr:=expr + '/';
  755. end;
  756. AS_MOD:
  757. Begin
  758. Consume(AS_MOD);
  759. expr:=expr + '%';
  760. end;
  761. AS_STAR:
  762. Begin
  763. Consume(AS_STAR);
  764. expr:=expr + '*';
  765. end;
  766. AS_PLUS:
  767. Begin
  768. Consume(AS_PLUS);
  769. expr:=expr + '+';
  770. end;
  771. AS_MINUS:
  772. Begin
  773. Consume(AS_MINUS);
  774. expr:=expr + '-';
  775. end;
  776. AS_AND:
  777. Begin
  778. Consume(AS_AND);
  779. expr:=expr + '&';
  780. end;
  781. AS_NOT:
  782. Begin
  783. Consume(AS_NOT);
  784. expr:=expr + '~';
  785. end;
  786. AS_XOR:
  787. Begin
  788. Consume(AS_XOR);
  789. expr:=expr + '^';
  790. end;
  791. AS_OR:
  792. Begin
  793. Consume(AS_OR);
  794. expr:=expr + '|';
  795. end;
  796. AS_INTNUM:
  797. Begin
  798. expr:=expr + actasmpattern;
  799. Consume(AS_INTNUM);
  800. end;
  801. AS_DOLLAR:
  802. begin
  803. Consume(AS_DOLLAR);
  804. if actasmtoken<>AS_ID then
  805. Message(asmr_e_dollar_without_identifier);
  806. end;
  807. AS_STRING:
  808. Begin
  809. l:=0;
  810. case Length(actasmpattern) of
  811. 1 :
  812. l:=ord(actasmpattern[1]);
  813. 2 :
  814. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  815. 3 :
  816. l:=ord(actasmpattern[3]) +
  817. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  818. 4 :
  819. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  820. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  821. else
  822. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  823. end;
  824. str(l, tempstr);
  825. expr:=expr + tempstr;
  826. Consume(AS_STRING);
  827. end;
  828. AS_ID:
  829. Begin
  830. tempstr:=actasmpattern;
  831. prevtok:=prevasmtoken;
  832. consume(AS_ID);
  833. if actasmtoken=AS_DOT then
  834. begin
  835. BuildRecordOffsetSize(tempstr,l,k);
  836. str(l, tempstr);
  837. expr:=expr + tempstr;
  838. end
  839. else
  840. if SearchIConstant(tempstr,l) then
  841. begin
  842. str(l, tempstr);
  843. expr:=expr + tempstr;
  844. end
  845. else
  846. begin
  847. hs:='';
  848. if is_locallabel(tempstr) then
  849. begin
  850. CreateLocalLabel(tempstr,hl,false);
  851. hs:=hl^.name
  852. end
  853. else
  854. if SearchLabel(tempstr,hl,false) then
  855. hs:=hl^.name
  856. else
  857. begin
  858. getsym(tempstr,false);
  859. sym:=srsym;
  860. if assigned(sym) then
  861. begin
  862. if sym^.owner^.symtabletype in [localsymtable,parasymtable] then
  863. Message(asmr_e_no_local_or_para_allowed);
  864. case srsym^.typ of
  865. varsym :
  866. hs:=pvarsym(srsym)^.mangledname;
  867. typedconstsym :
  868. hs:=ptypedconstsym(srsym)^.mangledname;
  869. procsym :
  870. hs:=pprocsym(srsym)^.mangledname;
  871. else
  872. Message(asmr_e_wrong_sym_type);
  873. end;
  874. end
  875. else
  876. Message1(sym_e_unknown_id,tempstr);
  877. end;
  878. { symbol found? }
  879. if hs<>'' then
  880. begin
  881. if needofs and (prevtok<>AS_DOLLAR) then
  882. Message(asmr_e_need_offset);
  883. if asmsym='' then
  884. asmsym:=hs
  885. else
  886. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  887. if (expr='') or (expr[length(expr)]='+') then
  888. begin
  889. delete(expr,length(expr),1);
  890. if not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END]) then
  891. Message(asmr_e_only_add_relocatable_symbol);
  892. end
  893. else
  894. Message(asmr_e_only_add_relocatable_symbol);
  895. end
  896. else
  897. begin
  898. { Error recovery }
  899. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  900. delete(expr,length(expr),1);
  901. end;
  902. end;
  903. end;
  904. AS_END,
  905. AS_SEPARATOR,
  906. AS_COMMA:
  907. Begin
  908. break;
  909. end;
  910. else
  911. Begin
  912. { write error only once. }
  913. if not errorflag then
  914. Message(asmr_e_invalid_constant_expression);
  915. { consume tokens until we find COMMA or SEPARATOR }
  916. Consume(actasmtoken);
  917. errorflag:=TRUE;
  918. end;
  919. end;
  920. Until false;
  921. { calculate expression }
  922. if not ErrorFlag then
  923. value:=CalculateExpression(expr)
  924. else
  925. value:=0;
  926. { no longer in an expression }
  927. inexpression:=FALSE;
  928. end;
  929. Function BuildConstExpression(allowref,betweenbracket:boolean): longint;
  930. var
  931. l : longint;
  932. hs : string;
  933. begin
  934. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs);
  935. if hs<>'' then
  936. Message(asmr_e_relocatable_symbol_not_allowed);
  937. BuildConstExpression:=l;
  938. end;
  939. {****************************************************************************
  940. T386ATTOperand
  941. ****************************************************************************}
  942. type
  943. P386ATTOperand=^T386ATTOperand;
  944. T386ATTOperand=object(T386Operand)
  945. Procedure BuildOperand;virtual;
  946. private
  947. Procedure BuildReference;
  948. Procedure BuildConstant;
  949. end;
  950. Procedure T386ATTOperand.BuildReference;
  951. procedure Consume_RParen;
  952. begin
  953. if actasmtoken <> AS_RPAREN then
  954. Begin
  955. Message(asmr_e_invalid_reference_syntax);
  956. RecoverConsume(true);
  957. end
  958. else
  959. begin
  960. Consume(AS_RPAREN);
  961. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR,AS_END]) then
  962. Begin
  963. Message(asmr_e_invalid_reference_syntax);
  964. RecoverConsume(true);
  965. end;
  966. end;
  967. end;
  968. procedure Consume_Scale;
  969. var
  970. l : longint;
  971. begin
  972. { we have to process the scaling }
  973. l:=BuildConstExpression(false,true);
  974. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) then
  975. opr.ref.scalefactor:=l
  976. else
  977. Begin
  978. Message(asmr_e_wrong_scale_factor);
  979. opr.ref.scalefactor:=0;
  980. end;
  981. end;
  982. Begin
  983. Consume(AS_LPAREN);
  984. Case actasmtoken of
  985. AS_INTNUM,
  986. AS_MINUS,
  987. AS_PLUS: { absolute offset, such as fs:(0x046c) }
  988. Begin
  989. { offset(offset) is invalid }
  990. If opr.Ref.Offset <> 0 Then
  991. Begin
  992. Message(asmr_e_invalid_reference_syntax);
  993. RecoverConsume(true);
  994. End
  995. Else
  996. Begin
  997. opr.Ref.Offset:=BuildConstExpression(false,true);
  998. Consume_RParen;
  999. end;
  1000. exit;
  1001. End;
  1002. AS_REGISTER: { (reg ... }
  1003. Begin
  1004. { Check if there is already a base (mostly ebp,esp) than this is
  1005. not allowed,becuase it will give crashing code }
  1006. if opr.ref.base<>R_NO then
  1007. Message(asmr_e_cannot_index_relative_var);
  1008. opr.ref.base:=actasmregister;
  1009. Consume(AS_REGISTER);
  1010. { can either be a register or a right parenthesis }
  1011. { (reg) }
  1012. if actasmtoken=AS_RPAREN then
  1013. Begin
  1014. Consume_RParen;
  1015. exit;
  1016. end;
  1017. { (reg,reg .. }
  1018. Consume(AS_COMMA);
  1019. if actasmtoken=AS_REGISTER then
  1020. Begin
  1021. opr.ref.index:=actasmregister;
  1022. Consume(AS_REGISTER);
  1023. { check for scaling ... }
  1024. case actasmtoken of
  1025. AS_RPAREN:
  1026. Begin
  1027. Consume_RParen;
  1028. exit;
  1029. end;
  1030. AS_COMMA:
  1031. Begin
  1032. Consume(AS_COMMA);
  1033. Consume_Scale;
  1034. Consume_RParen;
  1035. end;
  1036. else
  1037. Begin
  1038. Message(asmr_e_invalid_reference_syntax);
  1039. RecoverConsume(false);
  1040. end;
  1041. end; { end case }
  1042. end
  1043. else
  1044. Begin
  1045. Message(asmr_e_invalid_reference_syntax);
  1046. RecoverConsume(false);
  1047. end;
  1048. end; {end case }
  1049. AS_COMMA: { (, ... can either be scaling, or index }
  1050. Begin
  1051. Consume(AS_COMMA);
  1052. { Index }
  1053. if (actasmtoken=AS_REGISTER) then
  1054. Begin
  1055. opr.ref.index:=actasmregister;
  1056. Consume(AS_REGISTER);
  1057. { check for scaling ... }
  1058. case actasmtoken of
  1059. AS_RPAREN:
  1060. Begin
  1061. Consume_RParen;
  1062. exit;
  1063. end;
  1064. AS_COMMA:
  1065. Begin
  1066. Consume(AS_COMMA);
  1067. Consume_Scale;
  1068. Consume_RParen;
  1069. end;
  1070. else
  1071. Begin
  1072. Message(asmr_e_invalid_reference_syntax);
  1073. RecoverConsume(false);
  1074. end;
  1075. end; {end case }
  1076. end
  1077. { Scaling }
  1078. else
  1079. Begin
  1080. Consume_Scale;
  1081. Consume_RParen;
  1082. exit;
  1083. end;
  1084. end;
  1085. else
  1086. Begin
  1087. Message(asmr_e_invalid_reference_syntax);
  1088. RecoverConsume(false);
  1089. end;
  1090. end;
  1091. end;
  1092. Procedure T386ATTOperand.BuildConstant;
  1093. var
  1094. l : longint;
  1095. tempstr : string;
  1096. begin
  1097. BuildConstSymbolExpression(false,false,true,l,tempstr);
  1098. if tempstr<>'' then
  1099. begin
  1100. opr.typ:=OPR_SYMBOL;
  1101. opr.symofs:=l;
  1102. opr.symbol:=newasmsymbol(tempstr);
  1103. end
  1104. else
  1105. begin
  1106. opr.typ:=OPR_CONSTANT;
  1107. opr.val:=l;
  1108. end;
  1109. end;
  1110. Procedure T386ATTOperand.BuildOperand;
  1111. var
  1112. expr : string;
  1113. procedure AddLabelOperand(hl:pasmlabel);
  1114. begin
  1115. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  1116. is_calljmp(actopcode) then
  1117. begin
  1118. opr.typ:=OPR_SYMBOL;
  1119. opr.symbol:=hl;
  1120. end
  1121. else
  1122. begin
  1123. InitRef;
  1124. opr.ref.symbol:=hl;
  1125. end;
  1126. end;
  1127. procedure MaybeRecordOffset;
  1128. var
  1129. l,
  1130. toffset,
  1131. tsize : longint;
  1132. begin
  1133. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  1134. exit;
  1135. l:=0;
  1136. if actasmtoken=AS_DOT then
  1137. begin
  1138. if expr<>'' then
  1139. begin
  1140. BuildRecordOffsetSize(expr,toffset,tsize);
  1141. inc(l,toffset);
  1142. SetSize(tsize);
  1143. end;
  1144. end;
  1145. if actasmtoken in [AS_PLUS,AS_MINUS] then
  1146. inc(l,BuildConstExpression(true,false));
  1147. if opr.typ=OPR_REFERENCE then
  1148. begin
  1149. if opr.ref.options=ref_parafixup then
  1150. Message(asmr_e_cannot_access_field_directly_for_parameters);
  1151. inc(opr.ref.offset,l)
  1152. end
  1153. else
  1154. inc(opr.val,l);
  1155. end;
  1156. function MaybeBuildReference:boolean;
  1157. { Try to create a reference, if not a reference is found then false
  1158. is returned }
  1159. begin
  1160. MaybeBuildReference:=true;
  1161. case actasmtoken of
  1162. AS_INTNUM,
  1163. AS_MINUS,
  1164. AS_PLUS:
  1165. Begin
  1166. opr.ref.offset:=BuildConstExpression(True,False);
  1167. if actasmtoken<>AS_LPAREN then
  1168. Message(asmr_e_invalid_reference_syntax)
  1169. else
  1170. BuildReference;
  1171. end;
  1172. AS_LPAREN:
  1173. BuildReference;
  1174. AS_ID: { only a variable is allowed ... }
  1175. Begin
  1176. if not SetupVar(actasmpattern) then
  1177. Message(asmr_e_invalid_reference_syntax);
  1178. Consume(AS_ID);
  1179. case actasmtoken of
  1180. AS_END,
  1181. AS_SEPARATOR,
  1182. AS_COMMA: ;
  1183. AS_LPAREN: BuildReference;
  1184. else
  1185. Begin
  1186. Message(asmr_e_invalid_reference_syntax);
  1187. Consume(actasmtoken);
  1188. end;
  1189. end; {end case }
  1190. end;
  1191. else
  1192. MaybeBuildReference:=false;
  1193. end; { end case }
  1194. end;
  1195. var
  1196. tempstr : string;
  1197. tempreg : tregister;
  1198. hl : PAsmLabel;
  1199. l : longint;
  1200. Begin
  1201. tempstr:='';
  1202. expr:='';
  1203. case actasmtoken of
  1204. AS_LPAREN: { Memory reference or constant expression }
  1205. Begin
  1206. InitRef;
  1207. BuildReference;
  1208. end;
  1209. AS_DOLLAR: { Constant expression }
  1210. Begin
  1211. Consume(AS_DOLLAR);
  1212. BuildConstant;
  1213. end;
  1214. AS_INTNUM,
  1215. AS_MINUS,
  1216. AS_PLUS:
  1217. Begin
  1218. { Constant memory offset }
  1219. { This must absolutely be followed by ( }
  1220. InitRef;
  1221. opr.ref.offset:=BuildConstExpression(True,False);
  1222. if actasmtoken<>AS_LPAREN then
  1223. Message(asmr_e_invalid_reference_syntax)
  1224. else
  1225. BuildReference;
  1226. end;
  1227. AS_STAR: { Call from memory address }
  1228. Begin
  1229. Consume(AS_STAR);
  1230. if actasmtoken=AS_REGISTER then
  1231. begin
  1232. opr.typ:=OPR_REGISTER;
  1233. opr.reg:=actasmregister;
  1234. size:=reg_2_opsize[actasmregister];
  1235. Consume(AS_REGISTER);
  1236. end
  1237. else
  1238. begin
  1239. InitRef;
  1240. if not MaybeBuildReference then
  1241. Message(asmr_e_syn_operand);
  1242. end;
  1243. { this is only allowed for call's and jmp's }
  1244. if not is_calljmp(actopcode) then
  1245. Message(asmr_e_syn_operand);
  1246. end;
  1247. AS_ID: { A constant expression, or a Variable ref. }
  1248. Begin
  1249. { Local Label ? }
  1250. if is_locallabel(actasmpattern) then
  1251. begin
  1252. CreateLocalLabel(actasmpattern,hl,false);
  1253. Consume(AS_ID);
  1254. AddLabelOperand(hl);
  1255. end
  1256. else
  1257. { Check for label }
  1258. if SearchLabel(actasmpattern,hl,false) then
  1259. begin
  1260. Consume(AS_ID);
  1261. AddLabelOperand(hl);
  1262. end
  1263. else
  1264. { probably a variable or normal expression }
  1265. { or a procedure (such as in CALL ID) }
  1266. Begin
  1267. { is it a constant ? }
  1268. if SearchIConstant(actasmpattern,l) then
  1269. Begin
  1270. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1271. Message(asmr_e_invalid_operand_type);
  1272. BuildConstant;
  1273. end
  1274. else
  1275. begin
  1276. InitRef;
  1277. if SetupVar(actasmpattern) then
  1278. begin
  1279. expr:=actasmpattern;
  1280. Consume(AS_ID);
  1281. MaybeRecordOffset;
  1282. { add a constant expression? }
  1283. if (actasmtoken=AS_PLUS) then
  1284. begin
  1285. l:=BuildConstExpression(true,false);
  1286. if opr.typ=OPR_CONSTANT then
  1287. inc(opr.val,l)
  1288. else
  1289. inc(opr.ref.offset,l);
  1290. end
  1291. end
  1292. else
  1293. Begin
  1294. { look for special symbols ... }
  1295. if actasmpattern = '__RESULT' then
  1296. SetUpResult
  1297. else
  1298. if actasmpattern = '__SELF' then
  1299. SetupSelf
  1300. else
  1301. if actasmpattern = '__OLDEBP' then
  1302. SetupOldEBP
  1303. else
  1304. { check for direct symbolic names }
  1305. { only if compiling the system unit }
  1306. if (cs_compilesystem in aktmoduleswitches) then
  1307. begin
  1308. if not SetupDirectVar(actasmpattern) then
  1309. Begin
  1310. { not found, finally ... add it anyways ... }
  1311. Message1(asmr_w_id_supposed_external,actasmpattern);
  1312. opr.ref.symbol:=newasmsymbol(actasmpattern);
  1313. end;
  1314. end
  1315. else
  1316. Message1(sym_e_unknown_id,actasmpattern);
  1317. Consume(AS_ID);
  1318. end;
  1319. end;
  1320. end;
  1321. { Do we have a indexing reference, then parse it also }
  1322. if actasmtoken=AS_LPAREN then
  1323. begin
  1324. if (opr.typ=OPR_CONSTANT) then
  1325. begin
  1326. l:=opr.val;
  1327. opr.typ:=OPR_REFERENCE;
  1328. reset_reference(opr.Ref);
  1329. opr.Ref.Offset:=l;
  1330. end;
  1331. BuildReference;
  1332. end;
  1333. end;
  1334. AS_REGISTER: { Register, a variable reference or a constant reference }
  1335. Begin
  1336. { save the type of register used. }
  1337. tempreg:=actasmregister;
  1338. Consume(AS_REGISTER);
  1339. if actasmtoken = AS_COLON then
  1340. Begin
  1341. Consume(AS_COLON);
  1342. InitRef;
  1343. opr.ref.segment:=tempreg;
  1344. { This must absolutely be followed by a reference }
  1345. if not MaybeBuildReference then
  1346. Begin
  1347. Message(asmr_e_invalid_seg_override);
  1348. Consume(actasmtoken);
  1349. end;
  1350. end
  1351. { Simple register }
  1352. else if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1353. Begin
  1354. if not (opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1355. Message(asmr_e_invalid_operand_type);
  1356. opr.typ:=OPR_REGISTER;
  1357. opr.reg:=tempreg;
  1358. size:=reg_2_opsize[tempreg];
  1359. end
  1360. else
  1361. Message(asmr_e_syn_operand);
  1362. end;
  1363. AS_END,
  1364. AS_SEPARATOR,
  1365. AS_COMMA: ;
  1366. else
  1367. Begin
  1368. Message(asmr_e_syn_operand);
  1369. Consume(actasmtoken);
  1370. end;
  1371. end; { end case }
  1372. end;
  1373. {*****************************************************************************
  1374. T386ATTInstruction
  1375. *****************************************************************************}
  1376. type
  1377. P386AttInstruction=^T386AttInstruction;
  1378. T386AttInstruction=object(T386Instruction)
  1379. procedure InitOperands;virtual;
  1380. procedure BuildOpcode;virtual;
  1381. end;
  1382. procedure T386AttInstruction.InitOperands;
  1383. var
  1384. i : longint;
  1385. begin
  1386. for i:=1to 3 do
  1387. Operands[i]:=new(P386AttOperand,Init);
  1388. end;
  1389. Procedure T386AttInstruction.BuildOpCode;
  1390. var
  1391. operandnum : longint;
  1392. PrefixOp,OverrideOp: tasmop;
  1393. expr : string;
  1394. Begin
  1395. expr:='';
  1396. PrefixOp:=A_None;
  1397. OverrideOp:=A_None;
  1398. { prefix seg opcode / prefix opcode }
  1399. repeat
  1400. if is_prefix(actopcode) then
  1401. begin
  1402. PrefixOp:=ActOpcode;
  1403. opcode:=ActOpcode;
  1404. condition:=ActCondition;
  1405. opsize:=ActOpsize;
  1406. ConcatInstruction(curlist);
  1407. Consume(AS_OPCODE);
  1408. end
  1409. else
  1410. if is_override(actopcode) then
  1411. begin
  1412. OverrideOp:=ActOpcode;
  1413. opcode:=ActOpcode;
  1414. condition:=ActCondition;
  1415. opsize:=ActOpsize;
  1416. ConcatInstruction(curlist);
  1417. Consume(AS_OPCODE);
  1418. end
  1419. else
  1420. break;
  1421. { allow for newline as in gas styled syntax }
  1422. while actasmtoken=AS_SEPARATOR do
  1423. Consume(AS_SEPARATOR);
  1424. until (actasmtoken<>AS_OPCODE);
  1425. { opcode }
  1426. if (actasmtoken <> AS_OPCODE) then
  1427. Begin
  1428. Message(asmr_e_invalid_or_missing_opcode);
  1429. RecoverConsume(true);
  1430. exit;
  1431. end;
  1432. { Fill the instr object with the current state }
  1433. Opcode:=ActOpcode;
  1434. condition:=ActCondition;
  1435. opsize:=ActOpsize;
  1436. { Valid combination of prefix/override and instruction ? }
  1437. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1438. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1439. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1440. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1441. { We are reading operands, so opcode will be an AS_ID }
  1442. operandnum:=1;
  1443. Consume(AS_OPCODE);
  1444. { Zero operand opcode ? }
  1445. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1446. begin
  1447. operandnum:=0;
  1448. exit;
  1449. end;
  1450. { Read the operands }
  1451. repeat
  1452. case actasmtoken of
  1453. AS_COMMA: { Operand delimiter }
  1454. Begin
  1455. if operandnum > MaxOperands then
  1456. Message(asmr_e_too_many_operands)
  1457. else
  1458. Inc(operandnum);
  1459. Consume(AS_COMMA);
  1460. end;
  1461. AS_SEPARATOR,
  1462. AS_END : { End of asm operands for this opcode }
  1463. begin
  1464. break;
  1465. end;
  1466. else
  1467. Operands[operandnum]^.BuildOperand;
  1468. end; { end case }
  1469. until false;
  1470. Ops:=operandnum;
  1471. end;
  1472. Procedure BuildConstant(maxvalue: longint);
  1473. var
  1474. strlength: byte;
  1475. asmsym,
  1476. expr: string;
  1477. value : longint;
  1478. Begin
  1479. Repeat
  1480. Case actasmtoken of
  1481. AS_STRING:
  1482. Begin
  1483. if maxvalue = $ff then
  1484. strlength:=1
  1485. else
  1486. Message(asmr_e_string_not_allowed_as_const);
  1487. expr:=actasmpattern;
  1488. if length(expr) > 1 then
  1489. Message(asmr_e_string_not_allowed_as_const);
  1490. Consume(AS_STRING);
  1491. Case actasmtoken of
  1492. AS_COMMA: Consume(AS_COMMA);
  1493. AS_END,
  1494. AS_SEPARATOR: ;
  1495. else
  1496. Message(asmr_e_invalid_string_expression);
  1497. end; { end case }
  1498. ConcatString(curlist,expr);
  1499. end;
  1500. AS_INTNUM,
  1501. AS_PLUS,
  1502. AS_MINUS,
  1503. AS_LPAREN,
  1504. AS_NOT,
  1505. AS_ID :
  1506. Begin
  1507. BuildConstSymbolExpression(false,false,false,value,asmsym);
  1508. if asmsym<>'' then
  1509. begin
  1510. if maxvalue<>$ffffffff then
  1511. Message(asmr_w_32bit_const_for_address);
  1512. ConcatConstSymbol(curlist,asmsym,value)
  1513. end
  1514. else
  1515. ConcatConstant(curlist,value,maxvalue);
  1516. end;
  1517. AS_COMMA:
  1518. Consume(AS_COMMA);
  1519. AS_END,
  1520. AS_SEPARATOR:
  1521. break;
  1522. else
  1523. begin
  1524. Message(asmr_e_syn_constant);
  1525. RecoverConsume(false);
  1526. end
  1527. end; { end case }
  1528. Until false;
  1529. end;
  1530. Procedure BuildRealConstant(typ : tfloattype);
  1531. var
  1532. expr : string;
  1533. r : bestreal;
  1534. code : integer;
  1535. negativ : boolean;
  1536. errorflag: boolean;
  1537. Begin
  1538. errorflag:=FALSE;
  1539. Repeat
  1540. negativ:=false;
  1541. expr:='';
  1542. if actasmtoken=AS_PLUS then
  1543. Consume(AS_PLUS)
  1544. else
  1545. if actasmtoken=AS_MINUS then
  1546. begin
  1547. negativ:=true;
  1548. consume(AS_MINUS);
  1549. end;
  1550. Case actasmtoken of
  1551. AS_INTNUM:
  1552. Begin
  1553. expr:=actasmpattern;
  1554. Consume(AS_INTNUM);
  1555. if negativ then
  1556. expr:='-'+expr;
  1557. val(expr,r,code);
  1558. if code<>0 then
  1559. Begin
  1560. r:=0;
  1561. Message(asmr_e_invalid_float_expr);
  1562. End;
  1563. ConcatRealConstant(curlist,r,typ);
  1564. end;
  1565. AS_REALNUM:
  1566. Begin
  1567. expr:=actasmpattern;
  1568. Consume(AS_REALNUM);
  1569. { in ATT syntax you have 0d in front of the real }
  1570. { should this be forced ? yes i think so, as to }
  1571. { conform to gas as much as possible. }
  1572. if (expr[1]='0') and (upper(expr[2])='D') then
  1573. Delete(expr,1,2);
  1574. if negativ then
  1575. expr:='-'+expr;
  1576. val(expr,r,code);
  1577. if code<>0 then
  1578. Begin
  1579. r:=0;
  1580. Message(asmr_e_invalid_float_expr);
  1581. End;
  1582. ConcatRealConstant(curlist,r,typ);
  1583. end;
  1584. AS_COMMA:
  1585. begin
  1586. Consume(AS_COMMA);
  1587. end;
  1588. AS_END,
  1589. AS_SEPARATOR:
  1590. begin
  1591. break;
  1592. end;
  1593. else
  1594. Begin
  1595. Consume(actasmtoken);
  1596. if not errorflag then
  1597. Message(asmr_e_invalid_float_expr);
  1598. errorflag:=TRUE;
  1599. end;
  1600. end;
  1601. Until false;
  1602. end;
  1603. Procedure BuildStringConstant(asciiz: boolean);
  1604. var
  1605. expr: string;
  1606. errorflag : boolean;
  1607. Begin
  1608. errorflag:=FALSE;
  1609. Repeat
  1610. Case actasmtoken of
  1611. AS_STRING:
  1612. Begin
  1613. expr:=actasmpattern;
  1614. if asciiz then
  1615. expr:=expr+#0;
  1616. ConcatPasString(curlist,expr);
  1617. Consume(AS_STRING);
  1618. end;
  1619. AS_COMMA:
  1620. begin
  1621. Consume(AS_COMMA);
  1622. end;
  1623. AS_END,
  1624. AS_SEPARATOR:
  1625. begin
  1626. break;
  1627. end;
  1628. else
  1629. Begin
  1630. Consume(actasmtoken);
  1631. if not errorflag then
  1632. Message(asmr_e_invalid_string_expression);
  1633. errorflag:=TRUE;
  1634. end;
  1635. end;
  1636. Until false;
  1637. end;
  1638. Function Assemble: Ptree;
  1639. Var
  1640. hl : PAsmLabel;
  1641. commname : string;
  1642. lastsec : tsection;
  1643. l1,l2 : longint;
  1644. instr : T386ATTInstruction;
  1645. Begin
  1646. Message1(asmr_d_start_reading,'AT&T');
  1647. firsttoken:=TRUE;
  1648. if assigned(procinfo^.retdef) and
  1649. (is_fpu(procinfo^.retdef) or
  1650. ret_in_acc(procinfo^.retdef)) then
  1651. procinfo^.funcret_is_valid:=true;
  1652. { sets up all opcode and register tables in uppercase }
  1653. if not _asmsorted then
  1654. Begin
  1655. SetupTables;
  1656. _asmsorted:=TRUE;
  1657. end;
  1658. curlist:=new(paasmoutput,init);
  1659. lastsec:=sec_code;
  1660. { setup label linked list }
  1661. new(LocalLabelList,Init);
  1662. { start tokenizer }
  1663. c:=current_scanner^.asmgetchar;
  1664. gettoken;
  1665. { main loop }
  1666. repeat
  1667. case actasmtoken of
  1668. AS_LLABEL:
  1669. Begin
  1670. if CreateLocalLabel(actasmpattern,hl,true) then
  1671. ConcatLabel(curlist,hl);
  1672. Consume(AS_LLABEL);
  1673. end;
  1674. AS_LABEL:
  1675. Begin
  1676. if SearchLabel(upper(actasmpattern),hl,true) then
  1677. ConcatLabel(curlist,hl)
  1678. else
  1679. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1680. Consume(AS_LABEL);
  1681. end;
  1682. AS_DW:
  1683. Begin
  1684. Consume(AS_DW);
  1685. BuildConstant($ffff);
  1686. end;
  1687. AS_DATA:
  1688. Begin
  1689. curlist^.Concat(new(pai_section,init(sec_data)));
  1690. lastsec:=sec_data;
  1691. Consume(AS_DATA);
  1692. end;
  1693. AS_TEXT:
  1694. Begin
  1695. curlist^.Concat(new(pai_section,init(sec_code)));
  1696. lastsec:=sec_code;
  1697. Consume(AS_TEXT);
  1698. end;
  1699. AS_DB:
  1700. Begin
  1701. Consume(AS_DB);
  1702. BuildConstant($ff);
  1703. end;
  1704. AS_DD:
  1705. Begin
  1706. Consume(AS_DD);
  1707. BuildConstant($ffffffff);
  1708. end;
  1709. AS_DQ:
  1710. Begin
  1711. Consume(AS_DQ);
  1712. BuildRealConstant(s64comp);
  1713. end;
  1714. AS_SINGLE:
  1715. Begin
  1716. Consume(AS_SINGLE);
  1717. BuildRealConstant(s32real);
  1718. end;
  1719. AS_DOUBLE:
  1720. Begin
  1721. Consume(AS_DOUBLE);
  1722. BuildRealConstant(s64real);
  1723. end;
  1724. AS_EXTENDED:
  1725. Begin
  1726. Consume(AS_EXTENDED);
  1727. BuildRealConstant(s80real);
  1728. end;
  1729. AS_GLOBAL:
  1730. Begin
  1731. Consume(AS_GLOBAL);
  1732. if actasmtoken=AS_ID then
  1733. ConcatPublic(curlist,actasmpattern);
  1734. Consume(AS_ID);
  1735. if actasmtoken<>AS_SEPARATOR then
  1736. Consume(AS_SEPARATOR);
  1737. end;
  1738. AS_ALIGN:
  1739. Begin
  1740. Consume(AS_ALIGN);
  1741. l1:=BuildConstExpression(false,false);
  1742. if (target_info.target in [target_i386_GO32V1,target_i386_GO32V2]) then
  1743. begin
  1744. l2:=1;
  1745. if (l1>=0) and (l1<=16) then
  1746. while (l1>0) do
  1747. begin
  1748. l2:=2*l2;
  1749. dec(l1);
  1750. end;
  1751. l1:=l2;
  1752. end;
  1753. ConcatAlign(curlist,l1);
  1754. Message(asmr_n_align_is_target_specific);
  1755. if actasmtoken<>AS_SEPARATOR then
  1756. Consume(AS_SEPARATOR);
  1757. end;
  1758. AS_BALIGN:
  1759. Begin
  1760. Consume(AS_BALIGN);
  1761. ConcatAlign(curlist,BuildConstExpression(false,false));
  1762. if actasmtoken<>AS_SEPARATOR then
  1763. Consume(AS_SEPARATOR);
  1764. end;
  1765. AS_P2ALIGN:
  1766. Begin
  1767. Consume(AS_P2ALIGN);
  1768. l1:=BuildConstExpression(false,false);
  1769. l2:=1;
  1770. if (l1>=0) and (l1<=16) then
  1771. while (l1>0) do
  1772. begin
  1773. l2:=2*l2;
  1774. dec(l1);
  1775. end;
  1776. l1:=l2;
  1777. ConcatAlign(curlist,l1);
  1778. if actasmtoken<>AS_SEPARATOR then
  1779. Consume(AS_SEPARATOR);
  1780. end;
  1781. AS_ASCIIZ:
  1782. Begin
  1783. Consume(AS_ASCIIZ);
  1784. BuildStringConstant(TRUE);
  1785. end;
  1786. AS_ASCII:
  1787. Begin
  1788. Consume(AS_ASCII);
  1789. BuildStringConstant(FALSE);
  1790. end;
  1791. AS_LCOMM:
  1792. Begin
  1793. Consume(AS_LCOMM);
  1794. commname:=actasmpattern;
  1795. Consume(AS_ID);
  1796. Consume(AS_COMMA);
  1797. ConcatLocalBss(commname,BuildConstExpression(false,false));
  1798. if actasmtoken<>AS_SEPARATOR then
  1799. Consume(AS_SEPARATOR);
  1800. end;
  1801. AS_COMM:
  1802. Begin
  1803. Consume(AS_COMM);
  1804. commname:=actasmpattern;
  1805. Consume(AS_ID);
  1806. Consume(AS_COMMA);
  1807. ConcatGlobalBss(commname,BuildConstExpression(false,false));
  1808. if actasmtoken<>AS_SEPARATOR then
  1809. Consume(AS_SEPARATOR);
  1810. end;
  1811. AS_OPCODE:
  1812. Begin
  1813. instr.init;
  1814. instr.BuildOpcode;
  1815. instr.AddReferenceSizes;
  1816. instr.SetInstructionOpsize;
  1817. instr.CheckOperandSizes;
  1818. instr.ConcatInstruction(curlist);
  1819. instr.done;
  1820. end;
  1821. AS_SEPARATOR:
  1822. Begin
  1823. Consume(AS_SEPARATOR);
  1824. end;
  1825. AS_END:
  1826. begin
  1827. break; { end assembly block }
  1828. end;
  1829. else
  1830. Begin
  1831. Message(asmr_e_syntax_error);
  1832. RecoverConsume(false);
  1833. end;
  1834. end;
  1835. until false;
  1836. { Check LocalLabelList }
  1837. LocalLabelList^.CheckEmitted;
  1838. dispose(LocalLabelList,Done);
  1839. { are we back in the code section? }
  1840. if lastsec<>sec_code then
  1841. begin
  1842. Message(asmr_w_assembler_code_not_returned_to_text);
  1843. curlist^.Concat(new(pai_section,init(sec_code)));
  1844. end;
  1845. { Return the list in an asmnode }
  1846. assemble:=genasmnode(curlist);
  1847. Message1(asmr_d_finish_reading,'AT&T');
  1848. end;
  1849. {*****************************************************************************
  1850. Initialize
  1851. *****************************************************************************}
  1852. var
  1853. old_exit : pointer;
  1854. procedure ra386att_exit;{$ifndef FPC}far;{$endif}
  1855. begin
  1856. if assigned(iasmops) then
  1857. dispose(iasmops);
  1858. if assigned(iasmregs) then
  1859. dispose(iasmregs);
  1860. exitproc:=old_exit;
  1861. end;
  1862. begin
  1863. old_exit:=exitproc;
  1864. exitproc:=@ra386att_exit;
  1865. end.
  1866. {
  1867. $Log$
  1868. Revision 1.62 1999-11-09 23:06:46 peter
  1869. * esi_offset -> selfpointer_offset to be newcg compatible
  1870. * hcogegen -> cgbase fixes for newcg
  1871. Revision 1.61 1999/11/06 14:34:23 peter
  1872. * truncated log to 20 revs
  1873. Revision 1.60 1999/10/01 07:59:20 peter
  1874. * fixed object field parsing
  1875. Revision 1.59 1999/09/27 23:44:57 peter
  1876. * procinfo is now a pointer
  1877. * support for result setting in sub procedure
  1878. Revision 1.58 1999/09/08 16:04:01 peter
  1879. * better support for object fields and more error checks for
  1880. field accesses which create buggy code
  1881. Revision 1.57 1999/08/05 16:53:08 peter
  1882. * V_Fatal=1, all other V_ are also increased
  1883. * Check for local procedure when assigning procvar
  1884. * fixed comment parsing because directives
  1885. * oldtp mode directives better supported
  1886. * added some messages to errore.msg
  1887. Revision 1.56 1999/08/04 00:23:25 florian
  1888. * renamed i386asm and i386base to cpuasm and cpubase
  1889. Revision 1.55 1999/08/03 22:03:09 peter
  1890. * moved bitmask constants to sets
  1891. * some other type/const renamings
  1892. Revision 1.54 1999/07/24 11:17:12 peter
  1893. * suffix parsing for at&t fixed for things like movsbl
  1894. * string constants are now handle correctly and also allowed in
  1895. constant expressions
  1896. Revision 1.53 1999/06/21 16:45:01 peter
  1897. * merged
  1898. Revision 1.52 1999/06/14 17:48:03 peter
  1899. * merged
  1900. Revision 1.50.2.2 1999/06/21 16:42:57 peter
  1901. * fixed label and opcode on the same line
  1902. Revision 1.50.2.1 1999/06/14 17:30:44 peter
  1903. * align fixes from pierre
  1904. Revision 1.51 1999/06/11 22:54:12 pierre
  1905. * .align problem treated :
  1906. .align is considered as .p2align on go32v1 and go32v2
  1907. and as .balign on other targets
  1908. + ra386att supports also .balign and .p2align
  1909. * ag386att uses .balign allways
  1910. Revision 1.50 1999/06/08 11:51:58 peter
  1911. * fixed some intel bugs with scale parsing
  1912. * end is now also a separator in many more cases
  1913. Revision 1.49 1999/06/03 16:28:03 pierre
  1914. * typo corrected
  1915. Revision 1.48 1999/05/27 19:44:56 peter
  1916. * removed oldasm
  1917. * plabel -> pasmlabel
  1918. * -a switches to source writing automaticly
  1919. * assembler readers OOPed
  1920. * asmsymbol automaticly external
  1921. * jumptables and other label fixes for asm readers
  1922. Revision 1.47 1999/05/21 13:55:13 peter
  1923. * NEWLAB for label as symbol
  1924. Revision 1.46 1999/05/12 00:19:56 peter
  1925. * removed R_DEFAULT_SEG
  1926. * uniform float names
  1927. Revision 1.45 1999/05/06 09:05:25 peter
  1928. * generic write_float and str_float
  1929. * fixed constant float conversions
  1930. Revision 1.44 1999/05/05 22:22:00 peter
  1931. * updated messages
  1932. Revision 1.43 1999/05/04 21:45:01 florian
  1933. * changes to compile it with Delphi 4.0
  1934. }