rautils.pas 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  4. This unit implements some support routines for assembler parsing
  5. independent of the processor
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. **********************************************************************}
  18. Unit RAUtils;
  19. {$i fpcdefs.inc}
  20. Interface
  21. Uses
  22. cutils,cclasses,
  23. globtype,aasmbase,aasmtai,cpubase,cpuinfo,cgbase,
  24. symconst,symbase,symtype,symdef,symsym;
  25. Const
  26. RPNMax = 10; { I think you only need 4, but just to be safe }
  27. OpMax = 25;
  28. {---------------------------------------------------------------------
  29. Local Label Management
  30. ---------------------------------------------------------------------}
  31. Type
  32. { Each local label has this structure associated with it }
  33. TLocalLabel = class(TNamedIndexItem)
  34. Emitted : boolean;
  35. constructor Create(const n:string);
  36. function Gettasmlabel:tasmlabel;
  37. private
  38. lab : tasmlabel;
  39. end;
  40. TLocalLabelList = class(TDictionary)
  41. procedure CheckEmitted;
  42. end;
  43. var
  44. LocalLabelList : TLocalLabelList;
  45. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  46. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  47. {---------------------------------------------------------------------
  48. Instruction management
  49. ---------------------------------------------------------------------}
  50. type
  51. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_LOCAL,
  52. OPR_REFERENCE,OPR_REGISTER,OPR_REGLIST,OPR_COND,OPR_REGSET);
  53. TOprRec = record
  54. case typ:TOprType of
  55. OPR_NONE : ();
  56. OPR_CONSTANT : (val:aint);
  57. OPR_SYMBOL : (symbol:tasmsymbol;symofs:aint);
  58. OPR_REFERENCE : (ref:treference);
  59. OPR_LOCAL : (localsym:tvarsym;localsymofs:aint;localindexreg:tregister;localscale:byte;localgetoffset:boolean);
  60. OPR_REGISTER : (reg:tregister);
  61. {$ifdef m68k}
  62. OPR_REGLIST : (regset : tcpuregisterset);
  63. {$endif m68k}
  64. {$ifdef powerpc}
  65. OPR_COND : (cond : tasmcond);
  66. {$endif powerpc}
  67. {$ifdef arm}
  68. OPR_REGSET : (regset : tcpuregisterset);
  69. {$endif arm}
  70. end;
  71. TOperand = class
  72. hastype, { if the operand has typecasted variable }
  73. hasvar : boolean; { if the operand is loaded with a variable }
  74. size : TCGSize;
  75. opr : TOprRec;
  76. constructor create;virtual;
  77. destructor destroy;override;
  78. Procedure SetSize(_size:longint;force:boolean);virtual;
  79. Procedure SetCorrectSize(opcode:tasmop);virtual;
  80. Function SetupResult:boolean;virtual;
  81. Function SetupSelf:boolean;
  82. Function SetupOldEBP:boolean;
  83. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  84. Function SetupDirectVar(const hs:string): Boolean;
  85. Procedure InitRef;
  86. end;
  87. TCOperand = class of TOperand;
  88. TInstruction = class
  89. opcode : tasmop;
  90. condition : tasmcond;
  91. ops : byte;
  92. labeled : boolean;
  93. operands : array[1..max_operands] of toperand;
  94. constructor create(optype : tcoperand);virtual;
  95. destructor destroy;override;
  96. Procedure BuildOpcode;virtual;abstract;
  97. { converts the instruction to an instruction how it's used by the assembler writer
  98. and concats it to the passed list, the newly created item is returned }
  99. function ConcatInstruction(p:TAAsmoutput) : tai;virtual;
  100. Procedure Swapoperands;
  101. end;
  102. tstr2opentry = class(Tnamedindexitem)
  103. op: TAsmOp;
  104. end;
  105. {---------------------------------------------------------------------}
  106. { Expression parser types }
  107. {---------------------------------------------------------------------}
  108. TExprOperator = record
  109. ch: char; { operator }
  110. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  111. end;
  112. {**********************************************************************}
  113. { The following operators are supported: }
  114. { '+' : addition }
  115. { '-' : subtraction }
  116. { '*' : multiplication }
  117. { '/' : modulo division }
  118. { '^' : exclusive or }
  119. { '<' : shift left }
  120. { '>' : shift right }
  121. { '&' : bitwise and }
  122. { '|' : bitwise or }
  123. { '~' : bitwise complement }
  124. { '%' : modulo division }
  125. { nnn: longint numbers }
  126. { ( and ) parenthesis }
  127. {**********************************************************************}
  128. TExprParse = class
  129. public
  130. Constructor create;
  131. Destructor Destroy;override;
  132. Function Evaluate(Expr: String): aint;
  133. Function Priority(_Operator: Char): aint;
  134. private
  135. RPNStack : Array[1..RPNMax] of aint; { Stack For RPN calculator }
  136. RPNTop : aint;
  137. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  138. OpTop : aint;
  139. Procedure RPNPush(Num: aint);
  140. Function RPNPop: aint;
  141. Procedure RPNCalc(const token: String; prefix: boolean);
  142. Procedure OpPush(_Operator: char; prefix: boolean);
  143. { In reality returns TExprOperaotr }
  144. Procedure OpPop(var _Operator:TExprOperator);
  145. end;
  146. { Evaluate an expression string to a aint }
  147. Function CalculateExpression(const expression: string): aint;
  148. {---------------------------------------------------------------------}
  149. { String routines }
  150. {---------------------------------------------------------------------}
  151. Function ValDecimal(const S:String):aint;
  152. Function ValOctal(const S:String):aint;
  153. Function ValBinary(const S:String):aint;
  154. Function ValHexaDecimal(const S:String):aint;
  155. Function PadZero(Var s: String; n: byte): Boolean;
  156. Function EscapeToPascal(const s:string): string;
  157. {---------------------------------------------------------------------
  158. Symbol helper routines
  159. ---------------------------------------------------------------------}
  160. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  161. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint):boolean;
  162. Function SearchType(const hs:string;var size:aint): Boolean;
  163. Function SearchRecordType(const s:string): boolean;
  164. Function SearchIConstant(const s:string; var l:aint): boolean;
  165. {---------------------------------------------------------------------
  166. Instruction generation routines
  167. ---------------------------------------------------------------------}
  168. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  169. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  170. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  171. Procedure ConcatConstant(p : TAAsmoutput;value: aint; constsize:byte);
  172. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:aint);
  173. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  174. Procedure ConcatString(p : TAAsmoutput;s:string);
  175. procedure ConcatAlign(p:TAAsmoutput;l:aint);
  176. Procedure ConcatPublic(p:TAAsmoutput;const s : string);
  177. Procedure ConcatLocal(p:TAAsmoutput;const s : string);
  178. Procedure ConcatGlobalBss(const s : string;size : aint);
  179. Procedure ConcatLocalBss(const s : string;size : aint);
  180. Implementation
  181. uses
  182. {$ifdef delphi}
  183. sysutils,
  184. {$else}
  185. strings,
  186. {$endif}
  187. defutil,systems,verbose,globals,
  188. symtable,paramgr,
  189. aasmcpu,
  190. procinfo;
  191. {*************************************************************************
  192. TExprParse
  193. *************************************************************************}
  194. Constructor TExprParse.create;
  195. Begin
  196. end;
  197. Procedure TExprParse.RPNPush(Num : aint);
  198. { Add an operand to the top of the RPN stack }
  199. begin
  200. if RPNTop < RPNMax then
  201. begin
  202. Inc(RPNTop);
  203. RPNStack[RPNTop]:=Num;
  204. end
  205. else
  206. Message(asmr_e_expr_illegal);
  207. end;
  208. Function TExprParse.RPNPop : aint;
  209. { Get the operand at the top of the RPN stack }
  210. begin
  211. if RPNTop > 0 then
  212. begin
  213. RPNPop:=RPNStack[RPNTop];
  214. Dec(RPNTop);
  215. end
  216. else
  217. Message(asmr_e_expr_illegal);
  218. end;
  219. Procedure TExprParse.RPNCalc(const Token : String; prefix:boolean); { RPN Calculator }
  220. Var
  221. Temp : aint;
  222. n1,n2 : aint;
  223. LocalError : Integer;
  224. begin
  225. { Handle operators }
  226. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  227. Case Token[1] of
  228. '+' :
  229. Begin
  230. if not prefix then
  231. RPNPush(RPNPop + RPNPop);
  232. end;
  233. '-' :
  234. Begin
  235. if prefix then
  236. RPNPush(-(RPNPop))
  237. else
  238. begin
  239. n1:=RPNPop;
  240. n2:=RPNPop;
  241. RPNPush(n2 - n1);
  242. end;
  243. end;
  244. '*' : RPNPush(RPNPop * RPNPop);
  245. '&' :
  246. begin
  247. n1:=RPNPop;
  248. n2:=RPNPop;
  249. RPNPush(n2 and n1);
  250. end;
  251. '|' :
  252. begin
  253. n1:=RPNPop;
  254. n2:=RPNPop;
  255. RPNPush(n2 or n1);
  256. end;
  257. '~' : RPNPush(NOT RPNPop);
  258. '<' :
  259. begin
  260. n1:=RPNPop;
  261. n2:=RPNPop;
  262. RPNPush(n2 SHL n1);
  263. end;
  264. '>' :
  265. begin
  266. n1:=RPNPop;
  267. n2:=RPNPop;
  268. RPNPush(n2 SHR n1);
  269. end;
  270. '%' :
  271. begin
  272. Temp:=RPNPop;
  273. if Temp <> 0 then
  274. RPNPush(RPNPop mod Temp)
  275. else
  276. begin
  277. Message(asmr_e_expr_zero_divide);
  278. { push 1 for error recovery }
  279. RPNPush(1);
  280. end;
  281. end;
  282. '^' : RPNPush(RPNPop XOR RPNPop);
  283. '/' :
  284. begin
  285. Temp:=RPNPop;
  286. if Temp <> 0 then
  287. RPNPush(RPNPop div Temp)
  288. else
  289. begin
  290. Message(asmr_e_expr_zero_divide);
  291. { push 1 for error recovery }
  292. RPNPush(1);
  293. end;
  294. end;
  295. end
  296. else
  297. begin
  298. { Convert String to number and add to stack }
  299. Val(Token, Temp, LocalError);
  300. if LocalError = 0 then
  301. RPNPush(Temp)
  302. else
  303. begin
  304. Message(asmr_e_expr_illegal);
  305. { push 1 for error recovery }
  306. RPNPush(1);
  307. end;
  308. end;
  309. end;
  310. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  311. { Add an operator onto top of the stack }
  312. begin
  313. if OpTop < OpMax then
  314. begin
  315. Inc(OpTop);
  316. OpStack[OpTop].ch:=_Operator;
  317. OpStack[OpTop].is_prefix:=prefix;
  318. end
  319. else
  320. Message(asmr_e_expr_illegal);
  321. end;
  322. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  323. { Get operator at the top of the stack }
  324. begin
  325. if OpTop > 0 then
  326. begin
  327. _Operator:=OpStack[OpTop];
  328. Dec(OpTop);
  329. end
  330. else
  331. Message(asmr_e_expr_illegal);
  332. end;
  333. Function TExprParse.Priority(_Operator : Char) : aint;
  334. { Return priority of operator }
  335. { The greater the priority, the higher the precedence }
  336. begin
  337. Case _Operator OF
  338. '(' :
  339. Priority:=0;
  340. '+', '-' :
  341. Priority:=1;
  342. '*', '/','%','<','>' :
  343. Priority:=2;
  344. '|','&','^','~' :
  345. Priority:=0;
  346. else
  347. Message(asmr_e_expr_illegal);
  348. end;
  349. end;
  350. Function TExprParse.Evaluate(Expr : String):aint;
  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 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): aint;
  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 : aint;
  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(ValOctal(temp));
  490. end;
  491. 'x':
  492. Begin
  493. temp:=s[i+1];
  494. temp:=temp+s[i+2];
  495. inc(i,2);
  496. c:=chr(ValHexaDecimal(temp));
  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 ValDecimal(const S:String):aint;
  514. { Converts a decimal string to aint }
  515. var
  516. vs : aint;
  517. c : longint;
  518. Begin
  519. vs:=0;
  520. for c:=1 to length(s) do
  521. begin
  522. vs:=vs*10;
  523. if s[c] in ['0'..'9'] then
  524. inc(vs,ord(s[c])-ord('0'))
  525. else
  526. begin
  527. Message1(asmr_e_error_converting_decimal,s);
  528. ValDecimal:=0;
  529. exit;
  530. end;
  531. end;
  532. ValDecimal:=vs;
  533. end;
  534. Function ValOctal(const S:String):aint;
  535. { Converts an octal string to aint }
  536. var
  537. vs : aint;
  538. c : longint;
  539. Begin
  540. vs:=0;
  541. for c:=1 to length(s) do
  542. begin
  543. vs:=vs shl 3;
  544. if s[c] in ['0'..'7'] then
  545. inc(vs,ord(s[c])-ord('0'))
  546. else
  547. begin
  548. Message1(asmr_e_error_converting_octal,s);
  549. ValOctal:=0;
  550. exit;
  551. end;
  552. end;
  553. ValOctal:=vs;
  554. end;
  555. Function ValBinary(const S:String):aint;
  556. { Converts a binary string to aint }
  557. var
  558. vs : aint;
  559. c : longint;
  560. Begin
  561. vs:=0;
  562. for c:=1 to length(s) do
  563. begin
  564. vs:=vs shl 1;
  565. if s[c] in ['0'..'1'] then
  566. inc(vs,ord(s[c])-ord('0'))
  567. else
  568. begin
  569. Message1(asmr_e_error_converting_binary,s);
  570. ValBinary:=0;
  571. exit;
  572. end;
  573. end;
  574. ValBinary:=vs;
  575. end;
  576. Function ValHexadecimal(const S:String):aint;
  577. { Converts a binary string to aint }
  578. var
  579. vs : aint;
  580. c : longint;
  581. Begin
  582. vs:=0;
  583. for c:=1 to length(s) do
  584. begin
  585. vs:=vs shl 4;
  586. case s[c] of
  587. '0'..'9' :
  588. inc(vs,ord(s[c])-ord('0'));
  589. 'A'..'F' :
  590. inc(vs,ord(s[c])-ord('A')+10);
  591. 'a'..'f' :
  592. inc(vs,ord(s[c])-ord('a')+10);
  593. else
  594. begin
  595. Message1(asmr_e_error_converting_hexadecimal,s);
  596. ValHexadecimal:=0;
  597. exit;
  598. end;
  599. end;
  600. end;
  601. ValHexadecimal:=vs;
  602. end;
  603. Function PadZero(Var s: String; n: byte): Boolean;
  604. Begin
  605. PadZero:=TRUE;
  606. { Do some error checking first }
  607. if Length(s) = n then
  608. exit
  609. else
  610. if Length(s) > n then
  611. Begin
  612. PadZero:=FALSE;
  613. delete(s,n+1,length(s));
  614. exit;
  615. end
  616. else
  617. PadZero:=TRUE;
  618. { Fill it up with the specified character }
  619. fillchar(s[length(s)+1],n-1,#0);
  620. s[0]:=chr(n);
  621. end;
  622. {****************************************************************************
  623. TOperand
  624. ****************************************************************************}
  625. constructor TOperand.Create;
  626. begin
  627. size:=OS_NO;
  628. hastype:=false;
  629. hasvar:=false;
  630. FillChar(Opr,sizeof(Opr),0);
  631. end;
  632. destructor TOperand.destroy;
  633. begin
  634. end;
  635. Procedure TOperand.SetSize(_size:longint;force:boolean);
  636. begin
  637. if force or
  638. ((size = OS_NO) and (_size<=extended_size)) then
  639. Begin
  640. case _size of
  641. 1 : size:=OS_8;
  642. 2 : size:=OS_16{ could be S_IS};
  643. 4 : size:=OS_32{ could be S_IL or S_FS};
  644. 8 : size:=OS_64{ could be S_D or S_FL};
  645. else
  646. begin
  647. { extended_size can also be 8, resulting in a
  648. duplicate label }
  649. if _size=extended_size then
  650. size:=OS_F80;
  651. end;
  652. end;
  653. end;
  654. end;
  655. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  656. begin
  657. end;
  658. function TOperand.SetupResult:boolean;
  659. begin
  660. SetupResult:=false;
  661. { replace by correct offset. }
  662. with current_procinfo.procdef do
  663. if (not is_void(rettype.def)) then
  664. begin
  665. if (m_tp7 in aktmodeswitches) and
  666. (not paramanager.ret_in_param(rettype.def,proccalloption)) then
  667. begin
  668. message(asmr_e_cannot_use_RESULT_here);
  669. exit;
  670. end;
  671. SetupResult:=setupvar('result',false)
  672. end
  673. else
  674. message(asmr_e_void_function);
  675. end;
  676. Function TOperand.SetupSelf:boolean;
  677. Begin
  678. SetupSelf:=false;
  679. if assigned(current_procinfo.procdef._class) then
  680. SetupSelf:=setupvar('self',false)
  681. else
  682. Message(asmr_e_cannot_use_SELF_outside_a_method);
  683. end;
  684. Function TOperand.SetupOldEBP:boolean;
  685. Begin
  686. SetupOldEBP:=false;
  687. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  688. SetupOldEBP:=setupvar('parentframe',false)
  689. else
  690. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  691. end;
  692. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  693. function symtable_has_varsyms(st:tsymtable):boolean;
  694. var
  695. sym : tsym;
  696. begin
  697. result:=false;
  698. sym:=tsym(st.symindex.first);
  699. while assigned(sym) do
  700. begin
  701. if sym.typ=varsym then
  702. begin
  703. result:=true;
  704. exit;
  705. end;
  706. sym:=tsym(sym.indexnext);
  707. end;
  708. end;
  709. procedure setconst(l:aint);
  710. begin
  711. { We return the address of the field, just like Delphi/TP }
  712. case opr.typ of
  713. OPR_NONE :
  714. begin
  715. opr.typ:=OPR_CONSTANT;
  716. opr.val:=l;
  717. end;
  718. OPR_CONSTANT :
  719. inc(opr.val,l);
  720. OPR_REFERENCE :
  721. inc(opr.ref.offset,l);
  722. OPR_LOCAL :
  723. inc(opr.localsymofs,l);
  724. else
  725. Message(asmr_e_invalid_operand_type);
  726. end;
  727. end;
  728. { search and sets up the correct fields in the Instr record }
  729. { for the NON-constant identifier passed to the routine. }
  730. { if not found returns FALSE. }
  731. var
  732. sym : tsym;
  733. srsymtable : tsymtable;
  734. harrdef : tarraydef;
  735. indexreg : tregister;
  736. l : aint;
  737. plist : psymlistitem;
  738. Begin
  739. SetupVar:=false;
  740. asmsearchsym(s,sym,srsymtable);
  741. if sym = nil then
  742. exit;
  743. if sym.typ=absolutesym then
  744. begin
  745. if (tabsolutesym(sym).abstyp=tovar) then
  746. begin
  747. { Only support simple loads }
  748. plist:=tabsolutesym(sym).ref.firstsym;
  749. if assigned(plist) and
  750. (plist^.sltype=sl_load) then
  751. sym:=plist^.sym
  752. else
  753. begin
  754. Message(asmr_e_unsupported_symbol_type);
  755. exit;
  756. end;
  757. end
  758. else
  759. begin
  760. Message(asmr_e_unsupported_symbol_type);
  761. exit;
  762. end;
  763. end;
  764. case sym.typ of
  765. varsym :
  766. begin
  767. { we always assume in asm statements that }
  768. { that the variable is valid. }
  769. tvarsym(sym).varstate:=vs_used;
  770. inc(tvarsym(sym).refs);
  771. case tvarsym(sym).owner.symtabletype of
  772. objectsymtable :
  773. begin
  774. setconst(tvarsym(sym).fieldoffset);
  775. hasvar:=true;
  776. SetupVar:=true;
  777. Exit;
  778. end;
  779. globalsymtable,
  780. staticsymtable :
  781. begin
  782. initref;
  783. opr.ref.symbol:=objectlibrary.newasmsymbol(tvarsym(sym).mangledname,AB_EXTERNAL,AT_DATA);
  784. end;
  785. parasymtable,
  786. localsymtable :
  787. begin
  788. if (vo_is_external in tvarsym(sym).varoptions) then
  789. begin
  790. initref;
  791. opr.ref.symbol:=objectlibrary.newasmsymbol(tvarsym(sym).mangledname,AB_EXTERNAL,AT_DATA)
  792. end
  793. else
  794. begin
  795. if opr.typ=OPR_REFERENCE then
  796. begin
  797. indexreg:=opr.ref.base;
  798. if opr.ref.index<>NR_NO then
  799. begin
  800. if indexreg=NR_NO then
  801. indexreg:=opr.ref.index
  802. else
  803. Message(asmr_e_multiple_index);
  804. end;
  805. end
  806. else
  807. indexreg:=NR_NO;
  808. opr.typ:=OPR_LOCAL;
  809. if assigned(current_procinfo.parent) and
  810. (current_procinfo.procdef.proccalloption<>pocall_inline) and
  811. (tvarsym(sym).owner<>current_procinfo.procdef.localst) and
  812. (tvarsym(sym).owner<>current_procinfo.procdef.parast) and
  813. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  814. symtable_has_varsyms(current_procinfo.procdef.localst) then
  815. message1(asmr_e_local_para_unreachable,s);
  816. { variable can't be placed in a register anymore }
  817. exclude(tvarsym(sym).varoptions,vo_regable);
  818. exclude(tvarsym(sym).varoptions,vo_fpuregable);
  819. opr.localsym:=tvarsym(sym);
  820. opr.localsymofs:=0;
  821. opr.localindexreg:=indexreg;
  822. opr.localscale:=0;
  823. opr.localgetoffset:=GetOffset;
  824. end;
  825. if paramanager.push_addr_param(tvarsym(sym).varspez,tvarsym(sym).vartype.def,current_procinfo.procdef.proccalloption) then
  826. SetSize(sizeof(aint),false);
  827. end;
  828. end;
  829. case tvarsym(sym).vartype.def.deftype of
  830. orddef,
  831. enumdef,
  832. pointerdef,
  833. floatdef :
  834. SetSize(tvarsym(sym).getsize,false);
  835. arraydef :
  836. begin
  837. { for arrays try to get the element size, take care of
  838. multiple indexes }
  839. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  840. while assigned(harrdef.elementtype.def) and
  841. (harrdef.elementtype.def.deftype=arraydef) do
  842. harrdef:=tarraydef(harrdef.elementtype.def);
  843. SetSize(harrdef.elesize,false);
  844. end;
  845. end;
  846. hasvar:=true;
  847. SetupVar:=true;
  848. Exit;
  849. end;
  850. typedconstsym :
  851. begin
  852. initref;
  853. opr.ref.symbol:=objectlibrary.newasmsymbol(ttypedconstsym(sym).mangledname,AB_EXTERNAL,AT_DATA);
  854. case ttypedconstsym(sym).typedconsttype.def.deftype of
  855. orddef,
  856. enumdef,
  857. pointerdef,
  858. floatdef :
  859. SetSize(ttypedconstsym(sym).getsize,false);
  860. arraydef :
  861. begin
  862. { for arrays try to get the element size, take care of
  863. multiple indexes }
  864. harrdef:=tarraydef(ttypedconstsym(sym).typedconsttype.def);
  865. while assigned(harrdef.elementtype.def) and
  866. (harrdef.elementtype.def.deftype=arraydef) do
  867. harrdef:=tarraydef(harrdef.elementtype.def);
  868. SetSize(harrdef.elesize,false);
  869. end;
  870. end;
  871. hasvar:=true;
  872. SetupVar:=true;
  873. Exit;
  874. end;
  875. constsym :
  876. begin
  877. if tconstsym(sym).consttyp=constord then
  878. begin
  879. setconst(tconstsym(sym).value.valueord);
  880. SetupVar:=true;
  881. Exit;
  882. end;
  883. end;
  884. typesym :
  885. begin
  886. if ttypesym(sym).restype.def.deftype in [recorddef,objectdef] then
  887. begin
  888. setconst(0);
  889. SetupVar:=TRUE;
  890. Exit;
  891. end;
  892. end;
  893. procsym :
  894. begin
  895. if opr.typ<>OPR_NONE then
  896. Message(asmr_e_invalid_operand_type);
  897. if Tprocsym(sym).procdef_count>1 then
  898. Message(asmr_w_calling_overload_func);
  899. l:=opr.ref.offset;
  900. opr.typ:=OPR_SYMBOL;
  901. opr.symbol:=objectlibrary.newasmsymbol(tprocsym(sym).first_procdef.mangledname,AB_EXTERNAL,AT_FUNCTION);
  902. opr.symofs:=l;
  903. hasvar:=true;
  904. SetupVar:=TRUE;
  905. Exit;
  906. end;
  907. else
  908. begin
  909. Message(asmr_e_unsupported_symbol_type);
  910. exit;
  911. end;
  912. end;
  913. end;
  914. { looks for internal names of variables and routines }
  915. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  916. var
  917. p : tasmsymbol;
  918. begin
  919. SetupDirectVar:=false;
  920. p:=objectlibrary.getasmsymbol(hs);
  921. if assigned(p) then
  922. begin
  923. InitRef;
  924. opr.ref.symbol:=p;
  925. hasvar:=true;
  926. SetupDirectVar:=true;
  927. end;
  928. end;
  929. procedure TOperand.InitRef;
  930. {*********************************************************************}
  931. { Description: This routine first check if the opcode is of }
  932. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  933. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  934. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  935. { to point to the default segment. }
  936. {*********************************************************************}
  937. var
  938. l : aint;
  939. reg : tregister;
  940. Begin
  941. case opr.typ of
  942. OPR_REFERENCE :
  943. exit;
  944. OPR_CONSTANT :
  945. begin
  946. l:=opr.val;
  947. opr.typ:=OPR_REFERENCE;
  948. Fillchar(opr.ref,sizeof(treference),0);
  949. opr.Ref.Offset:=l;
  950. end;
  951. OPR_NONE :
  952. begin
  953. opr.typ:=OPR_REFERENCE;
  954. Fillchar(opr.ref,sizeof(treference),0);
  955. end;
  956. OPR_REGISTER :
  957. begin
  958. reg:=opr.reg;
  959. opr.typ:=OPR_REFERENCE;
  960. Fillchar(opr.ref,sizeof(treference),0);
  961. opr.Ref.base:=reg;
  962. end;
  963. else
  964. begin
  965. Message(asmr_e_invalid_operand_type);
  966. { Recover }
  967. opr.typ:=OPR_REFERENCE;
  968. Fillchar(opr.ref,sizeof(treference),0);
  969. end;
  970. end;
  971. end;
  972. {****************************************************************************
  973. TInstruction
  974. ****************************************************************************}
  975. constructor TInstruction.create(optype : tcoperand);
  976. var
  977. i : longint;
  978. Begin
  979. { these field are set to 0 anyways by the constructor helper (FK)
  980. Opcode:=A_NONE;
  981. Condition:=C_NONE;
  982. Ops:=0;
  983. }
  984. for i:=1 to max_operands do
  985. Operands[i]:=optype.create;
  986. Labeled:=false;
  987. end;
  988. destructor TInstruction.destroy;
  989. var
  990. i : longint;
  991. Begin
  992. for i:=1 to max_operands do
  993. Operands[i].free;
  994. end;
  995. Procedure TInstruction.Swapoperands;
  996. Var
  997. p : toperand;
  998. Begin
  999. case Ops of
  1000. 2 :
  1001. begin
  1002. p:=Operands[1];
  1003. Operands[1]:=Operands[2];
  1004. Operands[2]:=p;
  1005. end;
  1006. 3 :
  1007. begin
  1008. p:=Operands[1];
  1009. Operands[1]:=Operands[3];
  1010. Operands[3]:=p;
  1011. end;
  1012. end;
  1013. end;
  1014. function TInstruction.ConcatInstruction(p:TAAsmoutput) : tai;
  1015. var
  1016. ai : taicpu;
  1017. i : longint;
  1018. begin
  1019. ai:=taicpu.op_none(opcode);
  1020. ai.Ops:=Ops;
  1021. ai.Allocate_oper(Ops);
  1022. for i:=1 to Ops do
  1023. with operands[i].opr do
  1024. begin
  1025. case typ of
  1026. OPR_CONSTANT :
  1027. ai.loadconst(i-1,val);
  1028. OPR_REGISTER:
  1029. ai.loadreg(i-1,reg);
  1030. OPR_SYMBOL:
  1031. ai.loadsymbol(i-1,symbol,symofs);
  1032. OPR_LOCAL :
  1033. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1034. localscale,localgetoffset);
  1035. OPR_REFERENCE:
  1036. ai.loadref(i-1,ref);
  1037. {$ifdef ARM}
  1038. OPR_REGSET:
  1039. ai.loadregset(i-1,regset);
  1040. {$endif ARM}
  1041. end;
  1042. end;
  1043. ai.SetCondition(condition);
  1044. { Concat the opcode or give an error }
  1045. if assigned(ai) then
  1046. p.concat(ai)
  1047. else
  1048. Message(asmr_e_invalid_opcode_and_operand);
  1049. result:=ai;
  1050. end;
  1051. {***************************************************************************
  1052. TLocalLabel
  1053. ***************************************************************************}
  1054. constructor TLocalLabel.create(const n:string);
  1055. begin
  1056. inherited CreateName(n);
  1057. lab:=nil;
  1058. emitted:=false;
  1059. end;
  1060. function TLocalLabel.Gettasmlabel:tasmlabel;
  1061. begin
  1062. if not assigned(lab) then
  1063. begin
  1064. objectlibrary.getlabel(lab);
  1065. { this label is forced to be used so it's always written }
  1066. lab.increfs;
  1067. end;
  1068. Gettasmlabel:=lab;
  1069. end;
  1070. {***************************************************************************
  1071. TLocalLabelList
  1072. ***************************************************************************}
  1073. procedure LocalLabelEmitted(p:tnamedindexitem;arg:pointer);
  1074. begin
  1075. if not TLocalLabel(p).emitted then
  1076. Message1(asmr_e_unknown_label_identifier,p.name);
  1077. end;
  1078. procedure TLocalLabelList.CheckEmitted;
  1079. begin
  1080. ForEach_Static({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted,nil)
  1081. end;
  1082. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1083. var
  1084. lab : TLocalLabel;
  1085. Begin
  1086. CreateLocalLabel:=true;
  1087. { Check if it already is defined }
  1088. lab:=TLocalLabel(LocalLabellist.Search(s));
  1089. if not assigned(lab) then
  1090. begin
  1091. lab:=TLocalLabel.Create(s);
  1092. LocalLabellist.Insert(lab);
  1093. end;
  1094. { set emitted flag and check for dup syms }
  1095. if emit then
  1096. begin
  1097. if lab.Emitted then
  1098. begin
  1099. Message1(asmr_e_dup_local_sym,lab.Name);
  1100. CreateLocalLabel:=false;
  1101. end;
  1102. lab.Emitted:=true;
  1103. end;
  1104. hl:=lab.Gettasmlabel;
  1105. end;
  1106. {****************************************************************************
  1107. Symbol table helper routines
  1108. ****************************************************************************}
  1109. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1110. var
  1111. i : integer;
  1112. begin
  1113. i:=pos('.',s);
  1114. { allow unit.identifier }
  1115. if i>0 then
  1116. begin
  1117. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1118. if assigned(srsym) then
  1119. begin
  1120. if (srsym.typ=unitsym) and
  1121. (srsym.owner.unitid=0) then
  1122. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1123. else
  1124. srsym:=nil;
  1125. end;
  1126. end
  1127. else
  1128. searchsym(s,srsym,srsymtable);
  1129. end;
  1130. Function SearchType(const hs:string;var size:aint): Boolean;
  1131. var
  1132. srsym : tsym;
  1133. srsymtable : tsymtable;
  1134. begin
  1135. result:=false;
  1136. size:=0;
  1137. asmsearchsym(hs,srsym,srsymtable);
  1138. if assigned(srsym) and
  1139. (srsym.typ=typesym) then
  1140. begin
  1141. size:=ttypesym(srsym).restype.def.size;
  1142. result:=true;
  1143. end;
  1144. end;
  1145. Function SearchRecordType(const s:string): boolean;
  1146. var
  1147. srsym : tsym;
  1148. srsymtable : tsymtable;
  1149. Begin
  1150. SearchRecordType:=false;
  1151. { Check the constants in symtable }
  1152. asmsearchsym(s,srsym,srsymtable);
  1153. if srsym <> nil then
  1154. Begin
  1155. case srsym.typ of
  1156. typesym :
  1157. begin
  1158. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1159. begin
  1160. SearchRecordType:=true;
  1161. exit;
  1162. end;
  1163. end;
  1164. end;
  1165. end;
  1166. end;
  1167. Function SearchIConstant(const s:string; var l:aint): boolean;
  1168. {**********************************************************************}
  1169. { Description: Searches for a CONSTANT of name s in either the local }
  1170. { symbol list, then in the global symbol list, and returns the value }
  1171. { of that constant in l. Returns TRUE if successfull, if not found, }
  1172. { or if the constant is not of correct type, then returns FALSE }
  1173. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1174. { respectively. }
  1175. {**********************************************************************}
  1176. var
  1177. srsym : tsym;
  1178. srsymtable : tsymtable;
  1179. Begin
  1180. SearchIConstant:=false;
  1181. { check for TRUE or FALSE reserved words first }
  1182. if s = 'TRUE' then
  1183. Begin
  1184. SearchIConstant:=TRUE;
  1185. l:=1;
  1186. exit;
  1187. end;
  1188. if s = 'FALSE' then
  1189. Begin
  1190. SearchIConstant:=TRUE;
  1191. l:=0;
  1192. exit;
  1193. end;
  1194. { Check the constants in symtable }
  1195. asmsearchsym(s,srsym,srsymtable);
  1196. if srsym <> nil then
  1197. Begin
  1198. case srsym.typ of
  1199. constsym :
  1200. begin
  1201. if tconstsym(srsym).consttyp=constord then
  1202. Begin
  1203. l:=tconstsym(srsym).value.valueord;
  1204. SearchIConstant:=TRUE;
  1205. exit;
  1206. end;
  1207. end;
  1208. enumsym:
  1209. Begin
  1210. l:=tenumsym(srsym).value;
  1211. SearchIConstant:=TRUE;
  1212. exit;
  1213. end;
  1214. end;
  1215. end;
  1216. end;
  1217. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint):boolean;
  1218. { search and returns the offset and size of records/objects of the base }
  1219. { with field name setup in field. }
  1220. { returns FALSE if not found. }
  1221. { used when base is a variable or a typed constant name. }
  1222. var
  1223. st : tsymtable;
  1224. harrdef : tarraydef;
  1225. sym : tsym;
  1226. srsymtable : tsymtable;
  1227. i : longint;
  1228. base : string;
  1229. Begin
  1230. GetRecordOffsetSize:=FALSE;
  1231. Offset:=0;
  1232. Size:=0;
  1233. i:=pos('.',s);
  1234. if i=0 then
  1235. i:=255;
  1236. base:=Copy(s,1,i-1);
  1237. delete(s,1,i);
  1238. if base='SELF' then
  1239. st:=current_procinfo.procdef._class.symtable
  1240. else
  1241. begin
  1242. asmsearchsym(base,sym,srsymtable);
  1243. st:=nil;
  1244. { we can start with a var,type,typedconst }
  1245. if assigned(sym) then
  1246. case sym.typ of
  1247. varsym :
  1248. with Tvarsym(sym).vartype do
  1249. case def.deftype of
  1250. recorddef :
  1251. st:=trecorddef(def).symtable;
  1252. objectdef :
  1253. st:=tobjectdef(def).symtable;
  1254. end;
  1255. typesym :
  1256. with Ttypesym(sym).restype do
  1257. case def.deftype of
  1258. recorddef :
  1259. st:=trecorddef(def).symtable;
  1260. objectdef :
  1261. st:=tobjectdef(def).symtable;
  1262. end;
  1263. typedconstsym :
  1264. with Ttypedconstsym(sym).typedconsttype do
  1265. case def.deftype of
  1266. recorddef :
  1267. st:=trecorddef(def).symtable;
  1268. objectdef :
  1269. st:=tobjectdef(def).symtable;
  1270. end;
  1271. end
  1272. else
  1273. s:='';
  1274. end;
  1275. { now walk all recordsymtables }
  1276. while assigned(st) and (s<>'') do
  1277. begin
  1278. { load next field in base }
  1279. i:=pos('.',s);
  1280. if i=0 then
  1281. i:=255;
  1282. base:=Copy(s,1,i-1);
  1283. delete(s,1,i);
  1284. if st.symtabletype=objectsymtable then
  1285. sym:=search_class_member(tobjectdef(st.defowner),base)
  1286. else
  1287. sym:=tsym(st.search(base));
  1288. if not assigned(sym) then
  1289. begin
  1290. GetRecordOffsetSize:=false;
  1291. exit;
  1292. end;
  1293. st:=nil;
  1294. case sym.typ of
  1295. varsym :
  1296. with Tvarsym(sym) do
  1297. begin
  1298. inc(Offset,fieldoffset);
  1299. size:=getsize;
  1300. with vartype do
  1301. case def.deftype of
  1302. arraydef :
  1303. begin
  1304. { for arrays try to get the element size, take care of
  1305. multiple indexes }
  1306. harrdef:=tarraydef(def);
  1307. while assigned(harrdef.elementtype.def) and
  1308. (harrdef.elementtype.def.deftype=arraydef) do
  1309. harrdef:=tarraydef(harrdef.elementtype.def);
  1310. size:=harrdef.elesize;
  1311. end;
  1312. recorddef :
  1313. st:=trecorddef(def).symtable;
  1314. objectdef :
  1315. st:=tobjectdef(def).symtable;
  1316. end;
  1317. end;
  1318. end;
  1319. end;
  1320. GetRecordOffsetSize:=(s='');
  1321. end;
  1322. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1323. var
  1324. sym : tsym;
  1325. srsymtable : tsymtable;
  1326. hs : string;
  1327. Begin
  1328. hl:=nil;
  1329. SearchLabel:=false;
  1330. { Check for pascal labels, which are case insensetive }
  1331. hs:=upper(s);
  1332. asmsearchsym(hs,sym,srsymtable);
  1333. if sym=nil then
  1334. exit;
  1335. case sym.typ of
  1336. labelsym :
  1337. begin
  1338. hl:=tlabelsym(sym).lab;
  1339. if emit then
  1340. tlabelsym(sym).defined:=true
  1341. else
  1342. tlabelsym(sym).used:=true;
  1343. SearchLabel:=true;
  1344. end;
  1345. end;
  1346. end;
  1347. {*************************************************************************}
  1348. { Instruction Generation Utilities }
  1349. {*************************************************************************}
  1350. Procedure ConcatString(p : TAAsmoutput;s:string);
  1351. {*********************************************************************}
  1352. { PROCEDURE ConcatString(s:string); }
  1353. { Description: This routine adds the character chain pointed to in }
  1354. { s to the instruction linked list. }
  1355. {*********************************************************************}
  1356. Var
  1357. pc: PChar;
  1358. Begin
  1359. getmem(pc,length(s)+1);
  1360. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1361. end;
  1362. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1363. {*********************************************************************}
  1364. { PROCEDURE ConcatPasString(s:string); }
  1365. { Description: This routine adds the character chain pointed to in }
  1366. { s to the instruction linked list, contrary to ConcatString it }
  1367. { uses a pascal style string, so it conserves null characters. }
  1368. {*********************************************************************}
  1369. Begin
  1370. p.concat(Tai_string.Create(s));
  1371. end;
  1372. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1373. {*********************************************************************}
  1374. { PROCEDURE ConcatDirect(s:string) }
  1375. { Description: This routine output the string directly to the asm }
  1376. { output, it is only sed when writing special labels in AT&T mode, }
  1377. { and should not be used without due consideration, since it may }
  1378. { cause problems. }
  1379. {*********************************************************************}
  1380. Var
  1381. pc: PChar;
  1382. Begin
  1383. getmem(pc,length(s)+1);
  1384. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1385. end;
  1386. Procedure ConcatConstant(p: TAAsmoutput; value: aint; constsize:byte);
  1387. {*********************************************************************}
  1388. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1389. { Description: This routine adds the value constant to the current }
  1390. { instruction linked list. }
  1391. { maxvalue -> indicates the size of the data to initialize: }
  1392. { $ff -> create a byte node. }
  1393. { $ffff -> create a word node. }
  1394. { $ffffffff -> create a dword node. }
  1395. {*********************************************************************}
  1396. var
  1397. rangelo,rangehi : int64;
  1398. Begin
  1399. case constsize of
  1400. 1 :
  1401. begin
  1402. p.concat(Tai_const.Create_8bit(byte(value)));
  1403. rangelo:=low(shortint);
  1404. rangehi:=high(byte);
  1405. end;
  1406. 2 :
  1407. begin
  1408. p.concat(Tai_const.Create_16bit(word(value)));
  1409. rangelo:=low(smallint);
  1410. rangehi:=high(word);
  1411. end;
  1412. 4 :
  1413. begin
  1414. p.concat(Tai_const.Create_32bit(longint(value)));
  1415. rangelo:=low(longint);
  1416. rangehi:=high(cardinal);
  1417. end;
  1418. 8 :
  1419. begin
  1420. p.concat(Tai_const.Create_64bit(int64(value)));
  1421. rangelo:=0;
  1422. rangehi:=0;
  1423. end;
  1424. else
  1425. internalerror(200405011);
  1426. end;
  1427. { check for out of bounds }
  1428. if (rangelo<>0) and
  1429. ((value>rangehi) or (value<rangelo)) then
  1430. Message(asmr_e_constant_out_of_bounds);
  1431. end;
  1432. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:aint);
  1433. begin
  1434. p.concat(Tai_const.Createname(sym,AT_DATA,l));
  1435. end;
  1436. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1437. {***********************************************************************}
  1438. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1439. { Description: This routine adds the value constant to the current }
  1440. { instruction linked list. }
  1441. { real_typ -> indicates the type of the real data to initialize: }
  1442. { s32real -> create a single node. }
  1443. { s64real -> create a double node. }
  1444. { s80real -> create an extended node. }
  1445. { s64bit -> create a comp node. }
  1446. { f32bit -> create a fixed node. (not used normally) }
  1447. {***********************************************************************}
  1448. Begin
  1449. case real_typ of
  1450. s32real : p.concat(Tai_real_32bit.Create(value));
  1451. s64real :
  1452. {$ifdef ARM}
  1453. if aktfputype in [fpu_fpa,fpu_fpa10,fpu_fpa11] then
  1454. p.concat(Tai_real_64bit.Create_hiloswapped(value))
  1455. else
  1456. {$endif ARM}
  1457. p.concat(Tai_real_64bit.Create(value));
  1458. s80real : p.concat(Tai_real_80bit.Create(value));
  1459. s64comp : p.concat(Tai_comp_64bit.Create(trunc(value)));
  1460. end;
  1461. end;
  1462. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1463. {*********************************************************************}
  1464. { PROCEDURE ConcatLabel }
  1465. { Description: This routine either emits a label or a labeled }
  1466. { instruction to the linked list of instructions. }
  1467. {*********************************************************************}
  1468. begin
  1469. p.concat(Tai_label.Create(l));
  1470. end;
  1471. procedure ConcatAlign(p:TAAsmoutput;l:aint);
  1472. {*********************************************************************}
  1473. { PROCEDURE ConcatPublic }
  1474. { Description: This routine emits an global definition to the }
  1475. { linked list of instructions.(used by AT&T styled asm) }
  1476. {*********************************************************************}
  1477. begin
  1478. p.concat(Tai_align.Create(l));
  1479. end;
  1480. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1481. {*********************************************************************}
  1482. { PROCEDURE ConcatPublic }
  1483. { Description: This routine emits an global definition to the }
  1484. { linked list of instructions.(used by AT&T styled asm) }
  1485. {*********************************************************************}
  1486. begin
  1487. p.concat(Tai_symbol.Createname_global(s,AT_FUNCTION,0));
  1488. end;
  1489. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1490. {*********************************************************************}
  1491. { PROCEDURE ConcatLocal }
  1492. { Description: This routine emits an local definition to the }
  1493. { linked list of instructions. }
  1494. {*********************************************************************}
  1495. begin
  1496. p.concat(Tai_symbol.Createname(s,AT_FUNCTION,0));
  1497. end;
  1498. Procedure ConcatGlobalBss(const s : string;size : aint);
  1499. {*********************************************************************}
  1500. { PROCEDURE ConcatGlobalBss }
  1501. { Description: This routine emits an global datablock to the }
  1502. { linked list of instructions. }
  1503. {*********************************************************************}
  1504. begin
  1505. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1506. end;
  1507. Procedure ConcatLocalBss(const s : string;size : aint);
  1508. {*********************************************************************}
  1509. { PROCEDURE ConcatLocalBss }
  1510. { Description: This routine emits a local datablcok to the }
  1511. { linked list of instructions. }
  1512. {*********************************************************************}
  1513. begin
  1514. bssSegment.concat(Tai_datablock.Create(s,size));
  1515. end;
  1516. end.
  1517. {
  1518. $Log$
  1519. Revision 1.89 2004-09-27 15:14:34 peter
  1520. * reference local varsyms can't be regvar
  1521. Revision 1.88 2004/06/20 08:55:30 florian
  1522. * logs truncated
  1523. Revision 1.87 2004/06/16 20:07:09 florian
  1524. * dwarf branch merged
  1525. Revision 1.86 2004/05/06 20:30:51 florian
  1526. * m68k compiler compilation fixed
  1527. Revision 1.85.2.7 2004/05/25 21:38:53 peter
  1528. * assembler reader/writer updates
  1529. Revision 1.85.2.6 2004/05/18 20:24:03 florian
  1530. * fixed crash with unknown symbols
  1531. Revision 1.85.2.5 2004/05/02 00:13:16 peter
  1532. * remove limit of 15 digits
  1533. }