ra386att.pas 56 KB

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