ra386int.pas 69 KB

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