ra386att.pas 54 KB

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