ra386int.pas 68 KB

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