rautils.pas 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. This unit implements some support routines for assembler parsing
  4. independent of the processor
  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. Unit RAUtils;
  18. {$i fpcdefs.inc}
  19. Interface
  20. Uses
  21. cutils,cclasses,
  22. globtype,aasmbase,aasmtai,aasmdata,cpubase,cpuinfo,cgbase,cgutils,
  23. symconst,symbase,symtype,symdef,symsym,constexp,symcpu;
  24. Const
  25. RPNMax = 10; { I think you only need 4, but just to be safe }
  26. OpMax = 25;
  27. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  28. {---------------------------------------------------------------------
  29. Instruction management
  30. ---------------------------------------------------------------------}
  31. type
  32. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_LOCAL,
  33. OPR_REFERENCE,OPR_REGISTER,OPR_COND,OPR_REGSET,
  34. OPR_SHIFTEROP,OPR_MODEFLAGS,OPR_SPECIALREG,
  35. OPR_REGPAIR);
  36. TOprRec = record
  37. case typ:TOprType of
  38. OPR_NONE : ();
  39. {$if defined(AVR)}
  40. OPR_CONSTANT : (val:longint);
  41. {$elseif defined(i8086)}
  42. OPR_CONSTANT : (val:longint);
  43. {$else}
  44. OPR_CONSTANT : (val:aint);
  45. {$endif}
  46. OPR_SYMBOL : (symbol:tasmsymbol;symofs:aint;symseg:boolean;sym_farproc_entry:boolean);
  47. OPR_REFERENCE : (varsize:asizeint; constoffset: asizeint;ref_farproc_entry:boolean;ref:treference);
  48. OPR_LOCAL : (localvarsize, localconstoffset: asizeint;localsym:tabstractnormalvarsym;localsymofs:aint;localsegment,localindexreg:tregister;localscale:byte;localgetoffset,localforceref:boolean);
  49. OPR_REGISTER : (reg:tregister);
  50. {$ifdef m68k}
  51. OPR_REGSET : (regsetdata,regsetaddr,regsetfpu : tcpuregisterset);
  52. OPR_REGPAIR : (reghi,reglo: tregister);
  53. {$endif m68k}
  54. {$ifdef powerpc}
  55. OPR_COND : (cond : tasmcond);
  56. {$endif powerpc}
  57. {$ifdef POWERPC64}
  58. OPR_COND : (cond : tasmcond);
  59. {$endif POWERPC64}
  60. {$ifdef arm}
  61. OPR_REGSET : (regset : tcpuregisterset; regtype: tregistertype; subreg: tsubregister; usermode: boolean);
  62. OPR_SHIFTEROP : (shifterop : tshifterop);
  63. OPR_COND : (cc : tasmcond);
  64. OPR_MODEFLAGS : (flags : tcpumodeflags);
  65. OPR_SPECIALREG: (specialreg : tregister; specialregflags : tspecialregflags);
  66. {$endif arm}
  67. {$ifdef aarch64}
  68. OPR_SHIFTEROP : (shifterop : tshifterop);
  69. OPR_COND : (cc : tasmcond);
  70. {$endif aarch64}
  71. end;
  72. TOperand = class
  73. opr : TOprRec;
  74. typesize : byte;
  75. haslabelref, { if the operand has a label, used in a reference like a
  76. var (e.g. 'mov ax, word ptr [label+5]', but *not*
  77. e.g. 'jmp label') }
  78. hasproc, { if the operand has a procedure/function reference }
  79. hastype, { if the operand has typecasted variable }
  80. hasvar : boolean; { if the operand is loaded with a variable }
  81. size : TCGSize;
  82. constructor create;virtual;
  83. destructor destroy;override;
  84. Procedure SetSize(_size:longint;force:boolean);virtual;
  85. Procedure SetCorrectSize(opcode:tasmop);virtual;
  86. Function SetupResult:boolean;virtual;
  87. Function SetupSelf:boolean;
  88. Function SetupOldEBP:boolean;
  89. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  90. Function CheckOperand: boolean; virtual;
  91. Procedure InitRef;
  92. Procedure InitRefConvertLocal;
  93. protected
  94. Procedure InitRefError;
  95. end;
  96. TCOperand = class of TOperand;
  97. TInstruction = class
  98. operands : array[1..max_operands] of toperand;
  99. opcode : tasmop;
  100. condition : tasmcond;
  101. ops : byte;
  102. labeled : boolean;
  103. filepos : tfileposinfo;
  104. constructor create(optype : tcoperand);virtual;
  105. destructor destroy;override;
  106. { converts the instruction to an instruction how it's used by the assembler writer
  107. and concats it to the passed list. The newly created item is returned if the
  108. instruction was valid, otherwise nil is returned }
  109. function ConcatInstruction(p:TAsmList) : tai;virtual;
  110. end;
  111. {---------------------------------------------------------------------}
  112. { Expression parser types }
  113. {---------------------------------------------------------------------}
  114. TExprOperator = record
  115. ch: char; { operator }
  116. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  117. end;
  118. {**********************************************************************}
  119. { The following operators are supported: }
  120. { '+' : addition }
  121. { '-' : subtraction }
  122. { '*' : multiplication }
  123. { '/' : modulo division }
  124. { '^' : exclusive or }
  125. { '<' : shift left }
  126. { '>' : shift right }
  127. { '&' : bitwise and }
  128. { '|' : bitwise or }
  129. { '~' : bitwise complement }
  130. { '%' : modulo division }
  131. { nnn: longint numbers }
  132. { ( and ) parenthesis }
  133. {**********************************************************************}
  134. TExprParse = class
  135. public
  136. Constructor create;
  137. Destructor Destroy;override;
  138. Function Evaluate(Expr: String): tcgint;
  139. Function Priority(_Operator: Char): aint;
  140. private
  141. RPNStack : Array[1..RPNMax] of tcgint; { Stack For RPN calculator }
  142. RPNTop : tcgint;
  143. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  144. OpTop : tcgint;
  145. Procedure RPNPush(Num: tcgint);
  146. Function RPNPop: tcgint;
  147. Procedure RPNCalc(const token: String; prefix: boolean);
  148. Procedure OpPush(_Operator: char; prefix: boolean);
  149. { In reality returns TExprOperaotr }
  150. Procedure OpPop(var _Operator:TExprOperator);
  151. end;
  152. { Evaluate an expression string to a tcgint }
  153. Function CalculateExpression(const expression: string): tcgint;
  154. {---------------------------------------------------------------------}
  155. { String routines }
  156. {---------------------------------------------------------------------}
  157. Function ParseVal(const S:String;base:byte):tcgint;
  158. Function PadZero(Var s: String; n: byte): Boolean;
  159. Function EscapeToPascal(const s:string): string;
  160. {---------------------------------------------------------------------
  161. Symbol helper routines
  162. ---------------------------------------------------------------------}
  163. procedure AsmSearchSym(const s:string;out srsym:tsym;out srsymtable:TSymtable);
  164. Function GetRecordOffsetSize(s:string;out Offset: tcgint;out Size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean):boolean;
  165. Function SearchType(const hs:string;out size:tcgint): Boolean;
  166. Function SearchRecordType(const s:string): boolean;
  167. Function SearchIConstant(const s:string; var l:tcgint): boolean;
  168. Function AsmRegisterPara(sym: tabstractnormalvarsym): boolean;
  169. {---------------------------------------------------------------------
  170. Instruction generation routines
  171. ---------------------------------------------------------------------}
  172. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  173. Procedure ConcatConstant(p : TAsmList;value: tcgint; constsize:byte);
  174. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:tcgint;constsize:byte;isofs:boolean);
  175. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  176. Procedure ConcatString(p : TAsmList;s:string);
  177. procedure ConcatAlign(p:TAsmList;l:tcgint);
  178. Procedure ConcatPublic(p:TAsmList;const s : string);
  179. Procedure ConcatLocal(p:TAsmList;const s : string);
  180. Implementation
  181. uses
  182. SysUtils,
  183. defutil,systems,verbose,globals,
  184. symtable,paramgr,
  185. aasmcpu,
  186. procinfo;
  187. {*************************************************************************
  188. TExprParse
  189. *************************************************************************}
  190. Constructor TExprParse.create;
  191. Begin
  192. end;
  193. Procedure TExprParse.RPNPush(Num : tcgint);
  194. { Add an operand to the top of the RPN stack }
  195. begin
  196. if RPNTop < RPNMax then
  197. begin
  198. Inc(RPNTop);
  199. RPNStack[RPNTop]:=Num;
  200. end
  201. else
  202. Message(asmr_e_expr_illegal);
  203. end;
  204. Function TExprParse.RPNPop : tcgint;
  205. { Get the operand at the top of the RPN stack }
  206. begin
  207. RPNPop:=0;
  208. if RPNTop > 0 then
  209. begin
  210. RPNPop:=RPNStack[RPNTop];
  211. Dec(RPNTop);
  212. end
  213. else
  214. Message(asmr_e_expr_illegal);
  215. end;
  216. Procedure TExprParse.RPNCalc(const Token : String; prefix:boolean); { RPN Calculator }
  217. Var
  218. Temp : tcgint;
  219. n1,n2 : tcgint;
  220. LocalError : Integer;
  221. begin
  222. { Handle operators }
  223. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  224. Case Token[1] of
  225. '+' :
  226. Begin
  227. if not prefix then
  228. RPNPush(RPNPop + RPNPop);
  229. end;
  230. '-' :
  231. Begin
  232. if prefix then
  233. RPNPush(-(RPNPop))
  234. else
  235. begin
  236. n1:=RPNPop;
  237. n2:=RPNPop;
  238. RPNPush(n2 - n1);
  239. end;
  240. end;
  241. '*' : RPNPush(RPNPop * RPNPop);
  242. '&' :
  243. begin
  244. n1:=RPNPop;
  245. n2:=RPNPop;
  246. RPNPush(n2 and n1);
  247. end;
  248. '|' :
  249. begin
  250. n1:=RPNPop;
  251. n2:=RPNPop;
  252. RPNPush(n2 or n1);
  253. end;
  254. '~' : RPNPush(NOT RPNPop);
  255. '<' :
  256. begin
  257. n1:=RPNPop;
  258. n2:=RPNPop;
  259. RPNPush(n2 SHL n1);
  260. end;
  261. '>' :
  262. begin
  263. n1:=RPNPop;
  264. n2:=RPNPop;
  265. RPNPush(n2 SHR n1);
  266. end;
  267. '%' :
  268. begin
  269. Temp:=RPNPop;
  270. if Temp <> 0 then
  271. RPNPush(RPNPop mod Temp)
  272. else
  273. begin
  274. Message(asmr_e_expr_zero_divide);
  275. { push 1 for error recovery }
  276. RPNPush(1);
  277. end;
  278. end;
  279. '^' : RPNPush(RPNPop XOR RPNPop);
  280. '/' :
  281. begin
  282. Temp:=RPNPop;
  283. if Temp <> 0 then
  284. RPNPush(RPNPop div Temp)
  285. else
  286. begin
  287. Message(asmr_e_expr_zero_divide);
  288. { push 1 for error recovery }
  289. RPNPush(1);
  290. end;
  291. end;
  292. end
  293. else
  294. begin
  295. { Convert String to number and add to stack }
  296. Val(Token, Temp, LocalError);
  297. if LocalError = 0 then
  298. RPNPush(Temp)
  299. else
  300. begin
  301. Message(asmr_e_expr_illegal);
  302. { push 1 for error recovery }
  303. RPNPush(1);
  304. end;
  305. end;
  306. end;
  307. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  308. { Add an operator onto top of the stack }
  309. begin
  310. if OpTop < OpMax then
  311. begin
  312. Inc(OpTop);
  313. OpStack[OpTop].ch:=_Operator;
  314. OpStack[OpTop].is_prefix:=prefix;
  315. end
  316. else
  317. Message(asmr_e_expr_illegal);
  318. end;
  319. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  320. { Get operator at the top of the stack }
  321. begin
  322. if OpTop > 0 then
  323. begin
  324. _Operator:=OpStack[OpTop];
  325. Dec(OpTop);
  326. end
  327. else
  328. Message(asmr_e_expr_illegal);
  329. end;
  330. Function TExprParse.Priority(_Operator : Char) : aint;
  331. { Return priority of operator }
  332. { The greater the priority, the higher the precedence }
  333. begin
  334. Priority:=0;
  335. Case _Operator OF
  336. '(' :
  337. Priority:=0;
  338. '|','^','~' : // the lowest priority: OR, XOR, NOT
  339. Priority:=0;
  340. '&' : // bigger priority: AND
  341. Priority:=1;
  342. '+', '-' : // bigger priority: +, -
  343. Priority:=2;
  344. '*', '/','%','<','>' : // the highest priority: *, /, MOD, SHL, SHR
  345. Priority:=3;
  346. else
  347. Message(asmr_e_expr_illegal);
  348. end;
  349. end;
  350. Function TExprParse.Evaluate(Expr : String):tcgint;
  351. Var
  352. I : longint;
  353. Token : String;
  354. opr : TExprOperator;
  355. begin
  356. Evaluate:=0;
  357. { Reset stacks }
  358. OpTop :=0;
  359. RPNTop:=0;
  360. Token :='';
  361. { nothing to do ? }
  362. if Expr='' then
  363. exit;
  364. For I:=1 to Length(Expr) DO
  365. begin
  366. if Expr[I] in ['0'..'9'] then
  367. begin { Build multi-digit numbers }
  368. Token:=Token + Expr[I];
  369. if I = Length(Expr) then { Send last one to calculator }
  370. RPNCalc(Token,false);
  371. end
  372. else
  373. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  374. begin
  375. if Token <> '' then
  376. begin { Send last built number to calc. }
  377. RPNCalc(Token,false);
  378. Token:='';
  379. end;
  380. Case Expr[I] OF
  381. '(' : OpPush('(',false);
  382. ')' : begin
  383. While (OpTop>0) and (OpStack[OpTop].ch <> '(') DO
  384. Begin
  385. OpPop(opr);
  386. RPNCalc(opr.ch,opr.is_prefix);
  387. end;
  388. OpPop(opr); { Pop off and ignore the '(' }
  389. end;
  390. '+','-','~' : Begin
  391. { workaround for -2147483648 }
  392. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  393. begin
  394. token:='-';
  395. expr[i]:='+';
  396. end;
  397. { if start of expression then surely a prefix }
  398. { or if previous char was also an operator }
  399. if (I = 1) or (not (Expr[I-1] in ['0'..'9',')'])) then
  400. OpPush(Expr[I],true)
  401. else
  402. Begin
  403. { Evaluate all higher priority operators }
  404. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  405. Begin
  406. OpPop(opr);
  407. RPNCalc(opr.ch,opr.is_prefix);
  408. end;
  409. OpPush(Expr[I],false);
  410. End;
  411. end;
  412. '*', '/',
  413. '^','|','&',
  414. '%','<','>' : begin
  415. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  416. Begin
  417. OpPop(opr);
  418. RPNCalc(opr.ch,opr.is_prefix);
  419. end;
  420. OpPush(Expr[I],false);
  421. end;
  422. end; { Case }
  423. end
  424. else
  425. Message(asmr_e_expr_illegal); { Handle bad input error }
  426. end;
  427. { Pop off the remaining operators }
  428. While OpTop > 0 do
  429. Begin
  430. OpPop(opr);
  431. RPNCalc(opr.ch,opr.is_prefix);
  432. end;
  433. { The result is stored on the top of the stack }
  434. Evaluate:=RPNPop;
  435. end;
  436. Destructor TExprParse.Destroy;
  437. Begin
  438. end;
  439. Function CalculateExpression(const expression: string): tcgint;
  440. var
  441. expr: TExprParse;
  442. Begin
  443. expr:=TExprParse.create;
  444. CalculateExpression:=expr.Evaluate(expression);
  445. expr.Free;
  446. end;
  447. {*************************************************************************}
  448. { String conversions/utils }
  449. {*************************************************************************}
  450. Function EscapeToPascal(const s:string): string;
  451. { converts a C styled string - which contains escape }
  452. { characters to a pascal style string. }
  453. var
  454. i,len : asizeint;
  455. hs : string;
  456. temp : string;
  457. c : char;
  458. Begin
  459. hs:='';
  460. len:=0;
  461. i:=0;
  462. while (i<length(s)) and (len<255) do
  463. begin
  464. Inc(i);
  465. if (s[i]='\') and (i<length(s)) then
  466. Begin
  467. inc(i);
  468. case s[i] of
  469. '\' :
  470. c:='\';
  471. 'b':
  472. c:=#8;
  473. 'f':
  474. c:=#12;
  475. 'n':
  476. c:=#10;
  477. 'r':
  478. c:=#13;
  479. 't':
  480. c:=#9;
  481. '"':
  482. c:='"';
  483. '0'..'7':
  484. Begin
  485. temp:=s[i];
  486. temp:=temp+s[i+1];
  487. temp:=temp+s[i+2];
  488. inc(i,2);
  489. c:=chr(ParseVal(temp,8));
  490. end;
  491. 'x':
  492. Begin
  493. temp:=s[i+1];
  494. temp:=temp+s[i+2];
  495. inc(i,2);
  496. c:=chr(ParseVal(temp,16));
  497. end;
  498. else
  499. Begin
  500. Message1(asmr_e_escape_seq_ignored,s[i]);
  501. c:=s[i];
  502. end;
  503. end;
  504. end
  505. else
  506. c:=s[i];
  507. inc(len);
  508. hs[len]:=c;
  509. end;
  510. hs[0]:=chr(len);
  511. EscapeToPascal:=hs;
  512. end;
  513. Function ParseVal(const S:String;base:byte):tcgint;
  514. { Converts a decimal string to tcgint }
  515. var
  516. code : integer;
  517. errmsg : word;
  518. prefix : string[2];
  519. Begin
  520. case base of
  521. 2 :
  522. begin
  523. errmsg:=asmr_e_error_converting_binary;
  524. prefix:='%';
  525. end;
  526. 8 :
  527. begin
  528. errmsg:=asmr_e_error_converting_octal;
  529. prefix:='&';
  530. end;
  531. 10 :
  532. begin
  533. errmsg:=asmr_e_error_converting_decimal;
  534. prefix:='';
  535. end;
  536. 16 :
  537. begin
  538. errmsg:=asmr_e_error_converting_hexadecimal;
  539. prefix:='$';
  540. end;
  541. else
  542. internalerror(200501202);
  543. end;
  544. val(prefix+s,result,code);
  545. if code<>0 then
  546. begin
  547. val(prefix+s,result,code);
  548. if code<>0 then
  549. begin
  550. Message1(errmsg,s);
  551. result:=0;
  552. end;
  553. end;
  554. end;
  555. Function PadZero(Var s: String; n: byte): Boolean;
  556. Begin
  557. PadZero:=TRUE;
  558. { Do some error checking first }
  559. if Length(s) = n then
  560. exit
  561. else
  562. if Length(s) > n then
  563. Begin
  564. PadZero:=FALSE;
  565. delete(s,n+1,length(s));
  566. exit;
  567. end
  568. else
  569. PadZero:=TRUE;
  570. { Fill it up with the specified character }
  571. fillchar(s[length(s)+1],n-1,#0);
  572. s[0]:=chr(n);
  573. end;
  574. {****************************************************************************
  575. TOperand
  576. ****************************************************************************}
  577. constructor TOperand.Create;
  578. begin
  579. size:=OS_NO;
  580. hasproc:=false;
  581. hastype:=false;
  582. hasvar:=false;
  583. FillChar(Opr,sizeof(Opr),0);
  584. end;
  585. destructor TOperand.destroy;
  586. begin
  587. end;
  588. Procedure TOperand.SetSize(_size:longint;force:boolean);
  589. begin
  590. if force or
  591. ((size = OS_NO) and (_size<=16)) then
  592. Begin
  593. case _size of
  594. 1 : size:=OS_8;
  595. 2 : size:=OS_16{ could be S_IS};
  596. 4 : size:=OS_32{ could be S_IL or S_FS};
  597. 8 : size:=OS_64{ could be S_D or S_FL};
  598. 10 : size:=OS_F80;
  599. 16 : size:=OS_128;
  600. end;
  601. end;
  602. end;
  603. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  604. begin
  605. end;
  606. function TOperand.SetupResult:boolean;
  607. begin
  608. SetupResult:=false;
  609. { replace by correct offset. }
  610. with current_procinfo.procdef do
  611. if (not is_void(returndef)) then
  612. begin
  613. if (m_tp7 in current_settings.modeswitches) and
  614. not (df_generic in defoptions) and
  615. (po_assembler in procoptions) and
  616. (not paramanager.ret_in_param(returndef,current_procinfo.procdef)) then
  617. begin
  618. message(asmr_e_cannot_use_RESULT_here);
  619. exit;
  620. end;
  621. SetupResult:=setupvar('result',false)
  622. end
  623. else
  624. message(asmr_e_void_function);
  625. end;
  626. Function TOperand.SetupSelf:boolean;
  627. Begin
  628. SetupSelf:=false;
  629. if assigned(current_structdef) then
  630. SetupSelf:=setupvar('self',false)
  631. else
  632. Message(asmr_e_cannot_use_SELF_outside_a_method);
  633. end;
  634. Function TOperand.SetupOldEBP:boolean;
  635. Begin
  636. SetupOldEBP:=false;
  637. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  638. SetupOldEBP:=setupvar('parentframe',false)
  639. else
  640. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  641. end;
  642. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  643. function symtable_has_localvarsyms(st:TSymtable):boolean;
  644. var
  645. sym : tsym;
  646. i : longint;
  647. begin
  648. result:=false;
  649. for i:=0 to st.SymList.Count-1 do
  650. begin
  651. sym:=tsym(st.SymList[i]);
  652. if sym.typ=localvarsym then
  653. begin
  654. result:=true;
  655. exit;
  656. end;
  657. end;
  658. end;
  659. procedure setconst(l:aint);
  660. begin
  661. { We return the address of the field, just like Delphi/TP }
  662. case opr.typ of
  663. OPR_NONE :
  664. begin
  665. opr.typ:=OPR_CONSTANT;
  666. opr.val:=l;
  667. end;
  668. OPR_CONSTANT :
  669. inc(opr.val,l);
  670. OPR_REFERENCE :
  671. inc(opr.ref.offset,l);
  672. OPR_LOCAL :
  673. inc(opr.localsymofs,l);
  674. else
  675. Message(asmr_e_invalid_operand_type);
  676. end;
  677. end;
  678. procedure setvarsize(sym: tabstractvarsym);
  679. var
  680. harrdef: tarraydef;
  681. l: asizeint;
  682. begin
  683. case sym.vardef.typ of
  684. orddef,
  685. enumdef,
  686. pointerdef,
  687. procvardef,
  688. floatdef :
  689. SetSize(sym.getsize,false);
  690. arraydef :
  691. begin
  692. { for arrays try to get the element size, take care of
  693. multiple indexes }
  694. harrdef:=tarraydef(sym.vardef);
  695. { calc array size }
  696. if is_special_array(harrdef) then
  697. l := -1
  698. else
  699. l := harrdef.size;
  700. case opr.typ of
  701. OPR_REFERENCE: opr.varsize := l;
  702. OPR_LOCAL: opr.localvarsize := l;
  703. end;
  704. while assigned(harrdef.elementdef) and
  705. (harrdef.elementdef.typ=arraydef) do
  706. harrdef:=tarraydef(harrdef.elementdef);
  707. if not is_packed_array(harrdef) then
  708. SetSize(harrdef.elesize,false)
  709. else
  710. if (harrdef.elepackedbitsize mod 8) = 0 then
  711. SetSize(harrdef.elepackedbitsize div 8,false);
  712. end;
  713. recorddef:
  714. case opr.typ of
  715. OPR_REFERENCE: opr.varsize := sym.getsize;
  716. OPR_LOCAL: opr.localvarsize := sym.getsize;
  717. end;
  718. end;
  719. end;
  720. { search and sets up the correct fields in the Instr record }
  721. { for the NON-constant identifier passed to the routine. }
  722. { if not found returns FALSE. }
  723. var
  724. sym : tsym;
  725. srsymtable : TSymtable;
  726. {$ifdef x86}
  727. segreg,
  728. {$endif x86}
  729. indexreg : tregister;
  730. plist : ppropaccesslistitem;
  731. size_set_from_absolute : boolean = false;
  732. { offset fixup (in bytes), coming from an absolute declaration with an index
  733. (e.g. var tralala: word absolute moo[5]; ) }
  734. absoffset: asizeint=0;
  735. harrdef: tarraydef;
  736. tmpprocinfo: tprocinfo;
  737. Begin
  738. SetupVar:=false;
  739. asmsearchsym(s,sym,srsymtable);
  740. if sym = nil then
  741. exit;
  742. if sym.typ=absolutevarsym then
  743. begin
  744. case tabsolutevarsym(sym).abstyp of
  745. tovar:
  746. begin
  747. { Only support simple loads }
  748. plist:=tabsolutevarsym(sym).ref.firstsym;
  749. if assigned(plist) and
  750. (plist^.sltype=sl_load) then
  751. begin
  752. setvarsize(tabstractvarsym(sym));
  753. size_set_from_absolute:=true;
  754. sym:=plist^.sym;
  755. { resolve the chain of array indexes (if there are any) }
  756. harrdef:=nil;
  757. while assigned(plist^.next) do
  758. begin
  759. plist:=plist^.next;
  760. if (plist^.sltype=sl_vec) and (tabstractvarsym(sym).vardef.typ=arraydef) then
  761. begin
  762. if harrdef=nil then
  763. harrdef:=tarraydef(tabstractvarsym(sym).vardef)
  764. else if harrdef.elementdef.typ=arraydef then
  765. harrdef:=tarraydef(harrdef.elementdef)
  766. else
  767. begin
  768. Message(asmr_e_unsupported_symbol_type);
  769. exit;
  770. end;
  771. if is_special_array(harrdef) then
  772. begin
  773. Message(asmr_e_unsupported_symbol_type);
  774. exit;
  775. end;
  776. if not is_packed_array(harrdef) then
  777. Inc(absoffset,asizeint(Int64(plist^.value-harrdef.lowrange))*harrdef.elesize)
  778. else if (Int64(plist^.value-harrdef.lowrange)*harrdef.elepackedbitsize mod 8)=0 then
  779. Inc(absoffset,asizeint(Int64(plist^.value-harrdef.lowrange)*harrdef.elepackedbitsize div 8))
  780. else
  781. Message(asmr_e_packed_element);
  782. end
  783. else
  784. begin
  785. Message(asmr_e_unsupported_symbol_type);
  786. exit;
  787. end;
  788. end;
  789. end
  790. else
  791. begin
  792. Message(asmr_e_unsupported_symbol_type);
  793. exit;
  794. end;
  795. end;
  796. toaddr:
  797. begin
  798. initref;
  799. opr.ref.offset:=tabsolutevarsym(sym).addroffset;
  800. setvarsize(tabstractvarsym(sym));
  801. size_set_from_absolute:=true;
  802. hasvar:=true;
  803. Result:=true;
  804. exit;
  805. end;
  806. else
  807. begin
  808. Message(asmr_e_unsupported_symbol_type);
  809. exit;
  810. end;
  811. end;
  812. end;
  813. case sym.typ of
  814. fieldvarsym :
  815. begin
  816. if not tabstractrecordsymtable(sym.owner).is_packed then
  817. setconst(absoffset+tfieldvarsym(sym).fieldoffset)
  818. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  819. setconst(absoffset+tfieldvarsym(sym).fieldoffset div 8)
  820. else
  821. Message(asmr_e_packed_element);
  822. if not size_set_from_absolute then
  823. setvarsize(tabstractvarsym(sym));
  824. hasvar:=true;
  825. SetupVar:=true;
  826. end;
  827. staticvarsym,
  828. localvarsym,
  829. paravarsym :
  830. begin
  831. { we always assume in asm statements that }
  832. { that the variable is valid. }
  833. tabstractvarsym(sym).varstate:=vs_readwritten;
  834. inc(tabstractvarsym(sym).refs);
  835. { variable can't be placed in a register }
  836. tabstractvarsym(sym).varregable:=vr_none;
  837. { and anything may happen with its address }
  838. tabstractvarsym(sym).addr_taken:=true;
  839. case sym.typ of
  840. staticvarsym :
  841. begin
  842. initref;
  843. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(sym).mangledname,AT_DATA);
  844. Inc(opr.ref.offset,absoffset);
  845. end;
  846. paravarsym,
  847. localvarsym :
  848. begin
  849. tmpprocinfo:=current_procinfo;
  850. while assigned(tmpprocinfo) do
  851. begin
  852. if (sym.owner=tmpprocinfo.procdef.localst) or
  853. (sym.owner=tmpprocinfo.procdef.parast) then
  854. begin
  855. tmpprocinfo.procdef.init_paraloc_info(calleeside);
  856. break;
  857. end;
  858. tmpprocinfo:=tmpprocinfo.parent;
  859. end;
  860. if opr.typ=OPR_REFERENCE then
  861. begin
  862. {$ifdef x86}
  863. segreg:=opr.ref.segment;
  864. {$endif x86}
  865. indexreg:=opr.ref.base;
  866. if opr.ref.index<>NR_NO then
  867. begin
  868. if indexreg=NR_NO then
  869. indexreg:=opr.ref.index
  870. else
  871. Message(asmr_e_multiple_index);
  872. end;
  873. end
  874. else
  875. begin
  876. {$ifdef x86}
  877. segreg:=NR_NO;
  878. {$endif x86}
  879. indexreg:=NR_NO;
  880. end;
  881. opr.typ:=OPR_LOCAL;
  882. if assigned(current_procinfo.parent) and
  883. not(po_inline in current_procinfo.procdef.procoptions) and
  884. (sym.owner<>current_procinfo.procdef.localst) and
  885. (sym.owner<>current_procinfo.procdef.parast) and
  886. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  887. symtable_has_localvarsyms(current_procinfo.procdef.localst) then
  888. message1(asmr_e_local_para_unreachable,s);
  889. opr.localsym:=tabstractnormalvarsym(sym);
  890. opr.localsymofs:=absoffset;
  891. {$ifdef x86}
  892. opr.localsegment:=segreg;
  893. {$endif x86}
  894. opr.localindexreg:=indexreg;
  895. opr.localscale:=0;
  896. opr.localgetoffset:=GetOffset;
  897. if paramanager.push_addr_param(tabstractvarsym(sym).varspez,tabstractvarsym(sym).vardef,current_procinfo.procdef.proccalloption) then
  898. SetSize(sizeof(pint),false);
  899. end;
  900. end;
  901. if not size_set_from_absolute then
  902. setvarsize(tabstractvarsym(sym));
  903. hasvar:=true;
  904. SetupVar:=true;
  905. Exit;
  906. end;
  907. constsym :
  908. begin
  909. if tconstsym(sym).consttyp=constord then
  910. begin
  911. setconst(tconstsym(sym).value.valueord.svalue);
  912. SetupVar:=true;
  913. Exit;
  914. end;
  915. end;
  916. typesym :
  917. begin
  918. if ttypesym(sym).typedef.typ in [recorddef,objectdef] then
  919. begin
  920. setconst(0);
  921. SetupVar:=TRUE;
  922. Exit;
  923. end;
  924. end;
  925. procsym :
  926. begin
  927. if Tprocsym(sym).ProcdefList.Count>1 then
  928. Message(asmr_w_calling_overload_func);
  929. case opr.typ of
  930. OPR_REFERENCE:
  931. begin
  932. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname,AT_FUNCTION);
  933. Inc(opr.ref.offset,absoffset);
  934. {$ifdef i8086}
  935. opr.ref_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  936. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  937. {$endif i8086}
  938. end;
  939. OPR_NONE:
  940. begin
  941. opr.typ:=OPR_SYMBOL;
  942. opr.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname,AT_FUNCTION);
  943. {$ifdef i8086}
  944. opr.sym_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  945. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  946. {$endif i8086}
  947. opr.symofs:=absoffset;
  948. end;
  949. else
  950. Message(asmr_e_invalid_operand_type);
  951. end;
  952. hasproc:=true;
  953. hasvar:=true;
  954. SetupVar:=TRUE;
  955. Exit;
  956. end;
  957. {$ifdef i8086}
  958. labelsym :
  959. begin
  960. case opr.typ of
  961. OPR_REFERENCE:
  962. begin
  963. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tlabelsym(sym).mangledname,AT_FUNCTION);
  964. Inc(opr.ref.offset,absoffset);
  965. if opr.ref.segment=NR_NO then
  966. opr.ref.segment:=NR_CS;
  967. end;
  968. else
  969. begin
  970. Message(asmr_e_unsupported_symbol_type);
  971. exit;
  972. end;
  973. end;
  974. haslabelref:=true;
  975. hasvar:=true;
  976. SetupVar:=TRUE;
  977. Exit;
  978. end
  979. {$endif i8086}
  980. else
  981. begin
  982. Message(asmr_e_unsupported_symbol_type);
  983. exit;
  984. end;
  985. end;
  986. end;
  987. procedure TOperand.InitRef;
  988. {*********************************************************************}
  989. { Description: This routine first check if the opcode is of }
  990. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  991. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  992. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  993. { to point to the default segment. }
  994. {*********************************************************************}
  995. var
  996. l : aint;
  997. hsymofs : aint;
  998. hsymbol : tasmsymbol;
  999. reg : tregister;
  1000. hsym_farprocentry: Boolean;
  1001. Begin
  1002. case opr.typ of
  1003. OPR_REFERENCE :
  1004. exit;
  1005. OPR_CONSTANT :
  1006. begin
  1007. l:=opr.val;
  1008. opr.typ:=OPR_REFERENCE;
  1009. Fillchar(opr.ref,sizeof(treference),0);
  1010. opr.Ref.Offset:=l;
  1011. opr.varsize:=0;
  1012. opr.constoffset:=0;
  1013. opr.ref_farproc_entry:=false;
  1014. end;
  1015. OPR_NONE :
  1016. begin
  1017. opr.typ:=OPR_REFERENCE;
  1018. opr.varsize:=0;
  1019. opr.constoffset:=0;
  1020. opr.ref_farproc_entry:=false;
  1021. Fillchar(opr.ref,sizeof(treference),0);
  1022. end;
  1023. OPR_REGISTER :
  1024. begin
  1025. reg:=opr.reg;
  1026. opr.typ:=OPR_REFERENCE;
  1027. opr.varsize:=0;
  1028. opr.constoffset:=0;
  1029. opr.ref_farproc_entry:=false;
  1030. Fillchar(opr.ref,sizeof(treference),0);
  1031. opr.Ref.base:=reg;
  1032. end;
  1033. OPR_SYMBOL :
  1034. begin
  1035. hsymbol:=opr.symbol;
  1036. hsymofs:=opr.symofs;
  1037. hsym_farprocentry:=opr.sym_farproc_entry;
  1038. opr.typ:=OPR_REFERENCE;
  1039. opr.varsize:=0;
  1040. opr.constoffset:=0;
  1041. Fillchar(opr.ref,sizeof(treference),0);
  1042. opr.ref.symbol:=hsymbol;
  1043. opr.ref.offset:=hsymofs;
  1044. opr.ref_farproc_entry:=hsym_farprocentry;
  1045. end;
  1046. else
  1047. InitRefError;
  1048. end;
  1049. end;
  1050. procedure TOperand.InitRefConvertLocal;
  1051. var
  1052. localvarsize,localconstoffset: asizeint;
  1053. localsym:tabstractnormalvarsym;
  1054. localsymofs:aint;
  1055. {$ifdef x86}
  1056. localsegment,
  1057. {$endif x86}
  1058. localindexreg:tregister;
  1059. localscale:byte;
  1060. begin
  1061. if opr.typ=OPR_LOCAL then
  1062. begin
  1063. if AsmRegisterPara(opr.localsym) and
  1064. not opr.localgetoffset then
  1065. begin
  1066. localvarsize:=opr.localvarsize;
  1067. localconstoffset:=opr.localconstoffset;
  1068. localsym:=opr.localsym;
  1069. localsymofs:=opr.localsymofs;
  1070. {$ifdef x86}
  1071. localsegment:=opr.localsegment;
  1072. {$endif x86}
  1073. localindexreg:=opr.localindexreg;
  1074. localscale:=opr.localscale;;
  1075. opr.typ:=OPR_REFERENCE;
  1076. hasvar:=false;
  1077. Fillchar(opr.ref,sizeof(treference),0);
  1078. opr.varsize:=localvarsize;
  1079. opr.constoffset:=localconstoffset;
  1080. opr.ref_farproc_entry:=false;
  1081. opr.ref.base:=tparavarsym(localsym).paraloc[calleeside].Location^.register;
  1082. opr.ref.offset:=localsymofs;
  1083. {$ifdef x86}
  1084. opr.ref.segment:=localsegment;
  1085. {$endif x86}
  1086. opr.ref.index:=localindexreg;
  1087. opr.ref.scalefactor:=localscale;
  1088. end
  1089. else
  1090. InitRefError;
  1091. end
  1092. else
  1093. InitRef;
  1094. end;
  1095. procedure TOperand.InitRefError;
  1096. begin
  1097. Message(asmr_e_invalid_operand_type);
  1098. { Recover }
  1099. opr.typ:=OPR_REFERENCE;
  1100. opr.varsize:=0;
  1101. opr.constoffset:=0;
  1102. opr.ref_farproc_entry:=false;
  1103. Fillchar(opr.ref,sizeof(treference),0);
  1104. end;
  1105. Function TOperand.CheckOperand: boolean;
  1106. {*********************************************************************}
  1107. { Description: This routine checks if the operand is of }
  1108. { valid, and returns false if it isn't. Does nothing by default. }
  1109. {*********************************************************************}
  1110. begin
  1111. result:=true;
  1112. end;
  1113. {****************************************************************************
  1114. TInstruction
  1115. ****************************************************************************}
  1116. constructor TInstruction.create(optype : tcoperand);
  1117. var
  1118. i : longint;
  1119. Begin
  1120. { these field are set to 0 anyways by the constructor helper (FK)
  1121. Opcode:=A_NONE;
  1122. Condition:=C_NONE;
  1123. Ops:=0;
  1124. }
  1125. filepos:=current_filepos;
  1126. for i:=1 to max_operands do
  1127. Operands[i]:=optype.create;
  1128. Labeled:=false;
  1129. end;
  1130. destructor TInstruction.destroy;
  1131. var
  1132. i : longint;
  1133. Begin
  1134. for i:=1 to max_operands do
  1135. Operands[i].free;
  1136. end;
  1137. function TInstruction.ConcatInstruction(p:TAsmList) : tai;
  1138. var
  1139. ai : taicpu;
  1140. i : longint;
  1141. begin
  1142. for i:=1 to Ops do
  1143. operands[i].CheckOperand;
  1144. ai:=taicpu.op_none(opcode);
  1145. ai.fileinfo:=filepos;
  1146. ai.Ops:=Ops;
  1147. ai.Allocate_oper(Ops);
  1148. for i:=1 to Ops do
  1149. with operands[i].opr do
  1150. begin
  1151. case typ of
  1152. OPR_CONSTANT :
  1153. ai.loadconst(i-1,val);
  1154. OPR_REGISTER:
  1155. ai.loadreg(i-1,reg);
  1156. OPR_SYMBOL:
  1157. ai.loadsymbol(i-1,symbol,symofs);
  1158. OPR_LOCAL :
  1159. begin
  1160. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1161. localscale,localgetoffset,localforceref);
  1162. {$ifdef x86}
  1163. ai.oper[i-1]^.localoper^.localsegment:=localsegment;
  1164. {$endif x86}
  1165. end;
  1166. OPR_REFERENCE:
  1167. ai.loadref(i-1,ref);
  1168. {$ifdef m68k}
  1169. OPR_REGSET:
  1170. ai.loadregset(i-1,regsetdata,regsetaddr,regsetfpu);
  1171. OPR_REGPAIR:
  1172. ai.loadregpair(i-1,reghi,reglo);
  1173. {$endif}
  1174. {$ifdef ARM}
  1175. OPR_REGSET:
  1176. ai.loadregset(i-1,regtype,subreg,regset,usermode);
  1177. OPR_MODEFLAGS:
  1178. ai.loadmodeflags(i-1,flags);
  1179. OPR_SPECIALREG:
  1180. ai.loadspecialreg(i-1,specialreg,specialregflags);
  1181. {$endif ARM}
  1182. {$if defined(arm) or defined(aarch64)}
  1183. OPR_SHIFTEROP:
  1184. ai.loadshifterop(i-1,shifterop);
  1185. OPR_COND:
  1186. ai.loadconditioncode(i-1,cc);
  1187. {$endif arm or aarch64}
  1188. { ignore wrong operand }
  1189. OPR_NONE:
  1190. ;
  1191. else
  1192. internalerror(200501051);
  1193. end;
  1194. end;
  1195. ai.SetCondition(condition);
  1196. { Concat the opcode or give an error }
  1197. if assigned(ai) then
  1198. p.concat(ai)
  1199. else
  1200. Message(asmr_e_invalid_opcode_and_operand);
  1201. result:=ai;
  1202. end;
  1203. {****************************************************************************
  1204. Symbol table helper routines
  1205. ****************************************************************************}
  1206. procedure AsmSearchSym(const s:string;out srsym:tsym;out srsymtable:TSymtable);
  1207. var
  1208. i : integer;
  1209. begin
  1210. i:=pos('.',s);
  1211. { allow unit.identifier }
  1212. if i>0 then
  1213. begin
  1214. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1215. if assigned(srsym) then
  1216. begin
  1217. if (srsym.typ=unitsym) and
  1218. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  1219. srsym.owner.iscurrentunit then
  1220. searchsym_in_module(tunitsym(srsym).module,Copy(s,i+1,255),srsym,srsymtable)
  1221. else
  1222. begin
  1223. srsym:=nil;
  1224. srsymtable:=nil;
  1225. end;
  1226. end;
  1227. end
  1228. else
  1229. searchsym(s,srsym,srsymtable);
  1230. { in asm routines, the function result variable, that matches the function
  1231. name should be avoided, because:
  1232. 1) there's already a @Result directive (even in TP7) that can be used, if
  1233. you want to access the function result
  1234. 2) there's no other way to disambiguate between the function result variable
  1235. and the function's address (using asm syntax only)
  1236. This fixes code, such as:
  1237. function test1: word;
  1238. begin
  1239. asm
  1240. mov ax, offset test1
  1241. end;
  1242. end;
  1243. and makes it work in a consistent manner as this code:
  1244. procedure test2;
  1245. begin
  1246. asm
  1247. mov ax, offset test2
  1248. end;
  1249. end; }
  1250. if assigned(srsym) and
  1251. assigned(srsymtable) and
  1252. (srsym.typ=absolutevarsym) and
  1253. (vo_is_funcret in tabsolutevarsym(srsym).varoptions) and
  1254. (srsymtable.symtabletype=localsymtable) and
  1255. assigned(srsymtable.defowner) and
  1256. (srsymtable.defowner.typ=procdef) and
  1257. (tprocdef(srsymtable.defowner).procsym.name=tabsolutevarsym(srsym).Name) then
  1258. begin
  1259. srsym:=tprocdef(srsymtable.defowner).procsym;
  1260. srsymtable:=srsym.Owner;
  1261. end;
  1262. end;
  1263. Function SearchType(const hs:string;out size:tcgint): Boolean;
  1264. var
  1265. srsym : tsym;
  1266. srsymtable : TSymtable;
  1267. begin
  1268. result:=false;
  1269. size:=0;
  1270. asmsearchsym(hs,srsym,srsymtable);
  1271. if assigned(srsym) and
  1272. (srsym.typ=typesym) then
  1273. begin
  1274. size:=ttypesym(srsym).typedef.size;
  1275. result:=true;
  1276. end;
  1277. end;
  1278. Function SearchRecordType(const s:string): boolean;
  1279. var
  1280. srsym : tsym;
  1281. srsymtable : TSymtable;
  1282. Begin
  1283. SearchRecordType:=false;
  1284. { Check the constants in symtable }
  1285. asmsearchsym(s,srsym,srsymtable);
  1286. if srsym <> nil then
  1287. Begin
  1288. case srsym.typ of
  1289. typesym :
  1290. begin
  1291. if ttypesym(srsym).typedef.typ in [recorddef,objectdef] then
  1292. begin
  1293. SearchRecordType:=true;
  1294. exit;
  1295. end;
  1296. end;
  1297. fieldvarsym :
  1298. begin
  1299. if (tfieldvarsym(srsym).vardef.typ in [recorddef,objectdef]) then
  1300. begin
  1301. SearchRecordType:=true;
  1302. exit;
  1303. end;
  1304. end;
  1305. end;
  1306. end;
  1307. end;
  1308. Function SearchIConstant(const s:string; var l:tcgint): boolean;
  1309. {**********************************************************************}
  1310. { Description: Searches for a CONSTANT of name s in either the local }
  1311. { symbol list, then in the global symbol list, and returns the value }
  1312. { of that constant in l. Returns TRUE if successfull, if not found, }
  1313. { or if the constant is not of correct type, then returns FALSE }
  1314. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1315. { respectively. }
  1316. {**********************************************************************}
  1317. var
  1318. srsym : tsym;
  1319. srsymtable : TSymtable;
  1320. Begin
  1321. SearchIConstant:=false;
  1322. { check for TRUE or FALSE reserved words first }
  1323. if s = 'TRUE' then
  1324. Begin
  1325. SearchIConstant:=TRUE;
  1326. l:=1;
  1327. exit;
  1328. end;
  1329. if s = 'FALSE' then
  1330. Begin
  1331. SearchIConstant:=TRUE;
  1332. l:=0;
  1333. exit;
  1334. end;
  1335. { Check the constants in symtable }
  1336. asmsearchsym(s,srsym,srsymtable);
  1337. if srsym <> nil then
  1338. Begin
  1339. case srsym.typ of
  1340. constsym :
  1341. begin
  1342. if tconstsym(srsym).consttyp=constord then
  1343. Begin
  1344. l:=tconstsym(srsym).value.valueord.svalue;
  1345. SearchIConstant:=TRUE;
  1346. exit;
  1347. end;
  1348. end;
  1349. enumsym:
  1350. Begin
  1351. l:=tenumsym(srsym).value;
  1352. SearchIConstant:=TRUE;
  1353. exit;
  1354. end;
  1355. end;
  1356. end;
  1357. end;
  1358. function AsmRegisterPara(sym: tabstractnormalvarsym): boolean;
  1359. begin
  1360. result:=
  1361. (po_assembler in current_procinfo.procdef.procoptions) and
  1362. (sym.typ=paravarsym) and
  1363. (tparavarsym(sym).paraloc[calleeside].Location^.Loc=LOC_REGISTER);
  1364. end;
  1365. Function GetRecordOffsetSize(s:string;out Offset: tcgint;out Size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean):boolean;
  1366. { search and returns the offset and size of records/objects of the base }
  1367. { with field name setup in field. }
  1368. { returns FALSE if not found. }
  1369. { used when base is a variable or a typed constant name. }
  1370. var
  1371. st : TSymtable;
  1372. harrdef : tarraydef;
  1373. sym : tsym;
  1374. srsymtable : TSymtable;
  1375. i : longint;
  1376. base : string;
  1377. procdef: tprocdef;
  1378. Begin
  1379. GetRecordOffsetSize:=FALSE;
  1380. Offset:=0;
  1381. Size:=0;
  1382. mangledname:='';
  1383. hastypecast:=false;
  1384. i:=pos('.',s);
  1385. if i=0 then
  1386. i:=255;
  1387. base:=Copy(s,1,i-1);
  1388. delete(s,1,i);
  1389. if base='SELF' then
  1390. st:=current_structdef.symtable
  1391. else
  1392. begin
  1393. asmsearchsym(base,sym,srsymtable);
  1394. st:=nil;
  1395. { we can start with a var,type,typedconst }
  1396. if assigned(sym) then
  1397. case sym.typ of
  1398. staticvarsym,
  1399. localvarsym,
  1400. paravarsym :
  1401. st:=Tabstractvarsym(sym).vardef.GetSymtable(gs_record);
  1402. typesym :
  1403. st:=Ttypesym(sym).typedef.GetSymtable(gs_record);
  1404. end
  1405. else
  1406. s:='';
  1407. end;
  1408. { now walk all recordsymtables }
  1409. while assigned(st) and (s<>'') do
  1410. begin
  1411. { load next field in base }
  1412. i:=pos('.',s);
  1413. if i=0 then
  1414. i:=255;
  1415. base:=Copy(s,1,i-1);
  1416. delete(s,1,i);
  1417. sym:=search_struct_member(tabstractrecorddef(st.defowner),base);
  1418. if not assigned(sym) then
  1419. begin
  1420. GetRecordOffsetSize:=false;
  1421. exit;
  1422. end;
  1423. st:=nil;
  1424. case sym.typ of
  1425. fieldvarsym :
  1426. with Tfieldvarsym(sym) do
  1427. begin
  1428. if not tabstractrecordsymtable(sym.owner).is_packed then
  1429. inc(Offset,fieldoffset)
  1430. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  1431. inc(Offset,fieldoffset div 8)
  1432. else
  1433. Message(asmr_e_packed_element);
  1434. size:=getsize;
  1435. case vardef.typ of
  1436. arraydef :
  1437. begin
  1438. { for arrays try to get the element size, take care of
  1439. multiple indexes }
  1440. harrdef:=tarraydef(vardef);
  1441. while assigned(harrdef.elementdef) and
  1442. (harrdef.elementdef.typ=arraydef) do
  1443. harrdef:=tarraydef(harrdef.elementdef);
  1444. if not is_packed_array(harrdef) then
  1445. size:=harrdef.elesize
  1446. else
  1447. begin
  1448. if (harrdef.elepackedbitsize mod 8) <> 0 then
  1449. Message(asmr_e_packed_element);
  1450. size := (harrdef.elepackedbitsize + 7) div 8;
  1451. end;
  1452. end;
  1453. recorddef :
  1454. st:=trecorddef(vardef).symtable;
  1455. objectdef :
  1456. st:=tobjectdef(vardef).symtable;
  1457. end;
  1458. end;
  1459. procsym:
  1460. begin
  1461. st:=nil;
  1462. if Tprocsym(sym).ProcdefList.Count>1 then
  1463. Message(asmr_w_calling_overload_func);
  1464. procdef:=tprocdef(tprocsym(sym).ProcdefList[0]);
  1465. if (not needvmtofs) then
  1466. begin
  1467. mangledname:=procdef.mangledname;
  1468. end
  1469. else
  1470. begin
  1471. { can only get the vmtoffset of virtual methods }
  1472. if not(po_virtualmethod in procdef.procoptions) or
  1473. is_objectpascal_helper(procdef.struct) then
  1474. Message1(asmr_e_no_vmtoffset_possible,FullTypeName(procdef,nil))
  1475. else
  1476. begin
  1477. { size = sizeof(target_system_pointer) }
  1478. size:=sizeof(pint);
  1479. offset:=tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber)
  1480. end;
  1481. end;
  1482. { if something comes after the procsym, it's invalid assembler syntax }
  1483. GetRecordOffsetSize:=(s='');
  1484. exit;
  1485. end;
  1486. end;
  1487. end;
  1488. { Support Field.Type as typecasting }
  1489. if (st=nil) and (s<>'') then
  1490. begin
  1491. asmsearchsym(s,sym,srsymtable);
  1492. if assigned(sym) and (sym.typ=typesym) then
  1493. begin
  1494. size:=ttypesym(sym).typedef.size;
  1495. s:='';
  1496. hastypecast:=true;
  1497. end;
  1498. end;
  1499. GetRecordOffsetSize:=(s='');
  1500. end;
  1501. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1502. var
  1503. sym : tsym;
  1504. srsymtable : TSymtable;
  1505. hs : string;
  1506. Begin
  1507. hl:=nil;
  1508. SearchLabel:=false;
  1509. { Check for pascal labels, which are case insensetive }
  1510. hs:=upper(s);
  1511. asmsearchsym(hs,sym,srsymtable);
  1512. if sym=nil then
  1513. exit;
  1514. case sym.typ of
  1515. labelsym :
  1516. begin
  1517. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1518. begin
  1519. Tlabelsym(sym).nonlocal:=true;
  1520. if emit then
  1521. exclude(current_procinfo.procdef.procoptions,po_inline);
  1522. end;
  1523. if not(assigned(tlabelsym(sym).asmblocklabel)) then
  1524. if Tlabelsym(sym).nonlocal then
  1525. current_asmdata.getglobaljumplabel(tlabelsym(sym).asmblocklabel)
  1526. else
  1527. current_asmdata.getjumplabel(tlabelsym(sym).asmblocklabel);
  1528. hl:=tlabelsym(sym).asmblocklabel;
  1529. if emit then
  1530. begin
  1531. if tlabelsym(sym).defined then
  1532. Message(sym_e_label_already_defined);
  1533. tlabelsym(sym).defined:=true
  1534. end
  1535. else
  1536. tlabelsym(sym).used:=true;
  1537. SearchLabel:=true;
  1538. end;
  1539. end;
  1540. end;
  1541. {*************************************************************************}
  1542. { Instruction Generation Utilities }
  1543. {*************************************************************************}
  1544. Procedure ConcatString(p : TAsmList;s:string);
  1545. {*********************************************************************}
  1546. { PROCEDURE ConcatString(s:string); }
  1547. { Description: This routine adds the character chain pointed to in }
  1548. { s to the instruction linked list. }
  1549. {*********************************************************************}
  1550. Begin
  1551. p.concat(Tai_string.Create(s));
  1552. end;
  1553. Procedure ConcatConstant(p: TAsmList; value: tcgint; constsize:byte);
  1554. {*********************************************************************}
  1555. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1556. { Description: This routine adds the value constant to the current }
  1557. { instruction linked list. }
  1558. { maxvalue -> indicates the size of the data to initialize: }
  1559. { $ff -> create a byte node. }
  1560. { $ffff -> create a word node. }
  1561. { $ffffffff -> create a dword node. }
  1562. {*********************************************************************}
  1563. var
  1564. rangelo,rangehi : int64;
  1565. Begin
  1566. case constsize of
  1567. 1 :
  1568. begin
  1569. p.concat(Tai_const.Create_8bit(byte(value)));
  1570. rangelo:=low(shortint);
  1571. rangehi:=high(byte);
  1572. end;
  1573. 2 :
  1574. begin
  1575. p.concat(Tai_const.Create_16bit(word(value)));
  1576. rangelo:=low(smallint);
  1577. rangehi:=high(word);
  1578. end;
  1579. 4 :
  1580. begin
  1581. p.concat(Tai_const.Create_32bit(longint(value)));
  1582. rangelo:=low(longint);
  1583. rangehi:=high(cardinal);
  1584. end;
  1585. 8 :
  1586. begin
  1587. p.concat(Tai_const.Create_64bit(int64(value)));
  1588. rangelo:=0;
  1589. rangehi:=0;
  1590. end;
  1591. else
  1592. internalerror(200405011);
  1593. end;
  1594. { check for out of bounds }
  1595. if (rangelo<>0) and
  1596. ((value>rangehi) or (value<rangelo)) then
  1597. Message(asmr_e_constant_out_of_bounds);
  1598. end;
  1599. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:tcgint;constsize:byte;isofs:boolean);
  1600. begin
  1601. {$ifdef i8086}
  1602. { 'DW xx' as well as 'DW OFFSET xx' are just near pointers }
  1603. if constsize=2 then
  1604. p.concat(Tai_const.Createname_near(sym,l))
  1605. else if constsize=4 then
  1606. begin
  1607. if isofs then
  1608. begin
  1609. { 'DD OFFSET xx' is a 32-bit offset; since we don't produce 32-bit
  1610. relocations yet, just do a 16-bit one and set the high word to 0 }
  1611. p.concat(Tai_const.Createname_near(sym,l));
  1612. p.concat(Tai_const.Create_16bit(0));
  1613. end
  1614. else
  1615. { 'DD xx' is a far pointer }
  1616. p.concat(Tai_const.Createname_far(sym,l));
  1617. end
  1618. else
  1619. internalerror(2018020701);
  1620. {$else i8086}
  1621. p.concat(Tai_const.Createname(sym,l));
  1622. {$endif i8086}
  1623. end;
  1624. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  1625. {***********************************************************************}
  1626. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1627. { Description: This routine adds the value constant to the current }
  1628. { instruction linked list. }
  1629. { real_typ -> indicates the type of the real data to initialize: }
  1630. { s32real -> create a single node. }
  1631. { s64real -> create a double node. }
  1632. { s80real -> create an extended node. }
  1633. { s64bit -> create a comp node. }
  1634. { f32bit -> create a fixed node. (not used normally) }
  1635. {***********************************************************************}
  1636. Begin
  1637. case real_typ of
  1638. s32real : p.concat(tai_realconst.create_s32real(value));
  1639. s64real :
  1640. {$ifdef ARM}
  1641. if is_double_hilo_swapped then
  1642. p.concat(tai_realconst.create_s64real_hiloswapped(value))
  1643. else
  1644. {$endif ARM}
  1645. p.concat(tai_realconst.create_s64real(value));
  1646. s80real : p.concat(tai_realconst.create_s80real(value,s80floattype.size));
  1647. sc80real : p.concat(tai_realconst.create_s80real(value,sc80floattype.size));
  1648. s64comp : p.concat(tai_realconst.create_s64compreal(trunc(value)));
  1649. else
  1650. internalerror(2014050608);
  1651. end;
  1652. end;
  1653. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  1654. {*********************************************************************}
  1655. { PROCEDURE ConcatLabel }
  1656. { Description: This routine either emits a label or a labeled }
  1657. { instruction to the linked list of instructions. }
  1658. {*********************************************************************}
  1659. begin
  1660. p.concat(Tai_label.Create(l));
  1661. end;
  1662. procedure ConcatAlign(p:TAsmList;l:tcgint);
  1663. {*********************************************************************}
  1664. { PROCEDURE ConcatPublic }
  1665. { Description: This routine emits an global definition to the }
  1666. { linked list of instructions.(used by AT&T styled asm) }
  1667. {*********************************************************************}
  1668. begin
  1669. p.concat(Tai_align.Create(l));
  1670. end;
  1671. procedure ConcatPublic(p:TAsmList;const s : string);
  1672. {*********************************************************************}
  1673. { PROCEDURE ConcatPublic }
  1674. { Description: This routine emits an global definition to the }
  1675. { linked list of instructions.(used by AT&T styled asm) }
  1676. {*********************************************************************}
  1677. begin
  1678. p.concat(Tai_symbol.Createname_global(s,AT_LABEL,0,voidcodepointertype));
  1679. end;
  1680. procedure ConcatLocal(p:TAsmList;const s : string);
  1681. {*********************************************************************}
  1682. { PROCEDURE ConcatLocal }
  1683. { Description: This routine emits an local definition to the }
  1684. { linked list of instructions. }
  1685. {*********************************************************************}
  1686. begin
  1687. p.concat(Tai_symbol.Createname(s,AT_LABEL,0,voidcodepointertype));
  1688. end;
  1689. end.