ra386int.pas 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Carl Eric Codere and Peter Vreman
  4. Does the parsing process for the intel 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. Unit Ra386int;
  19. {$i defines.inc}
  20. Interface
  21. uses
  22. node;
  23. function assemble: tnode;
  24. Implementation
  25. uses
  26. { common }
  27. cutils,cobjects,
  28. { global }
  29. globtype,globals,verbose,
  30. systems,
  31. { aasm }
  32. cpubase,aasm,
  33. { symtable }
  34. symconst,symtype,symsym,symtable,types,
  35. { pass 1 }
  36. nbas,
  37. { parser }
  38. scanner,
  39. ra386,rautils,
  40. { codegen }
  41. {$ifdef newcg}
  42. cgbase
  43. {$else}
  44. hcodegen
  45. {$endif}
  46. ;
  47. type
  48. tasmtoken = (
  49. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  50. AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  51. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  52. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,
  53. {------------------ Assembler directives --------------------}
  54. AS_DB,AS_DW,AS_DD,AS_END,
  55. {------------------ Assembler Operators --------------------}
  56. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_NEAR,AS_FAR,
  57. AS_HIGH,AS_LOW,AS_OFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  58. AS_AND,AS_OR,AS_XOR);
  59. tasmkeyword = string[6];
  60. const
  61. { These tokens should be modified accordingly to the modifications }
  62. { in the different enumerations. }
  63. firstdirective = AS_DB;
  64. lastdirective = AS_END;
  65. firstoperator = AS_BYTE;
  66. lastoperator = AS_XOR;
  67. firstsreg = R_CS;
  68. lastsreg = R_SS;
  69. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  70. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  71. _count_asmprefixes = 5;
  72. _count_asmspecialops = 25;
  73. _count_asmoverrides = 3;
  74. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  75. ('DB','DW','DD','END');
  76. { problems with shl,shr,not,and,or and xor, they are }
  77. { context sensitive. }
  78. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  79. 'BYTE','WORD','DWORD','QWORD','TBYTE','NEAR','FAR','HIGH',
  80. 'LOW','OFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  81. 'OR','XOR');
  82. token2str : array[tasmtoken] of string[10] = (
  83. '','Label','LLabel','String','Integer',
  84. ',','[',']','(',
  85. ')',':','.','+','-','*',
  86. ';','identifier','register','opcode','/',
  87. '','','','END',
  88. '','','','','','','','',
  89. '','','','type','ptr','mod','shl','shr','not',
  90. 'and','or','xor'
  91. );
  92. const
  93. newline = #10;
  94. firsttoken : boolean = TRUE;
  95. var
  96. _asmsorted : boolean;
  97. inexpression : boolean;
  98. curlist : TAAsmoutput;
  99. c : char;
  100. prevasmtoken : tasmtoken;
  101. actasmtoken : tasmtoken;
  102. actasmpattern : string;
  103. actasmregister : tregister;
  104. actopcode : tasmop;
  105. actopsize : topsize;
  106. actcondition : tasmcond;
  107. iasmops : Pdictionary;
  108. iasmregs : ^reg2strtable;
  109. Procedure SetupTables;
  110. { creates uppercased symbol tables for speed access }
  111. var
  112. i : tasmop;
  113. j : tregister;
  114. str2opentry: pstr2opentry;
  115. Begin
  116. { opcodes }
  117. new(iasmops,init);
  118. for i:=firstop to lastop do
  119. begin
  120. new(str2opentry,initname(upper(int_op2str[i])));
  121. str2opentry^.op:=i;
  122. iasmops^.insert(str2opentry);
  123. end;
  124. { registers }
  125. new(iasmregs);
  126. for j:=firstreg to lastreg do
  127. iasmregs^[j] := upper(int_reg2str[j]);
  128. end;
  129. {---------------------------------------------------------------------}
  130. { Routines for the tokenizing }
  131. {---------------------------------------------------------------------}
  132. function is_asmopcode(const s: string):boolean;
  133. var
  134. str2opentry: pstr2opentry;
  135. cond : string[4];
  136. cnd : tasmcond;
  137. j: longint;
  138. Begin
  139. is_asmopcode:=FALSE;
  140. actopcode:=A_None;
  141. actcondition:=C_None;
  142. actopsize:=S_NO;
  143. str2opentry:=pstr2opentry(iasmops^.search(s));
  144. if assigned(str2opentry) then
  145. begin
  146. actopcode:=str2opentry^.op;
  147. actasmtoken:=AS_OPCODE;
  148. is_asmopcode:=TRUE;
  149. exit;
  150. end;
  151. { not found yet, check condition opcodes }
  152. j:=0;
  153. while (j<CondAsmOps) do
  154. begin
  155. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  156. begin
  157. cond:=Copy(s,Length(CondAsmOpStr[j])+1,255);
  158. if cond<>'' then
  159. begin
  160. for cnd:=low(TasmCond) to high(TasmCond) do
  161. if Cond=Upper(cond2str[cnd]) then
  162. begin
  163. actopcode:=CondASmOp[j];
  164. actcondition:=cnd;
  165. is_asmopcode:=TRUE;
  166. actasmtoken:=AS_OPCODE;
  167. exit
  168. end;
  169. end;
  170. end;
  171. inc(j);
  172. end;
  173. end;
  174. function is_asmoperator(const s: string):boolean;
  175. var
  176. i : longint;
  177. Begin
  178. for i:=0 to _count_asmoperators do
  179. if s=_asmoperators[i] then
  180. begin
  181. actasmtoken:=tasmtoken(longint(firstoperator)+i);
  182. is_asmoperator:=true;
  183. exit;
  184. end;
  185. is_asmoperator:=false;
  186. end;
  187. Function is_asmdirective(const s: string):boolean;
  188. var
  189. i : longint;
  190. Begin
  191. for i:=0 to _count_asmdirectives do
  192. if s=_asmdirectives[i] then
  193. begin
  194. actasmtoken:=tasmtoken(longint(firstdirective)+i);
  195. is_asmdirective:=true;
  196. exit;
  197. end;
  198. is_asmdirective:=false;
  199. end;
  200. Function is_register(const s: string):boolean;
  201. Var
  202. i : tregister;
  203. Begin
  204. actasmregister:=R_NO;
  205. for i:=firstreg to lastreg do
  206. if s=iasmregs^[i] then
  207. begin
  208. actasmtoken:=AS_REGISTER;
  209. actasmregister:=i;
  210. is_register:=true;
  211. exit;
  212. end;
  213. is_register:=false;
  214. end;
  215. function is_locallabel(const s:string):boolean;
  216. begin
  217. is_locallabel:=(length(s)>1) and (s[1]='@');
  218. end;
  219. Procedure GetToken;
  220. var
  221. len : longint;
  222. forcelabel : boolean;
  223. begin
  224. { save old token and reset new token }
  225. prevasmtoken:=actasmtoken;
  226. actasmtoken:=AS_NONE;
  227. { reset }
  228. forcelabel:=FALSE;
  229. actasmpattern:='';
  230. { while space and tab , continue scan... }
  231. while (c in [' ',#9]) do
  232. c:=current_scanner^.asmgetchar;
  233. { get token pos }
  234. if not (c in [newline,#13,'{',';']) then
  235. current_scanner^.gettokenpos;
  236. { Local Label, Label, Directive, Prefix or Opcode }
  237. if firsttoken and not (c in [newline,#13,'{',';']) then
  238. begin
  239. firsttoken:=FALSE;
  240. len:=0;
  241. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  242. begin
  243. { if there is an at_sign, then this must absolutely be a label }
  244. if c = '@' then
  245. forcelabel:=TRUE;
  246. inc(len);
  247. actasmpattern[len]:=c;
  248. c:=current_scanner^.asmgetchar;
  249. end;
  250. actasmpattern[0]:=chr(len);
  251. uppervar(actasmpattern);
  252. { label ? }
  253. if c = ':' then
  254. begin
  255. if actasmpattern[1]='@' then
  256. actasmtoken:=AS_LLABEL
  257. else
  258. actasmtoken:=AS_LABEL;
  259. { let us point to the next character }
  260. c:=current_scanner^.asmgetchar;
  261. firsttoken:=true;
  262. exit;
  263. end;
  264. { Are we trying to create an identifier with }
  265. { an at-sign...? }
  266. if forcelabel then
  267. Message(asmr_e_none_label_contain_at);
  268. { opcode ? }
  269. If is_asmopcode(actasmpattern) then
  270. Begin
  271. { check if we are in an expression }
  272. { then continue with asm directives }
  273. if not inexpression then
  274. exit;
  275. end;
  276. if is_asmdirective(actasmpattern) then
  277. exit;
  278. message1(asmr_e_unknown_opcode,actasmpattern);
  279. actasmtoken:=AS_NONE;
  280. exit;
  281. end
  282. else { else firsttoken }
  283. begin
  284. case c of
  285. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  286. { - @Result, @Code or @Data special variables. }
  287. begin
  288. actasmpattern:=c;
  289. c:=current_scanner^.asmgetchar;
  290. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  291. begin
  292. actasmpattern:=actasmpattern + c;
  293. c:=current_scanner^.asmgetchar;
  294. end;
  295. uppervar(actasmpattern);
  296. actasmtoken:=AS_ID;
  297. exit;
  298. end;
  299. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  300. begin
  301. actasmpattern:=c;
  302. c:=current_scanner^.asmgetchar;
  303. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  304. begin
  305. actasmpattern:=actasmpattern + c;
  306. c:=current_scanner^.asmgetchar;
  307. end;
  308. uppervar(actasmpattern);
  309. { after prefix we allow also a new opcode }
  310. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  311. Begin
  312. { if we are not in a constant }
  313. { expression than this is an }
  314. { opcode. }
  315. if not inexpression then
  316. exit;
  317. end;
  318. { support st(X) for fpu registers }
  319. if (actasmpattern = 'ST') and (c='(') then
  320. Begin
  321. actasmpattern:=actasmpattern+c;
  322. c:=current_scanner^.asmgetchar;
  323. if c in ['0'..'7'] then
  324. actasmpattern:=actasmpattern + c
  325. else
  326. Message(asmr_e_invalid_fpu_register);
  327. c:=current_scanner^.asmgetchar;
  328. if c <> ')' then
  329. Message(asmr_e_invalid_fpu_register)
  330. else
  331. Begin
  332. actasmpattern:=actasmpattern + c;
  333. c:=current_scanner^.asmgetchar;
  334. end;
  335. end;
  336. if is_register(actasmpattern) then
  337. exit;
  338. if is_asmdirective(actasmpattern) then
  339. exit;
  340. if is_asmoperator(actasmpattern) then
  341. exit;
  342. actasmtoken:=AS_ID;
  343. exit;
  344. end;
  345. '&' : { override operator... not supported }
  346. begin
  347. Message(asmr_w_override_op_not_supported);
  348. c:=current_scanner^.asmgetchar;
  349. actasmtoken:=AS_NONE;
  350. end;
  351. '''' : { string or character }
  352. begin
  353. actasmpattern:='';
  354. current_scanner^.in_asm_string:=true;
  355. repeat
  356. if c = '''' then
  357. begin
  358. c:=current_scanner^.asmgetchar;
  359. if c=newline then
  360. begin
  361. Message(scan_f_string_exceeds_line);
  362. break;
  363. end;
  364. repeat
  365. if c='''' then
  366. begin
  367. c:=current_scanner^.asmgetchar;
  368. if c='''' then
  369. begin
  370. actasmpattern:=actasmpattern+'''';
  371. c:=current_scanner^.asmgetchar;
  372. if c=newline then
  373. begin
  374. Message(scan_f_string_exceeds_line);
  375. break;
  376. end;
  377. end
  378. else
  379. break;
  380. end
  381. else
  382. begin
  383. actasmpattern:=actasmpattern+c;
  384. c:=current_scanner^.asmgetchar;
  385. if c=newline then
  386. begin
  387. Message(scan_f_string_exceeds_line);
  388. break
  389. end;
  390. end;
  391. until false; { end repeat }
  392. end
  393. else
  394. break; { end if }
  395. until false;
  396. current_scanner^.in_asm_string:=false;
  397. actasmtoken:=AS_STRING;
  398. exit;
  399. end;
  400. '"' : { string or character }
  401. begin
  402. current_scanner^.in_asm_string:=true;
  403. actasmpattern:='';
  404. repeat
  405. if c = '"' then
  406. begin
  407. c:=current_scanner^.asmgetchar;
  408. if c=newline then
  409. begin
  410. Message(scan_f_string_exceeds_line);
  411. break;
  412. end;
  413. repeat
  414. if c='"' then
  415. begin
  416. c:=current_scanner^.asmgetchar;
  417. if c='"' then
  418. begin
  419. actasmpattern:=actasmpattern+'"';
  420. c:=current_scanner^.asmgetchar;
  421. if c=newline then
  422. begin
  423. Message(scan_f_string_exceeds_line);
  424. break;
  425. end;
  426. end
  427. else
  428. break;
  429. end
  430. else
  431. begin
  432. actasmpattern:=actasmpattern+c;
  433. c:=current_scanner^.asmgetchar;
  434. if c=newline then
  435. begin
  436. Message(scan_f_string_exceeds_line);
  437. break
  438. end;
  439. end;
  440. until false; { end repeat }
  441. end
  442. else
  443. break; { end if }
  444. until false;
  445. current_scanner^.in_asm_string:=false;
  446. actasmtoken:=AS_STRING;
  447. exit;
  448. end;
  449. '$' :
  450. begin
  451. c:=current_scanner^.asmgetchar;
  452. while c in ['0'..'9','A'..'F','a'..'f'] do
  453. begin
  454. actasmpattern:=actasmpattern + c;
  455. c:=current_scanner^.asmgetchar;
  456. end;
  457. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  458. actasmtoken:=AS_INTNUM;
  459. exit;
  460. end;
  461. ',' :
  462. begin
  463. actasmtoken:=AS_COMMA;
  464. c:=current_scanner^.asmgetchar;
  465. exit;
  466. end;
  467. '[' :
  468. begin
  469. actasmtoken:=AS_LBRACKET;
  470. c:=current_scanner^.asmgetchar;
  471. exit;
  472. end;
  473. ']' :
  474. begin
  475. actasmtoken:=AS_RBRACKET;
  476. c:=current_scanner^.asmgetchar;
  477. exit;
  478. end;
  479. '(' :
  480. begin
  481. actasmtoken:=AS_LPAREN;
  482. c:=current_scanner^.asmgetchar;
  483. exit;
  484. end;
  485. ')' :
  486. begin
  487. actasmtoken:=AS_RPAREN;
  488. c:=current_scanner^.asmgetchar;
  489. exit;
  490. end;
  491. ':' :
  492. begin
  493. actasmtoken:=AS_COLON;
  494. c:=current_scanner^.asmgetchar;
  495. exit;
  496. end;
  497. '.' :
  498. begin
  499. actasmtoken:=AS_DOT;
  500. c:=current_scanner^.asmgetchar;
  501. exit;
  502. end;
  503. '+' :
  504. begin
  505. actasmtoken:=AS_PLUS;
  506. c:=current_scanner^.asmgetchar;
  507. exit;
  508. end;
  509. '-' :
  510. begin
  511. actasmtoken:=AS_MINUS;
  512. c:=current_scanner^.asmgetchar;
  513. exit;
  514. end;
  515. '*' :
  516. begin
  517. actasmtoken:=AS_STAR;
  518. c:=current_scanner^.asmgetchar;
  519. exit;
  520. end;
  521. '/' :
  522. begin
  523. actasmtoken:=AS_SLASH;
  524. c:=current_scanner^.asmgetchar;
  525. exit;
  526. end;
  527. '0'..'9':
  528. begin
  529. actasmpattern:=c;
  530. c:=current_scanner^.asmgetchar;
  531. { Get the possible characters }
  532. while c in ['0'..'9','A'..'F','a'..'f'] do
  533. begin
  534. actasmpattern:=actasmpattern + c;
  535. c:=current_scanner^.asmgetchar;
  536. end;
  537. { Get ending character }
  538. uppervar(actasmpattern);
  539. c:=upcase(c);
  540. { possibly a binary number. }
  541. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  542. Begin
  543. { Delete the last binary specifier }
  544. delete(actasmpattern,length(actasmpattern),1);
  545. actasmpattern:=tostr(ValBinary(actasmpattern));
  546. actasmtoken:=AS_INTNUM;
  547. exit;
  548. end
  549. else
  550. Begin
  551. case c of
  552. 'O' :
  553. Begin
  554. actasmpattern:=tostr(ValOctal(actasmpattern));
  555. actasmtoken:=AS_INTNUM;
  556. c:=current_scanner^.asmgetchar;
  557. exit;
  558. end;
  559. 'H' :
  560. Begin
  561. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  562. actasmtoken:=AS_INTNUM;
  563. c:=current_scanner^.asmgetchar;
  564. exit;
  565. end;
  566. else { must be an integer number }
  567. begin
  568. actasmpattern:=tostr(ValDecimal(actasmpattern));
  569. actasmtoken:=AS_INTNUM;
  570. exit;
  571. end;
  572. end;
  573. end;
  574. end;
  575. ';','{',#13,newline :
  576. begin
  577. c:=current_scanner^.asmgetchar;
  578. firsttoken:=TRUE;
  579. actasmtoken:=AS_SEPARATOR;
  580. exit;
  581. end;
  582. else
  583. current_scanner^.illegal_char(c);
  584. end;
  585. end;
  586. end;
  587. function consume(t : tasmtoken):boolean;
  588. begin
  589. Consume:=true;
  590. if t<>actasmtoken then
  591. begin
  592. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  593. Consume:=false;
  594. end;
  595. repeat
  596. gettoken;
  597. until actasmtoken<>AS_NONE;
  598. end;
  599. procedure RecoverConsume(allowcomma:boolean);
  600. begin
  601. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  602. begin
  603. if allowcomma and (actasmtoken=AS_COMMA) then
  604. break;
  605. Consume(actasmtoken);
  606. end;
  607. end;
  608. {*****************************************************************************
  609. Parsing Helpers
  610. *****************************************************************************}
  611. Procedure BuildRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  612. { Description: This routine builds up a record offset after a AS_DOT }
  613. { token is encountered. }
  614. { On entry actasmtoken should be equal to AS_DOT }
  615. var
  616. s : string;
  617. Begin
  618. offset:=0;
  619. size:=0;
  620. s:=expr;
  621. while (actasmtoken=AS_DOT) do
  622. begin
  623. Consume(AS_DOT);
  624. if actasmtoken=AS_ID then
  625. s:=s+'.'+actasmpattern;
  626. if not Consume(AS_ID) then
  627. begin
  628. RecoverConsume(true);
  629. break;
  630. end;
  631. end;
  632. if not GetRecordOffsetSize(s,offset,size) then
  633. Message(asmr_e_building_record_offset);
  634. end;
  635. Procedure BuildConstSymbolExpression(needofs,exitreg:boolean;var value:longint;var asmsym:string);
  636. var
  637. tempstr,expr,hs : string;
  638. parenlevel,l,k : longint;
  639. errorflag : boolean;
  640. prevtok : tasmtoken;
  641. hl : PAsmLabel;
  642. sym : psym;
  643. Begin
  644. { reset }
  645. value:=0;
  646. asmsym:='';
  647. errorflag:=FALSE;
  648. tempstr:='';
  649. expr:='';
  650. inexpression:=TRUE;
  651. parenlevel:=0;
  652. Repeat
  653. Case actasmtoken of
  654. AS_LPAREN:
  655. Begin
  656. Consume(AS_LPAREN);
  657. expr:=expr + '(';
  658. inc(parenlevel);
  659. end;
  660. AS_RPAREN:
  661. Begin
  662. Consume(AS_RPAREN);
  663. expr:=expr + ')';
  664. dec(parenlevel);
  665. end;
  666. AS_SHL:
  667. Begin
  668. Consume(AS_SHL);
  669. expr:=expr + '<';
  670. end;
  671. AS_SHR:
  672. Begin
  673. Consume(AS_SHR);
  674. expr:=expr + '>';
  675. end;
  676. AS_SLASH:
  677. Begin
  678. Consume(AS_SLASH);
  679. expr:=expr + '/';
  680. end;
  681. AS_MOD:
  682. Begin
  683. Consume(AS_MOD);
  684. expr:=expr + '%';
  685. end;
  686. AS_STAR:
  687. Begin
  688. Consume(AS_STAR);
  689. if exitreg and (actasmtoken=AS_REGISTER) then
  690. break;
  691. expr:=expr + '*';
  692. end;
  693. AS_PLUS:
  694. Begin
  695. Consume(AS_PLUS);
  696. if exitreg and (actasmtoken=AS_REGISTER) then
  697. break;
  698. expr:=expr + '+';
  699. end;
  700. AS_MINUS:
  701. Begin
  702. Consume(AS_MINUS);
  703. expr:=expr + '-';
  704. end;
  705. AS_AND:
  706. Begin
  707. Consume(AS_AND);
  708. expr:=expr + '&';
  709. end;
  710. AS_NOT:
  711. Begin
  712. Consume(AS_NOT);
  713. expr:=expr + '~';
  714. end;
  715. AS_XOR:
  716. Begin
  717. Consume(AS_XOR);
  718. expr:=expr + '^';
  719. end;
  720. AS_OR:
  721. Begin
  722. Consume(AS_OR);
  723. expr:=expr + '|';
  724. end;
  725. AS_INTNUM:
  726. Begin
  727. expr:=expr + actasmpattern;
  728. Consume(AS_INTNUM);
  729. end;
  730. AS_OFFSET:
  731. begin
  732. Consume(AS_OFFSET);
  733. if actasmtoken<>AS_ID then
  734. Message(asmr_e_offset_without_identifier);
  735. end;
  736. AS_TYPE:
  737. begin
  738. l:=0;
  739. Consume(AS_TYPE);
  740. if actasmtoken<>AS_ID then
  741. Message(asmr_e_type_without_identifier)
  742. else
  743. begin
  744. tempstr:=actasmpattern;
  745. Consume(AS_ID);
  746. if actasmtoken=AS_DOT then
  747. BuildRecordOffsetSize(tempstr,k,l)
  748. else
  749. begin
  750. getsym(tempstr,false);
  751. if assigned(srsym) then
  752. begin
  753. case srsym^.typ of
  754. varsym :
  755. l:=pvarsym(srsym)^.getsize;
  756. typedconstsym :
  757. l:=ptypedconstsym(srsym)^.getsize;
  758. typesym :
  759. l:=ptypesym(srsym)^.restype.def^.size;
  760. else
  761. Message(asmr_e_wrong_sym_type);
  762. end;
  763. end
  764. else
  765. Message1(sym_e_unknown_id,tempstr);
  766. end;
  767. end;
  768. str(l, tempstr);
  769. expr:=expr + tempstr;
  770. end;
  771. AS_STRING:
  772. Begin
  773. l:=0;
  774. case Length(actasmpattern) of
  775. 1 :
  776. l:=ord(actasmpattern[1]);
  777. 2 :
  778. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  779. 3 :
  780. l:=ord(actasmpattern[3]) +
  781. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  782. 4 :
  783. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  784. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  785. else
  786. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  787. end;
  788. str(l, tempstr);
  789. expr:=expr + tempstr;
  790. Consume(AS_STRING);
  791. end;
  792. AS_ID:
  793. Begin
  794. hs:='';
  795. tempstr:=actasmpattern;
  796. prevtok:=prevasmtoken;
  797. consume(AS_ID);
  798. if SearchIConstant(tempstr,l) then
  799. begin
  800. str(l, tempstr);
  801. expr:=expr + tempstr;
  802. end
  803. else
  804. begin
  805. if is_locallabel(tempstr) then
  806. begin
  807. CreateLocalLabel(tempstr,hl,false);
  808. hs:=hl^.name
  809. end
  810. else
  811. if SearchLabel(tempstr,hl,false) then
  812. hs:=hl^.name
  813. else
  814. begin
  815. getsym(tempstr,false);
  816. sym:=srsym;
  817. if assigned(sym) then
  818. begin
  819. case srsym^.typ of
  820. varsym :
  821. begin
  822. if sym^.owner^.symtabletype in [localsymtable,parasymtable] then
  823. Message(asmr_e_no_local_or_para_allowed);
  824. hs:=pvarsym(srsym)^.mangledname;
  825. end;
  826. typedconstsym :
  827. hs:=ptypedconstsym(srsym)^.mangledname;
  828. procsym :
  829. hs:=pprocsym(srsym)^.mangledname;
  830. typesym :
  831. begin
  832. if not(ptypesym(srsym)^.restype.def^.deftype in [recorddef,objectdef]) then
  833. Message(asmr_e_wrong_sym_type);
  834. end;
  835. else
  836. Message(asmr_e_wrong_sym_type);
  837. end;
  838. end
  839. else
  840. Message1(sym_e_unknown_id,tempstr);
  841. end;
  842. { symbol found? }
  843. if hs<>'' then
  844. begin
  845. if needofs and (prevtok<>AS_OFFSET) then
  846. Message(asmr_e_need_offset);
  847. if asmsym='' then
  848. asmsym:=hs
  849. else
  850. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  851. if (expr='') or (expr[length(expr)]='+') then
  852. begin
  853. { don't remove the + if there could be a record field }
  854. if actasmtoken<>AS_DOT then
  855. delete(expr,length(expr),1);
  856. end
  857. else
  858. Message(asmr_e_only_add_relocatable_symbol);
  859. end;
  860. if actasmtoken=AS_DOT then
  861. begin
  862. BuildRecordOffsetSize(tempstr,l,k);
  863. str(l, tempstr);
  864. expr:=expr + tempstr;
  865. end
  866. else
  867. begin
  868. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  869. delete(expr,length(expr),1);
  870. end;
  871. end;
  872. { check if there are wrong operator used like / or mod etc. }
  873. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  874. Message(asmr_e_only_add_relocatable_symbol);
  875. end;
  876. AS_END,
  877. AS_RBRACKET,
  878. AS_SEPARATOR,
  879. AS_COMMA:
  880. Begin
  881. break;
  882. end;
  883. else
  884. Begin
  885. { write error only once. }
  886. if not errorflag then
  887. Message(asmr_e_invalid_constant_expression);
  888. { consume tokens until we find COMMA or SEPARATOR }
  889. Consume(actasmtoken);
  890. errorflag:=TRUE;
  891. end;
  892. end;
  893. Until false;
  894. { calculate expression }
  895. if not ErrorFlag then
  896. value:=CalculateExpression(expr)
  897. else
  898. value:=0;
  899. { no longer in an expression }
  900. inexpression:=FALSE;
  901. end;
  902. Function BuildConstExpression:longint;
  903. var
  904. l : longint;
  905. hs : string;
  906. begin
  907. BuildConstSymbolExpression(false,false,l,hs);
  908. if hs<>'' then
  909. Message(asmr_e_relocatable_symbol_not_allowed);
  910. BuildConstExpression:=l;
  911. end;
  912. Function BuildRefConstExpression:longint;
  913. var
  914. l : longint;
  915. hs : string;
  916. begin
  917. BuildConstSymbolExpression(false,true,l,hs);
  918. if hs<>'' then
  919. Message(asmr_e_relocatable_symbol_not_allowed);
  920. BuildRefConstExpression:=l;
  921. end;
  922. {****************************************************************************
  923. T386IntelOperand
  924. ****************************************************************************}
  925. type
  926. P386IntelOperand=^T386IntelOperand;
  927. T386IntelOperand=object(T386Operand)
  928. Procedure BuildOperand;virtual;
  929. private
  930. Procedure BuildReference;
  931. Procedure BuildConstant;
  932. end;
  933. Procedure T386IntelOperand.BuildReference;
  934. var
  935. k,l : longint;
  936. tempstr2,
  937. tempstr,hs : string;
  938. code : integer;
  939. hreg,
  940. oldbase : tregister;
  941. GotStar,GotOffset,HadVar,
  942. GotPlus,Negative : boolean;
  943. Begin
  944. Consume(AS_LBRACKET);
  945. InitRef;
  946. GotStar:=false;
  947. GotPlus:=true;
  948. GotOffset:=false;
  949. Negative:=false;
  950. repeat
  951. if GotOffset and (actasmtoken<>AS_ID) then
  952. Message(asmr_e_invalid_reference_syntax);
  953. Case actasmtoken of
  954. AS_ID: { Constant reference expression OR variable reference expression }
  955. Begin
  956. if not GotPlus then
  957. Message(asmr_e_invalid_reference_syntax);
  958. if actasmpattern[1] = '@' then
  959. Message(asmr_e_local_label_not_allowed_as_ref);
  960. GotStar:=false;
  961. GotPlus:=false;
  962. if SearchIConstant(actasmpattern,l) or
  963. SearchRecordType(actasmpattern) then
  964. begin
  965. l:=BuildRefConstExpression;
  966. GotPlus:=(prevasmtoken=AS_PLUS);
  967. GotStar:=(prevasmtoken=AS_STAR);
  968. if GotStar then
  969. opr.ref.scalefactor:=l
  970. else
  971. begin
  972. if negative then
  973. Dec(opr.ref.offset,l)
  974. else
  975. Inc(opr.ref.offset,l);
  976. end;
  977. end
  978. else
  979. Begin
  980. if hasvar and not GotOffset then
  981. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  982. HadVar:=hasvar and GotOffset;
  983. if negative then
  984. Message(asmr_e_only_add_relocatable_symbol);
  985. oldbase:=opr.ref.base;
  986. opr.ref.base:=R_NO;
  987. tempstr:=actasmpattern;
  988. Consume(AS_ID);
  989. { typecasting? }
  990. if (actasmtoken=AS_LPAREN) and
  991. SearchType(tempstr) then
  992. begin
  993. hastype:=true;
  994. Consume(AS_LPAREN);
  995. tempstr2:=actasmpattern;
  996. Consume(AS_ID);
  997. Consume(AS_RPAREN);
  998. if not SetupVar(tempstr2,GotOffset) then
  999. Message1(sym_e_unknown_id,tempstr2);
  1000. end
  1001. else
  1002. if not SetupVar(tempstr,GotOffset) then
  1003. Message1(sym_e_unknown_id,tempstr);
  1004. { record.field ? }
  1005. if actasmtoken=AS_DOT then
  1006. begin
  1007. BuildRecordOffsetSize(tempstr,l,k);
  1008. inc(opr.ref.offset,l);
  1009. end;
  1010. if GotOffset then
  1011. begin
  1012. if hasvar and (opr.ref.base=procinfo^.framepointer) then
  1013. begin
  1014. opr.ref.base:=R_NO;
  1015. hasvar:=hadvar;
  1016. end
  1017. else
  1018. begin
  1019. if hasvar and hadvar then
  1020. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1021. { should we allow ?? }
  1022. end;
  1023. end;
  1024. { is the base register loaded by the var ? }
  1025. if (opr.ref.base<>R_NO) then
  1026. begin
  1027. { check if we can move the old base to the index register }
  1028. if (opr.ref.index<>R_NO) then
  1029. Message(asmr_e_wrong_base_index)
  1030. else if assigned(procinfo^._class) and
  1031. (oldbase=self_pointer) and
  1032. (opr.ref.base=self_pointer) then
  1033. begin
  1034. Message(asmr_w_possible_object_field_bug);
  1035. { warn but accept... who knows what people
  1036. caninvent in assembler ! }
  1037. opr.ref.index:=oldbase;
  1038. end
  1039. else
  1040. opr.ref.index:=oldbase;
  1041. end
  1042. else
  1043. opr.ref.base:=oldbase;
  1044. { we can't have a Constant here so add the constant value to the
  1045. offset }
  1046. if opr.typ=OPR_CONSTANT then
  1047. begin
  1048. opr.typ:=OPR_REFERENCE;
  1049. inc(opr.ref.offset,opr.val);
  1050. end;
  1051. end;
  1052. GotOffset:=false;
  1053. end;
  1054. AS_PLUS :
  1055. Begin
  1056. Consume(AS_PLUS);
  1057. Negative:=false;
  1058. GotPlus:=true;
  1059. GotStar:=false;
  1060. end;
  1061. AS_MINUS :
  1062. begin
  1063. Consume(AS_MINUS);
  1064. Negative:=true;
  1065. GotPlus:=true;
  1066. GotStar:=false;
  1067. end;
  1068. AS_STAR : { Scaling, with eax*4 order }
  1069. begin
  1070. Consume(AS_STAR);
  1071. hs:='';
  1072. l:=0;
  1073. case actasmtoken of
  1074. AS_LPAREN :
  1075. l:=BuildConstExpression;
  1076. AS_INTNUM:
  1077. Begin
  1078. hs:=actasmpattern;
  1079. Consume(AS_INTNUM);
  1080. end;
  1081. AS_REGISTER :
  1082. begin
  1083. if opr.ref.scalefactor=0 then
  1084. Message(asmr_e_wrong_scale_factor);
  1085. end;
  1086. else
  1087. Message(asmr_e_invalid_reference_syntax);
  1088. end;
  1089. if actasmtoken<>AS_REGISTER then
  1090. begin
  1091. if hs<>'' then
  1092. val(hs,l,code);
  1093. opr.ref.scalefactor:=l;
  1094. if l>8 then
  1095. Message(asmr_e_wrong_scale_factor);
  1096. end;
  1097. GotPlus:=false;
  1098. GotStar:=false;
  1099. end;
  1100. AS_REGISTER :
  1101. begin
  1102. if not((GotPlus and (not Negative)) or
  1103. GotStar) then
  1104. Message(asmr_e_invalid_reference_syntax);
  1105. hreg:=actasmregister;
  1106. Consume(AS_REGISTER);
  1107. { this register will be the index:
  1108. 1. just read a *
  1109. 2. next token is a *
  1110. 3. base register is already used }
  1111. if (GotStar) or
  1112. (actasmtoken=AS_STAR) or
  1113. (opr.ref.base<>R_NO) then
  1114. begin
  1115. if (opr.ref.index<>R_NO) then
  1116. Message(asmr_e_multiple_index);
  1117. opr.ref.index:=hreg;
  1118. end
  1119. else
  1120. opr.ref.base:=hreg;
  1121. GotPlus:=false;
  1122. GotStar:=false;
  1123. end;
  1124. AS_OFFSET :
  1125. begin
  1126. Consume(AS_OFFSET);
  1127. GotOffset:=true;
  1128. end;
  1129. AS_TYPE,
  1130. AS_NOT,
  1131. AS_INTNUM,
  1132. AS_LPAREN : { Constant reference expression }
  1133. begin
  1134. if not GotPlus then
  1135. Message(asmr_e_invalid_reference_syntax);
  1136. BuildConstSymbolExpression(true,true,l,tempstr);
  1137. if tempstr<>'' then
  1138. begin
  1139. if GotStar then
  1140. Message(asmr_e_only_add_relocatable_symbol);
  1141. if not assigned(opr.ref.symbol) then
  1142. opr.ref.symbol:=newasmsymbol(tempstr)
  1143. else
  1144. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1145. end;
  1146. if GotStar then
  1147. opr.ref.scalefactor:=l
  1148. else
  1149. begin
  1150. if negative then
  1151. Dec(opr.ref.offset,l)
  1152. else
  1153. Inc(opr.ref.offset,l);
  1154. end;
  1155. GotPlus:=(prevasmtoken=AS_PLUS) or
  1156. (prevasmtoken=AS_MINUS);
  1157. if GotPlus then
  1158. negative := prevasmtoken = AS_MINUS;
  1159. GotStar:=(prevasmtoken=AS_STAR);
  1160. end;
  1161. AS_RBRACKET :
  1162. begin
  1163. if GotPlus then
  1164. Message(asmr_e_invalid_reference_syntax);
  1165. Consume(AS_RBRACKET);
  1166. break;
  1167. end;
  1168. else
  1169. Begin
  1170. Message(asmr_e_invalid_reference_syntax);
  1171. RecoverConsume(true);
  1172. break;
  1173. end;
  1174. end;
  1175. until false;
  1176. end;
  1177. Procedure T386IntelOperand.BuildConstant;
  1178. var
  1179. l : longint;
  1180. tempstr : string;
  1181. begin
  1182. BuildConstSymbolExpression(true,false,l,tempstr);
  1183. if tempstr<>'' then
  1184. begin
  1185. opr.typ:=OPR_SYMBOL;
  1186. opr.symofs:=l;
  1187. opr.symbol:=newasmsymbol(tempstr);
  1188. end
  1189. else
  1190. begin
  1191. opr.typ:=OPR_CONSTANT;
  1192. opr.val:=l;
  1193. end;
  1194. end;
  1195. Procedure T386IntelOperand.BuildOperand;
  1196. var
  1197. tempstr,
  1198. expr : string;
  1199. tempreg : tregister;
  1200. l : longint;
  1201. hl : PAsmLabel;
  1202. procedure AddLabelOperand(hl:pasmlabel);
  1203. begin
  1204. if is_calljmp(actopcode) then
  1205. begin
  1206. opr.typ:=OPR_SYMBOL;
  1207. opr.symbol:=hl;
  1208. end
  1209. else
  1210. begin
  1211. InitRef;
  1212. opr.ref.symbol:=hl;
  1213. end;
  1214. end;
  1215. procedure MaybeRecordOffset;
  1216. var
  1217. l,
  1218. toffset,
  1219. tsize : longint;
  1220. begin
  1221. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  1222. exit;
  1223. l:=0;
  1224. if actasmtoken=AS_DOT then
  1225. begin
  1226. { if no type was specified before the [] then we expect the
  1227. first ID to be the type }
  1228. if expr='' then
  1229. begin
  1230. consume(AS_DOT);
  1231. if actasmtoken=AS_ID then
  1232. begin
  1233. expr:=actasmpattern;
  1234. consume(AS_ID);
  1235. { now the next one must the be the dot }
  1236. if actasmtoken<>AS_DOT then
  1237. begin
  1238. Message(asmr_e_building_record_offset);
  1239. expr:='';
  1240. end;
  1241. end
  1242. else
  1243. Message(asmr_e_no_var_type_specified)
  1244. end;
  1245. if expr<>'' then
  1246. begin
  1247. BuildRecordOffsetSize(expr,toffset,tsize);
  1248. inc(l,toffset);
  1249. SetSize(tsize,true);
  1250. end;
  1251. end;
  1252. if actasmtoken in [AS_PLUS,AS_MINUS] then
  1253. inc(l,BuildConstExpression);
  1254. if (opr.typ=OPR_REFERENCE) then
  1255. begin
  1256. { don't allow direct access to fields of parameters, becuase that
  1257. will generate buggy code. Allow it only for explicit typecasting }
  1258. if (not hastype) then
  1259. begin
  1260. case opr.ref.options of
  1261. ref_parafixup :
  1262. Message(asmr_e_cannot_access_field_directly_for_parameters);
  1263. ref_selffixup :
  1264. Message(asmr_e_cannot_access_object_field_directly);
  1265. end;
  1266. end;
  1267. inc(opr.ref.offset,l)
  1268. end
  1269. else
  1270. inc(opr.val,l);
  1271. end;
  1272. Begin
  1273. expr:='';
  1274. case actasmtoken of
  1275. AS_OFFSET,
  1276. AS_TYPE,
  1277. AS_INTNUM,
  1278. AS_PLUS,
  1279. AS_MINUS,
  1280. AS_NOT,
  1281. AS_LPAREN,
  1282. AS_STRING :
  1283. Begin
  1284. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1285. Message(asmr_e_invalid_operand_type);
  1286. BuildConstant;
  1287. end;
  1288. AS_ID : { A constant expression, or a Variable ref. }
  1289. Begin
  1290. { Label or Special symbol reference? }
  1291. if actasmpattern[1] = '@' then
  1292. Begin
  1293. if actasmpattern = '@RESULT' then
  1294. Begin
  1295. InitRef;
  1296. SetupResult;
  1297. Consume(AS_ID);
  1298. end
  1299. else
  1300. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1301. begin
  1302. Message(asmr_w_CODE_and_DATA_not_supported);
  1303. Consume(AS_ID);
  1304. end
  1305. else
  1306. { Local Label }
  1307. begin
  1308. CreateLocalLabel(actasmpattern,hl,false);
  1309. Consume(AS_ID);
  1310. AddLabelOperand(hl);
  1311. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1312. Message(asmr_e_syntax_error);
  1313. end;
  1314. end
  1315. else
  1316. { support result for delphi modes }
  1317. if (m_objpas in aktmodeswitches) and (actasmpattern='RESULT') then
  1318. begin
  1319. InitRef;
  1320. SetUpResult;
  1321. Consume(AS_ID);
  1322. end
  1323. { probably a variable or normal expression }
  1324. { or a procedure (such as in CALL ID) }
  1325. else
  1326. Begin
  1327. { is it a constant ? }
  1328. if SearchIConstant(actasmpattern,l) then
  1329. Begin
  1330. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1331. Message(asmr_e_invalid_operand_type);
  1332. BuildConstant;
  1333. end
  1334. else
  1335. { Check for pascal label }
  1336. if SearchLabel(actasmpattern,hl,false) then
  1337. begin
  1338. Consume(AS_ID);
  1339. AddLabelOperand(hl);
  1340. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1341. Message(asmr_e_syntax_error);
  1342. end
  1343. else
  1344. { is it a normal variable ? }
  1345. Begin
  1346. InitRef;
  1347. expr:=actasmpattern;
  1348. Consume(AS_ID);
  1349. { typecasting? }
  1350. if (actasmtoken=AS_LPAREN) and
  1351. SearchType(expr) then
  1352. begin
  1353. hastype:=true;
  1354. Consume(AS_LPAREN);
  1355. tempstr:=actasmpattern;
  1356. Consume(AS_ID);
  1357. Consume(AS_RPAREN);
  1358. if SetupVar(tempstr,false) then
  1359. begin
  1360. MaybeRecordOffset;
  1361. { add a constant expression? }
  1362. if (actasmtoken=AS_PLUS) then
  1363. begin
  1364. l:=BuildConstExpression;
  1365. if opr.typ=OPR_CONSTANT then
  1366. inc(opr.val,l)
  1367. else
  1368. inc(opr.ref.offset,l);
  1369. end
  1370. end
  1371. else
  1372. Message1(sym_e_unknown_id,tempstr);
  1373. end
  1374. else
  1375. begin
  1376. if SetupVar(expr,false) then
  1377. begin
  1378. MaybeRecordOffset;
  1379. { add a constant expression? }
  1380. if (actasmtoken=AS_PLUS) then
  1381. begin
  1382. l:=BuildConstExpression;
  1383. if opr.typ=OPR_CONSTANT then
  1384. inc(opr.val,l)
  1385. else
  1386. inc(opr.ref.offset,l);
  1387. end
  1388. end
  1389. else
  1390. Begin
  1391. { not a variable, check special variables.. }
  1392. if expr = 'SELF' then
  1393. SetupSelf
  1394. else
  1395. Message1(sym_e_unknown_id,expr);
  1396. end;
  1397. end;
  1398. end;
  1399. { handle references }
  1400. if actasmtoken=AS_LBRACKET then
  1401. begin
  1402. if opr.typ=OPR_CONSTANT then
  1403. begin
  1404. l:=opr.val;
  1405. opr.typ:=OPR_REFERENCE;
  1406. reset_reference(opr.Ref);
  1407. opr.Ref.Offset:=l;
  1408. end;
  1409. BuildReference;
  1410. MaybeRecordOffset;
  1411. end;
  1412. end;
  1413. end;
  1414. AS_REGISTER : { Register, a variable reference or a constant reference }
  1415. Begin
  1416. { save the type of register used. }
  1417. tempreg:=actasmregister;
  1418. Consume(AS_REGISTER);
  1419. if actasmtoken = AS_COLON then
  1420. Begin
  1421. Consume(AS_COLON);
  1422. InitRef;
  1423. opr.ref.segment:=tempreg;
  1424. BuildReference;
  1425. end
  1426. else
  1427. { Simple register }
  1428. begin
  1429. if not (opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1430. Message(asmr_e_invalid_operand_type);
  1431. opr.typ:=OPR_REGISTER;
  1432. opr.reg:=tempreg;
  1433. size:=reg_2_opsize[opr.reg];
  1434. end;
  1435. end;
  1436. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1437. Begin
  1438. InitRef;
  1439. BuildReference;
  1440. MaybeRecordOffset;
  1441. end;
  1442. AS_SEG :
  1443. Begin
  1444. Message(asmr_e_seg_not_supported);
  1445. Consume(actasmtoken);
  1446. end;
  1447. AS_SEPARATOR,
  1448. AS_END,
  1449. AS_COMMA: ;
  1450. else
  1451. Message(asmr_e_syn_operand);
  1452. end;
  1453. if not(actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1454. begin
  1455. Message(asmr_e_syntax_error);
  1456. RecoverConsume(true);
  1457. end;
  1458. end;
  1459. {*****************************************************************************
  1460. T386IntelInstruction
  1461. *****************************************************************************}
  1462. type
  1463. P386IntelInstruction=^T386IntelInstruction;
  1464. T386IntelInstruction=object(T386Instruction)
  1465. procedure InitOperands;virtual;
  1466. procedure BuildOpcode;virtual;
  1467. end;
  1468. procedure T386IntelInstruction.InitOperands;
  1469. var
  1470. i : longint;
  1471. begin
  1472. for i:=1 to 3 do
  1473. Operands[i]:=new(P386IntelOperand,Init);
  1474. end;
  1475. Procedure T386IntelInstruction.BuildOpCode;
  1476. var
  1477. PrefixOp,OverrideOp: tasmop;
  1478. size : topsize;
  1479. operandnum : longint;
  1480. Begin
  1481. PrefixOp:=A_None;
  1482. OverrideOp:=A_None;
  1483. { prefix seg opcode / prefix opcode }
  1484. repeat
  1485. if is_prefix(actopcode) then
  1486. begin
  1487. PrefixOp:=ActOpcode;
  1488. opcode:=ActOpcode;
  1489. condition:=ActCondition;
  1490. opsize:=ActOpsize;
  1491. ConcatInstruction(curlist);
  1492. Consume(AS_OPCODE);
  1493. end
  1494. else
  1495. if is_override(actopcode) then
  1496. begin
  1497. OverrideOp:=ActOpcode;
  1498. opcode:=ActOpcode;
  1499. condition:=ActCondition;
  1500. opsize:=ActOpsize;
  1501. ConcatInstruction(curlist);
  1502. Consume(AS_OPCODE);
  1503. end
  1504. else
  1505. break;
  1506. { allow for newline after prefix or override }
  1507. while actasmtoken=AS_SEPARATOR do
  1508. Consume(AS_SEPARATOR);
  1509. until (actasmtoken<>AS_OPCODE);
  1510. { opcode }
  1511. if (actasmtoken <> AS_OPCODE) then
  1512. Begin
  1513. Message(asmr_e_invalid_or_missing_opcode);
  1514. RecoverConsume(false);
  1515. exit;
  1516. end;
  1517. { Fill the instr object with the current state }
  1518. Opcode:=ActOpcode;
  1519. condition:=ActCondition;
  1520. opsize:=ActOpsize;
  1521. { Valid combination of prefix/override and instruction ? }
  1522. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1523. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1524. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1525. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1526. { We are reading operands, so opcode will be an AS_ID }
  1527. operandnum:=1;
  1528. Consume(AS_OPCODE);
  1529. { Zero operand opcode ? }
  1530. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1531. begin
  1532. operandnum:=0;
  1533. exit;
  1534. end;
  1535. { Read Operands }
  1536. repeat
  1537. case actasmtoken of
  1538. { End of asm operands for this opcode }
  1539. AS_END,
  1540. AS_SEPARATOR :
  1541. break;
  1542. { Operand delimiter }
  1543. AS_COMMA :
  1544. Begin
  1545. if operandnum > MaxOperands then
  1546. Message(asmr_e_too_many_operands)
  1547. else
  1548. Inc(operandnum);
  1549. Consume(AS_COMMA);
  1550. end;
  1551. { Typecast, Constant Expression, Type Specifier }
  1552. AS_DWORD,
  1553. AS_BYTE,
  1554. AS_WORD,
  1555. AS_TBYTE,
  1556. AS_QWORD :
  1557. Begin
  1558. { load the size in a temp variable, so it can be set when the
  1559. operand is read }
  1560. Case actasmtoken of
  1561. AS_DWORD : size:=S_L;
  1562. AS_WORD : size:=S_W;
  1563. AS_BYTE : size:=S_B;
  1564. AS_QWORD : begin
  1565. if (opcode=A_FCOM) or
  1566. (opcode=A_FCOMP) or
  1567. (opcode=A_FDIV) or
  1568. (opcode=A_FDIVR) or
  1569. (opcode=A_FMUL) or
  1570. (opcode=A_FSUB) or
  1571. (opcode=A_FSUBR) or
  1572. (opcode=A_FLD) or
  1573. (opcode=A_FST) or
  1574. (opcode=A_FSTP) or
  1575. (opcode=A_FADD) then
  1576. size:=S_FL
  1577. else
  1578. size:=S_IQ;
  1579. end;
  1580. AS_TBYTE : size:=S_FX;
  1581. end;
  1582. Consume(actasmtoken);
  1583. if actasmtoken=AS_PTR then
  1584. begin
  1585. Consume(AS_PTR);
  1586. Operands[operandnum]^.InitRef;
  1587. end;
  1588. Operands[operandnum]^.BuildOperand;
  1589. { now set the size which was specified by the override }
  1590. Operands[operandnum]^.size:=size;
  1591. end;
  1592. { Type specifier }
  1593. AS_NEAR,
  1594. AS_FAR :
  1595. Begin
  1596. if actasmtoken = AS_NEAR then
  1597. Message(asmr_w_near_ignored)
  1598. else
  1599. Message(asmr_w_far_ignored);
  1600. Consume(actasmtoken);
  1601. if actasmtoken=AS_PTR then
  1602. begin
  1603. Consume(AS_PTR);
  1604. Operands[operandnum]^.InitRef;
  1605. end;
  1606. Operands[operandnum]^.BuildOperand;
  1607. end;
  1608. else
  1609. Operands[operandnum]^.BuildOperand;
  1610. end; { end case }
  1611. until false;
  1612. Ops:=operandnum;
  1613. end;
  1614. Procedure BuildConstant(maxvalue: longint);
  1615. var
  1616. strlength: byte;
  1617. asmsym,
  1618. expr: string;
  1619. value : longint;
  1620. Begin
  1621. strlength:=0; { assume it is a DB }
  1622. Repeat
  1623. Case actasmtoken of
  1624. AS_STRING:
  1625. Begin
  1626. if maxvalue = $ffff then
  1627. strlength:=2
  1628. else
  1629. if maxvalue = longint($ffffffff) then
  1630. strlength:=4;
  1631. { DD and DW cases }
  1632. if strlength <> 0 then
  1633. Begin
  1634. if Not PadZero(actasmpattern,strlength) then
  1635. Message(scan_f_string_exceeds_line);
  1636. end;
  1637. expr:=actasmpattern;
  1638. Consume(AS_STRING);
  1639. Case actasmtoken of
  1640. AS_COMMA:
  1641. Consume(AS_COMMA);
  1642. AS_END,
  1643. AS_SEPARATOR: ;
  1644. else
  1645. Message(asmr_e_invalid_string_expression);
  1646. end;
  1647. ConcatString(curlist,expr);
  1648. end;
  1649. AS_PLUS,
  1650. AS_MINUS,
  1651. AS_LPAREN,
  1652. AS_NOT,
  1653. AS_INTNUM,
  1654. AS_ID :
  1655. Begin
  1656. BuildConstSymbolExpression(false,false,value,asmsym);
  1657. if asmsym<>'' then
  1658. begin
  1659. if maxvalue<>longint($ffffffff) then
  1660. Message(asmr_w_const32bit_for_address);
  1661. ConcatConstSymbol(curlist,asmsym,value)
  1662. end
  1663. else
  1664. ConcatConstant(curlist,value,maxvalue);
  1665. end;
  1666. AS_COMMA:
  1667. Consume(AS_COMMA);
  1668. AS_END,
  1669. AS_SEPARATOR:
  1670. break;
  1671. else
  1672. begin
  1673. Message(asmr_e_syn_constant);
  1674. RecoverConsume(false);
  1675. end
  1676. end;
  1677. Until false;
  1678. end;
  1679. Function Assemble: tnode;
  1680. Var
  1681. hl : PAsmLabel;
  1682. instr : T386IntelInstruction;
  1683. Begin
  1684. Message1(asmr_d_start_reading,'intel');
  1685. inexpression:=FALSE;
  1686. firsttoken:=TRUE;
  1687. if assigned(procinfo^.returntype.def) and
  1688. (is_fpu(procinfo^.returntype.def) or
  1689. ret_in_acc(procinfo^.returntype.def)) then
  1690. procinfo^.funcret_state:=vs_assigned;
  1691. { sets up all opcode and register tables in uppercase }
  1692. if not _asmsorted then
  1693. Begin
  1694. SetupTables;
  1695. _asmsorted:=TRUE;
  1696. end;
  1697. curlist:=TAAsmoutput.Create;
  1698. { setup label linked list }
  1699. new(LocalLabelList,Init);
  1700. { start tokenizer }
  1701. c:=current_scanner^.asmgetchar;
  1702. gettoken;
  1703. { main loop }
  1704. repeat
  1705. case actasmtoken of
  1706. AS_LLABEL:
  1707. Begin
  1708. if CreateLocalLabel(actasmpattern,hl,true) then
  1709. ConcatLabel(curlist,hl);
  1710. Consume(AS_LLABEL);
  1711. end;
  1712. AS_LABEL:
  1713. Begin
  1714. if SearchLabel(upper(actasmpattern),hl,true) then
  1715. ConcatLabel(curlist,hl)
  1716. else
  1717. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1718. Consume(AS_LABEL);
  1719. end;
  1720. AS_DW :
  1721. Begin
  1722. inexpression:=true;
  1723. Consume(AS_DW);
  1724. BuildConstant($ffff);
  1725. inexpression:=false;
  1726. end;
  1727. AS_DB :
  1728. Begin
  1729. inexpression:=true;
  1730. Consume(AS_DB);
  1731. BuildConstant($ff);
  1732. inexpression:=false;
  1733. end;
  1734. AS_DD :
  1735. Begin
  1736. inexpression:=true;
  1737. Consume(AS_DD);
  1738. BuildConstant(longint($ffffffff));
  1739. inexpression:=false;
  1740. end;
  1741. AS_OPCODE :
  1742. Begin
  1743. instr.init;
  1744. instr.BuildOpcode;
  1745. { We need AT&T style operands }
  1746. instr.SwapOperands;
  1747. { Must be done with args in ATT order }
  1748. instr.CheckNonCommutativeOpcodes;
  1749. instr.AddReferenceSizes;
  1750. instr.SetInstructionOpsize;
  1751. instr.CheckOperandSizes;
  1752. instr.ConcatInstruction(curlist);
  1753. instr.done;
  1754. end;
  1755. AS_SEPARATOR :
  1756. Begin
  1757. Consume(AS_SEPARATOR);
  1758. end;
  1759. AS_END :
  1760. break; { end assembly block }
  1761. else
  1762. Begin
  1763. Message(asmr_e_syntax_error);
  1764. RecoverConsume(false);
  1765. end;
  1766. end; { end case }
  1767. until false;
  1768. { Check LocalLabelList }
  1769. LocalLabelList^.CheckEmitted;
  1770. dispose(LocalLabelList,Done);
  1771. { Return the list in an asmnode }
  1772. assemble:=casmnode.create(curlist);
  1773. Message1(asmr_d_finish_reading,'intel');
  1774. end;
  1775. {*****************************************************************************
  1776. Initialize
  1777. *****************************************************************************}
  1778. var
  1779. old_exit : pointer;
  1780. procedure ra386int_exit;
  1781. begin
  1782. exitproc:=old_exit;
  1783. if assigned(iasmops) then
  1784. dispose(iasmops,done);
  1785. if assigned(iasmregs) then
  1786. dispose(iasmregs);
  1787. end;
  1788. begin
  1789. old_exit:=exitproc;
  1790. exitproc:=@ra386int_exit;
  1791. end.
  1792. {
  1793. $Log$
  1794. Revision 1.7 2001-01-05 17:36:58 florian
  1795. * the info about exception frames is stored now on the stack
  1796. instead on the heap
  1797. Revision 1.6 2000/12/25 00:07:34 peter
  1798. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1799. tlinkedlist objects)
  1800. Revision 1.5 2000/12/07 17:19:46 jonas
  1801. * new constant handling: from now on, hex constants >$7fffffff are
  1802. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1803. and became $ffffffff80000000), all constants in the longint range
  1804. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1805. are cardinals and the rest are int64's.
  1806. * added lots of longint typecast to prevent range check errors in the
  1807. compiler and rtl
  1808. * type casts of symbolic ordinal constants are now preserved
  1809. * fixed bug where the original resulttype wasn't restored correctly
  1810. after doing a 64bit rangecheck
  1811. Revision 1.4 2000/11/30 20:27:51 peter
  1812. * merged fix for bug 1229
  1813. Revision 1.3 2000/11/29 00:30:51 florian
  1814. * unused units removed from uses clause
  1815. * some changes for widestrings
  1816. Revision 1.2 2000/10/31 22:02:57 peter
  1817. * symtable splitted, no real code changes
  1818. Revision 1.1 2000/10/15 09:47:43 peter
  1819. * moved to i386/
  1820. Revision 1.8 2000/10/14 10:14:52 peter
  1821. * moehrendorf oct 2000 rewrite
  1822. Revision 1.7 2000/09/24 21:19:51 peter
  1823. * delphi compile fixes
  1824. Revision 1.6 2000/09/24 15:06:26 peter
  1825. * use defines.inc
  1826. Revision 1.5 2000/08/27 16:11:52 peter
  1827. * moved some util functions from globals,cobjects to cutils
  1828. * splitted files into finput,fmodule
  1829. Revision 1.4 2000/08/12 15:32:02 peter
  1830. * reference reading fix from Jonas (merged)
  1831. Revision 1.3 2000/07/30 17:04:43 peter
  1832. * merged fixes
  1833. Revision 1.2 2000/07/13 11:32:48 michael
  1834. + removed logs
  1835. }