rautils.pas 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  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. strings,
  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 : aint);
  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 : aint;
  205. { Get the operand at the top of the RPN stack }
  206. begin
  207. if RPNTop > 0 then
  208. begin
  209. RPNPop:=RPNStack[RPNTop];
  210. Dec(RPNTop);
  211. end
  212. else
  213. Message(asmr_e_expr_illegal);
  214. end;
  215. Procedure TExprParse.RPNCalc(const Token : String; prefix:boolean); { RPN Calculator }
  216. Var
  217. Temp : aint;
  218. n1,n2 : aint;
  219. LocalError : Integer;
  220. begin
  221. { Handle operators }
  222. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  223. Case Token[1] of
  224. '+' :
  225. Begin
  226. if not prefix then
  227. RPNPush(RPNPop + RPNPop);
  228. end;
  229. '-' :
  230. Begin
  231. if prefix then
  232. RPNPush(-(RPNPop))
  233. else
  234. begin
  235. n1:=RPNPop;
  236. n2:=RPNPop;
  237. RPNPush(n2 - n1);
  238. end;
  239. end;
  240. '*' : RPNPush(RPNPop * RPNPop);
  241. '&' :
  242. begin
  243. n1:=RPNPop;
  244. n2:=RPNPop;
  245. RPNPush(n2 and n1);
  246. end;
  247. '|' :
  248. begin
  249. n1:=RPNPop;
  250. n2:=RPNPop;
  251. RPNPush(n2 or n1);
  252. end;
  253. '~' : RPNPush(NOT RPNPop);
  254. '<' :
  255. begin
  256. n1:=RPNPop;
  257. n2:=RPNPop;
  258. RPNPush(n2 SHL n1);
  259. end;
  260. '>' :
  261. begin
  262. n1:=RPNPop;
  263. n2:=RPNPop;
  264. RPNPush(n2 SHR n1);
  265. end;
  266. '%' :
  267. begin
  268. Temp:=RPNPop;
  269. if Temp <> 0 then
  270. RPNPush(RPNPop mod Temp)
  271. else
  272. begin
  273. Message(asmr_e_expr_zero_divide);
  274. { push 1 for error recovery }
  275. RPNPush(1);
  276. end;
  277. end;
  278. '^' : RPNPush(RPNPop XOR RPNPop);
  279. '/' :
  280. begin
  281. Temp:=RPNPop;
  282. if Temp <> 0 then
  283. RPNPush(RPNPop div Temp)
  284. else
  285. begin
  286. Message(asmr_e_expr_zero_divide);
  287. { push 1 for error recovery }
  288. RPNPush(1);
  289. end;
  290. end;
  291. end
  292. else
  293. begin
  294. { Convert String to number and add to stack }
  295. Val(Token, Temp, LocalError);
  296. if LocalError = 0 then
  297. RPNPush(Temp)
  298. else
  299. begin
  300. Message(asmr_e_expr_illegal);
  301. { push 1 for error recovery }
  302. RPNPush(1);
  303. end;
  304. end;
  305. end;
  306. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  307. { Add an operator onto top of the stack }
  308. begin
  309. if OpTop < OpMax then
  310. begin
  311. Inc(OpTop);
  312. OpStack[OpTop].ch:=_Operator;
  313. OpStack[OpTop].is_prefix:=prefix;
  314. end
  315. else
  316. Message(asmr_e_expr_illegal);
  317. end;
  318. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  319. { Get operator at the top of the stack }
  320. begin
  321. if OpTop > 0 then
  322. begin
  323. _Operator:=OpStack[OpTop];
  324. Dec(OpTop);
  325. end
  326. else
  327. Message(asmr_e_expr_illegal);
  328. end;
  329. Function TExprParse.Priority(_Operator : Char) : aint;
  330. { Return priority of operator }
  331. { The greater the priority, the higher the precedence }
  332. begin
  333. Case _Operator OF
  334. '(' :
  335. Priority:=0;
  336. '+', '-' :
  337. Priority:=1;
  338. '*', '/','%','<','>' :
  339. Priority:=2;
  340. '|','&','^','~' :
  341. Priority:=0;
  342. else
  343. Message(asmr_e_expr_illegal);
  344. end;
  345. end;
  346. Function TExprParse.Evaluate(Expr : String):aint;
  347. Var
  348. I : longint;
  349. Token : String;
  350. opr : TExprOperator;
  351. begin
  352. Evaluate:=0;
  353. { Reset stacks }
  354. OpTop :=0;
  355. RPNTop:=0;
  356. Token :='';
  357. { nothing to do ? }
  358. if Expr='' then
  359. exit;
  360. For I:=1 to Length(Expr) DO
  361. begin
  362. if Expr[I] in ['0'..'9'] then
  363. begin { Build multi-digit numbers }
  364. Token:=Token + Expr[I];
  365. if I = Length(Expr) then { Send last one to calculator }
  366. RPNCalc(Token,false);
  367. end
  368. else
  369. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  370. begin
  371. if Token <> '' then
  372. begin { Send last built number to calc. }
  373. RPNCalc(Token,false);
  374. Token:='';
  375. end;
  376. Case Expr[I] OF
  377. '(' : OpPush('(',false);
  378. ')' : begin
  379. While OpStack[OpTop].ch <> '(' DO
  380. Begin
  381. OpPop(opr);
  382. RPNCalc(opr.ch,opr.is_prefix);
  383. end;
  384. OpPop(opr); { Pop off and ignore the '(' }
  385. end;
  386. '+','-','~' : Begin
  387. { workaround for -2147483648 }
  388. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  389. begin
  390. token:='-';
  391. expr[i]:='+';
  392. end;
  393. { if start of expression then surely a prefix }
  394. { or if previous char was also an operator }
  395. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  396. OpPush(Expr[I],true)
  397. else
  398. Begin
  399. { Evaluate all higher priority operators }
  400. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  401. Begin
  402. OpPop(opr);
  403. RPNCalc(opr.ch,opr.is_prefix);
  404. end;
  405. OpPush(Expr[I],false);
  406. End;
  407. end;
  408. '*', '/',
  409. '^','|','&',
  410. '%','<','>' : begin
  411. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  412. Begin
  413. OpPop(opr);
  414. RPNCalc(opr.ch,opr.is_prefix);
  415. end;
  416. OpPush(Expr[I],false);
  417. end;
  418. end; { Case }
  419. end
  420. else
  421. Message(asmr_e_expr_illegal); { Handle bad input error }
  422. end;
  423. { Pop off the remaining operators }
  424. While OpTop > 0 do
  425. Begin
  426. OpPop(opr);
  427. RPNCalc(opr.ch,opr.is_prefix);
  428. end;
  429. { The result is stored on the top of the stack }
  430. Evaluate:=RPNPop;
  431. end;
  432. Destructor TExprParse.Destroy;
  433. Begin
  434. end;
  435. Function CalculateExpression(const expression: string): aint;
  436. var
  437. expr: TExprParse;
  438. Begin
  439. expr:=TExprParse.create;
  440. CalculateExpression:=expr.Evaluate(expression);
  441. expr.Free;
  442. end;
  443. {*************************************************************************}
  444. { String conversions/utils }
  445. {*************************************************************************}
  446. Function EscapeToPascal(const s:string): string;
  447. { converts a C styled string - which contains escape }
  448. { characters to a pascal style string. }
  449. var
  450. i,len : aint;
  451. hs : string;
  452. temp : string;
  453. c : char;
  454. Begin
  455. hs:='';
  456. len:=0;
  457. i:=0;
  458. while (i<length(s)) and (len<255) do
  459. begin
  460. Inc(i);
  461. if (s[i]='\') and (i<length(s)) then
  462. Begin
  463. inc(i);
  464. case s[i] of
  465. '\' :
  466. c:='\';
  467. 'b':
  468. c:=#8;
  469. 'f':
  470. c:=#12;
  471. 'n':
  472. c:=#10;
  473. 'r':
  474. c:=#13;
  475. 't':
  476. c:=#9;
  477. '"':
  478. c:='"';
  479. '0'..'7':
  480. Begin
  481. temp:=s[i];
  482. temp:=temp+s[i+1];
  483. temp:=temp+s[i+2];
  484. inc(i,2);
  485. c:=chr(ValOctal(temp));
  486. end;
  487. 'x':
  488. Begin
  489. temp:=s[i+1];
  490. temp:=temp+s[i+2];
  491. inc(i,2);
  492. c:=chr(ValHexaDecimal(temp));
  493. end;
  494. else
  495. Begin
  496. Message1(asmr_e_escape_seq_ignored,s[i]);
  497. c:=s[i];
  498. end;
  499. end;
  500. end
  501. else
  502. c:=s[i];
  503. inc(len);
  504. hs[len]:=c;
  505. end;
  506. hs[0]:=chr(len);
  507. EscapeToPascal:=hs;
  508. end;
  509. Function ValDecimal(const S:String):aint;
  510. { Converts a decimal string to aint }
  511. var
  512. vs : aint;
  513. c : longint;
  514. Begin
  515. vs:=0;
  516. for c:=1 to length(s) do
  517. begin
  518. vs:=vs*10;
  519. if s[c] in ['0'..'9'] then
  520. inc(vs,ord(s[c])-ord('0'))
  521. else
  522. begin
  523. Message1(asmr_e_error_converting_decimal,s);
  524. ValDecimal:=0;
  525. exit;
  526. end;
  527. end;
  528. ValDecimal:=vs;
  529. end;
  530. Function ValOctal(const S:String):aint;
  531. { Converts an octal string to aint }
  532. var
  533. vs : aint;
  534. c : longint;
  535. Begin
  536. vs:=0;
  537. for c:=1 to length(s) do
  538. begin
  539. vs:=vs shl 3;
  540. if s[c] in ['0'..'7'] then
  541. inc(vs,ord(s[c])-ord('0'))
  542. else
  543. begin
  544. Message1(asmr_e_error_converting_octal,s);
  545. ValOctal:=0;
  546. exit;
  547. end;
  548. end;
  549. ValOctal:=vs;
  550. end;
  551. Function ValBinary(const S:String):aint;
  552. { Converts a binary string to aint }
  553. var
  554. vs : aint;
  555. c : longint;
  556. Begin
  557. vs:=0;
  558. for c:=1 to length(s) do
  559. begin
  560. vs:=vs shl 1;
  561. if s[c] in ['0'..'1'] then
  562. inc(vs,ord(s[c])-ord('0'))
  563. else
  564. begin
  565. Message1(asmr_e_error_converting_binary,s);
  566. ValBinary:=0;
  567. exit;
  568. end;
  569. end;
  570. ValBinary:=vs;
  571. end;
  572. Function ValHexadecimal(const S:String):aint;
  573. { Converts a binary string to aint }
  574. var
  575. vs : aint;
  576. c : longint;
  577. Begin
  578. vs:=0;
  579. for c:=1 to length(s) do
  580. begin
  581. vs:=vs shl 4;
  582. case s[c] of
  583. '0'..'9' :
  584. inc(vs,ord(s[c])-ord('0'));
  585. 'A'..'F' :
  586. inc(vs,ord(s[c])-ord('A')+10);
  587. 'a'..'f' :
  588. inc(vs,ord(s[c])-ord('a')+10);
  589. else
  590. begin
  591. Message1(asmr_e_error_converting_hexadecimal,s);
  592. ValHexadecimal:=0;
  593. exit;
  594. end;
  595. end;
  596. end;
  597. ValHexadecimal:=vs;
  598. end;
  599. Function PadZero(Var s: String; n: byte): Boolean;
  600. Begin
  601. PadZero:=TRUE;
  602. { Do some error checking first }
  603. if Length(s) = n then
  604. exit
  605. else
  606. if Length(s) > n then
  607. Begin
  608. PadZero:=FALSE;
  609. delete(s,n+1,length(s));
  610. exit;
  611. end
  612. else
  613. PadZero:=TRUE;
  614. { Fill it up with the specified character }
  615. fillchar(s[length(s)+1],n-1,#0);
  616. s[0]:=chr(n);
  617. end;
  618. {****************************************************************************
  619. TOperand
  620. ****************************************************************************}
  621. constructor TOperand.Create;
  622. begin
  623. size:=OS_NO;
  624. hastype:=false;
  625. hasvar:=false;
  626. FillChar(Opr,sizeof(Opr),0);
  627. end;
  628. destructor TOperand.destroy;
  629. begin
  630. end;
  631. Procedure TOperand.SetSize(_size:longint;force:boolean);
  632. begin
  633. if force or
  634. ((size = OS_NO) and (_size<=extended_size)) then
  635. Begin
  636. case _size of
  637. 1 : size:=OS_8;
  638. 2 : size:=OS_16{ could be S_IS};
  639. 4 : size:=OS_32{ could be S_IL or S_FS};
  640. 8 : size:=OS_64{ could be S_D or S_FL};
  641. else
  642. begin
  643. { extended_size can also be 8, resulting in a
  644. duplicate label }
  645. if _size=extended_size then
  646. size:=OS_F80;
  647. end;
  648. end;
  649. end;
  650. end;
  651. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  652. begin
  653. end;
  654. function TOperand.SetupResult:boolean;
  655. begin
  656. SetupResult:=false;
  657. { replace by correct offset. }
  658. with current_procinfo.procdef do
  659. if (not is_void(rettype.def)) then
  660. begin
  661. if (m_tp7 in aktmodeswitches) and
  662. (not paramanager.ret_in_param(rettype.def,proccalloption)) then
  663. begin
  664. message(asmr_e_cannot_use_RESULT_here);
  665. exit;
  666. end;
  667. SetupResult:=setupvar('result',false)
  668. end
  669. else
  670. message(asmr_e_void_function);
  671. end;
  672. Function TOperand.SetupSelf:boolean;
  673. Begin
  674. SetupSelf:=false;
  675. if assigned(current_procinfo.procdef._class) then
  676. SetupSelf:=setupvar('self',false)
  677. else
  678. Message(asmr_e_cannot_use_SELF_outside_a_method);
  679. end;
  680. Function TOperand.SetupOldEBP:boolean;
  681. Begin
  682. SetupOldEBP:=false;
  683. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  684. SetupOldEBP:=setupvar('parentframe',false)
  685. else
  686. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  687. end;
  688. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  689. function symtable_has_varsyms(st:tsymtable):boolean;
  690. var
  691. sym : tsym;
  692. begin
  693. result:=false;
  694. sym:=tsym(st.symindex.first);
  695. while assigned(sym) do
  696. begin
  697. if sym.typ=varsym then
  698. begin
  699. result:=true;
  700. exit;
  701. end;
  702. sym:=tsym(sym.indexnext);
  703. end;
  704. end;
  705. procedure setconst(l:aint);
  706. begin
  707. { We return the address of the field, just like Delphi/TP }
  708. case opr.typ of
  709. OPR_NONE :
  710. begin
  711. opr.typ:=OPR_CONSTANT;
  712. opr.val:=l;
  713. end;
  714. OPR_CONSTANT :
  715. inc(opr.val,l);
  716. OPR_REFERENCE :
  717. inc(opr.ref.offset,l);
  718. OPR_LOCAL :
  719. inc(opr.localsymofs,l);
  720. else
  721. Message(asmr_e_invalid_operand_type);
  722. end;
  723. end;
  724. { search and sets up the correct fields in the Instr record }
  725. { for the NON-constant identifier passed to the routine. }
  726. { if not found returns FALSE. }
  727. var
  728. sym : tsym;
  729. srsymtable : tsymtable;
  730. harrdef : tarraydef;
  731. indexreg : tregister;
  732. l : aint;
  733. plist : psymlistitem;
  734. Begin
  735. SetupVar:=false;
  736. asmsearchsym(s,sym,srsymtable);
  737. if sym = nil then
  738. exit;
  739. if sym.typ=absolutesym then
  740. begin
  741. if (tabsolutesym(sym).abstyp=tovar) then
  742. begin
  743. { Only support simple loads }
  744. plist:=tabsolutesym(sym).ref.firstsym;
  745. if assigned(plist) and
  746. (plist^.sltype=sl_load) then
  747. sym:=plist^.sym
  748. else
  749. begin
  750. Message(asmr_e_unsupported_symbol_type);
  751. exit;
  752. end;
  753. end
  754. else
  755. begin
  756. Message(asmr_e_unsupported_symbol_type);
  757. exit;
  758. end;
  759. end;
  760. case sym.typ of
  761. varsym :
  762. begin
  763. { we always assume in asm statements that }
  764. { that the variable is valid. }
  765. tvarsym(sym).varstate:=vs_used;
  766. inc(tvarsym(sym).refs);
  767. { variable can't be placed in a register }
  768. tvarsym(sym).varregable:=vr_none;
  769. case tvarsym(sym).owner.symtabletype of
  770. objectsymtable :
  771. begin
  772. setconst(tvarsym(sym).fieldoffset);
  773. hasvar:=true;
  774. SetupVar:=true;
  775. Exit;
  776. end;
  777. globalsymtable,
  778. staticsymtable :
  779. begin
  780. initref;
  781. opr.ref.symbol:=objectlibrary.newasmsymbol(tvarsym(sym).mangledname,AB_EXTERNAL,AT_DATA);
  782. end;
  783. parasymtable,
  784. localsymtable :
  785. begin
  786. if (vo_is_external in tvarsym(sym).varoptions) then
  787. begin
  788. initref;
  789. opr.ref.symbol:=objectlibrary.newasmsymbol(tvarsym(sym).mangledname,AB_EXTERNAL,AT_DATA)
  790. end
  791. else
  792. begin
  793. if opr.typ=OPR_REFERENCE then
  794. begin
  795. indexreg:=opr.ref.base;
  796. if opr.ref.index<>NR_NO then
  797. begin
  798. if indexreg=NR_NO then
  799. indexreg:=opr.ref.index
  800. else
  801. Message(asmr_e_multiple_index);
  802. end;
  803. end
  804. else
  805. indexreg:=NR_NO;
  806. opr.typ:=OPR_LOCAL;
  807. if assigned(current_procinfo.parent) and
  808. (current_procinfo.procdef.proccalloption<>pocall_inline) and
  809. (tvarsym(sym).owner<>current_procinfo.procdef.localst) and
  810. (tvarsym(sym).owner<>current_procinfo.procdef.parast) and
  811. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  812. symtable_has_varsyms(current_procinfo.procdef.localst) then
  813. message1(asmr_e_local_para_unreachable,s);
  814. opr.localsym:=tvarsym(sym);
  815. opr.localsymofs:=0;
  816. opr.localindexreg:=indexreg;
  817. opr.localscale:=0;
  818. opr.localgetoffset:=GetOffset;
  819. end;
  820. if paramanager.push_addr_param(tvarsym(sym).varspez,tvarsym(sym).vartype.def,current_procinfo.procdef.proccalloption) then
  821. SetSize(sizeof(aint),false);
  822. end;
  823. end;
  824. case tvarsym(sym).vartype.def.deftype of
  825. orddef,
  826. enumdef,
  827. pointerdef,
  828. floatdef :
  829. SetSize(tvarsym(sym).getsize,false);
  830. arraydef :
  831. begin
  832. { for arrays try to get the element size, take care of
  833. multiple indexes }
  834. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  835. while assigned(harrdef.elementtype.def) and
  836. (harrdef.elementtype.def.deftype=arraydef) do
  837. harrdef:=tarraydef(harrdef.elementtype.def);
  838. SetSize(harrdef.elesize,false);
  839. end;
  840. end;
  841. hasvar:=true;
  842. SetupVar:=true;
  843. Exit;
  844. end;
  845. typedconstsym :
  846. begin
  847. initref;
  848. opr.ref.symbol:=objectlibrary.newasmsymbol(ttypedconstsym(sym).mangledname,AB_EXTERNAL,AT_DATA);
  849. case ttypedconstsym(sym).typedconsttype.def.deftype of
  850. orddef,
  851. enumdef,
  852. pointerdef,
  853. floatdef :
  854. SetSize(ttypedconstsym(sym).getsize,false);
  855. arraydef :
  856. begin
  857. { for arrays try to get the element size, take care of
  858. multiple indexes }
  859. harrdef:=tarraydef(ttypedconstsym(sym).typedconsttype.def);
  860. while assigned(harrdef.elementtype.def) and
  861. (harrdef.elementtype.def.deftype=arraydef) do
  862. harrdef:=tarraydef(harrdef.elementtype.def);
  863. SetSize(harrdef.elesize,false);
  864. end;
  865. end;
  866. hasvar:=true;
  867. SetupVar:=true;
  868. Exit;
  869. end;
  870. constsym :
  871. begin
  872. if tconstsym(sym).consttyp=constord then
  873. begin
  874. setconst(tconstsym(sym).value.valueord);
  875. SetupVar:=true;
  876. Exit;
  877. end;
  878. end;
  879. typesym :
  880. begin
  881. if ttypesym(sym).restype.def.deftype in [recorddef,objectdef] then
  882. begin
  883. setconst(0);
  884. SetupVar:=TRUE;
  885. Exit;
  886. end;
  887. end;
  888. procsym :
  889. begin
  890. if opr.typ<>OPR_NONE then
  891. Message(asmr_e_invalid_operand_type);
  892. if Tprocsym(sym).procdef_count>1 then
  893. Message(asmr_w_calling_overload_func);
  894. l:=opr.ref.offset;
  895. opr.typ:=OPR_SYMBOL;
  896. opr.symbol:=objectlibrary.newasmsymbol(tprocsym(sym).first_procdef.mangledname,AB_EXTERNAL,AT_FUNCTION);
  897. opr.symofs:=l;
  898. hasvar:=true;
  899. SetupVar:=TRUE;
  900. Exit;
  901. end;
  902. else
  903. begin
  904. Message(asmr_e_unsupported_symbol_type);
  905. exit;
  906. end;
  907. end;
  908. end;
  909. { looks for internal names of variables and routines }
  910. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  911. var
  912. p : tasmsymbol;
  913. begin
  914. SetupDirectVar:=false;
  915. p:=objectlibrary.getasmsymbol(hs);
  916. if assigned(p) then
  917. begin
  918. InitRef;
  919. opr.ref.symbol:=p;
  920. hasvar:=true;
  921. SetupDirectVar:=true;
  922. end;
  923. end;
  924. procedure TOperand.InitRef;
  925. {*********************************************************************}
  926. { Description: This routine first check if the opcode is of }
  927. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  928. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  929. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  930. { to point to the default segment. }
  931. {*********************************************************************}
  932. var
  933. l : aint;
  934. reg : tregister;
  935. Begin
  936. case opr.typ of
  937. OPR_REFERENCE :
  938. exit;
  939. OPR_CONSTANT :
  940. begin
  941. l:=opr.val;
  942. opr.typ:=OPR_REFERENCE;
  943. Fillchar(opr.ref,sizeof(treference),0);
  944. opr.Ref.Offset:=l;
  945. end;
  946. OPR_NONE :
  947. begin
  948. opr.typ:=OPR_REFERENCE;
  949. Fillchar(opr.ref,sizeof(treference),0);
  950. end;
  951. OPR_REGISTER :
  952. begin
  953. reg:=opr.reg;
  954. opr.typ:=OPR_REFERENCE;
  955. Fillchar(opr.ref,sizeof(treference),0);
  956. opr.Ref.base:=reg;
  957. end;
  958. else
  959. begin
  960. Message(asmr_e_invalid_operand_type);
  961. { Recover }
  962. opr.typ:=OPR_REFERENCE;
  963. Fillchar(opr.ref,sizeof(treference),0);
  964. end;
  965. end;
  966. end;
  967. {****************************************************************************
  968. TInstruction
  969. ****************************************************************************}
  970. constructor TInstruction.create(optype : tcoperand);
  971. var
  972. i : longint;
  973. Begin
  974. { these field are set to 0 anyways by the constructor helper (FK)
  975. Opcode:=A_NONE;
  976. Condition:=C_NONE;
  977. Ops:=0;
  978. }
  979. for i:=1 to max_operands do
  980. Operands[i]:=optype.create;
  981. Labeled:=false;
  982. end;
  983. destructor TInstruction.destroy;
  984. var
  985. i : longint;
  986. Begin
  987. for i:=1 to max_operands do
  988. Operands[i].free;
  989. end;
  990. Procedure TInstruction.Swapoperands;
  991. Var
  992. p : toperand;
  993. Begin
  994. case Ops of
  995. 2 :
  996. begin
  997. p:=Operands[1];
  998. Operands[1]:=Operands[2];
  999. Operands[2]:=p;
  1000. end;
  1001. 3 :
  1002. begin
  1003. p:=Operands[1];
  1004. Operands[1]:=Operands[3];
  1005. Operands[3]:=p;
  1006. end;
  1007. end;
  1008. end;
  1009. function TInstruction.ConcatInstruction(p:TAAsmoutput) : tai;
  1010. var
  1011. ai : taicpu;
  1012. i : longint;
  1013. begin
  1014. ai:=taicpu.op_none(opcode);
  1015. ai.Ops:=Ops;
  1016. ai.Allocate_oper(Ops);
  1017. for i:=1 to Ops do
  1018. with operands[i].opr do
  1019. begin
  1020. case typ of
  1021. OPR_CONSTANT :
  1022. ai.loadconst(i-1,val);
  1023. OPR_REGISTER:
  1024. ai.loadreg(i-1,reg);
  1025. OPR_SYMBOL:
  1026. ai.loadsymbol(i-1,symbol,symofs);
  1027. OPR_LOCAL :
  1028. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1029. localscale,localgetoffset);
  1030. OPR_REFERENCE:
  1031. ai.loadref(i-1,ref);
  1032. {$ifdef ARM}
  1033. OPR_REGSET:
  1034. ai.loadregset(i-1,regset);
  1035. {$endif ARM}
  1036. end;
  1037. end;
  1038. ai.SetCondition(condition);
  1039. { Concat the opcode or give an error }
  1040. if assigned(ai) then
  1041. p.concat(ai)
  1042. else
  1043. Message(asmr_e_invalid_opcode_and_operand);
  1044. result:=ai;
  1045. end;
  1046. {***************************************************************************
  1047. TLocalLabel
  1048. ***************************************************************************}
  1049. constructor TLocalLabel.create(const n:string);
  1050. begin
  1051. inherited CreateName(n);
  1052. lab:=nil;
  1053. emitted:=false;
  1054. end;
  1055. function TLocalLabel.Gettasmlabel:tasmlabel;
  1056. begin
  1057. if not assigned(lab) then
  1058. begin
  1059. objectlibrary.getlabel(lab);
  1060. { this label is forced to be used so it's always written }
  1061. lab.increfs;
  1062. end;
  1063. Gettasmlabel:=lab;
  1064. end;
  1065. {***************************************************************************
  1066. TLocalLabelList
  1067. ***************************************************************************}
  1068. procedure LocalLabelEmitted(p:tnamedindexitem;arg:pointer);
  1069. begin
  1070. if not TLocalLabel(p).emitted then
  1071. Message1(asmr_e_unknown_label_identifier,p.name);
  1072. end;
  1073. procedure TLocalLabelList.CheckEmitted;
  1074. begin
  1075. ForEach_Static(@LocalLabelEmitted,nil)
  1076. end;
  1077. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1078. var
  1079. lab : TLocalLabel;
  1080. Begin
  1081. CreateLocalLabel:=true;
  1082. { Check if it already is defined }
  1083. lab:=TLocalLabel(LocalLabellist.Search(s));
  1084. if not assigned(lab) then
  1085. begin
  1086. lab:=TLocalLabel.Create(s);
  1087. LocalLabellist.Insert(lab);
  1088. end;
  1089. { set emitted flag and check for dup syms }
  1090. if emit then
  1091. begin
  1092. if lab.Emitted then
  1093. begin
  1094. Message1(asmr_e_dup_local_sym,lab.Name);
  1095. CreateLocalLabel:=false;
  1096. end;
  1097. lab.Emitted:=true;
  1098. end;
  1099. hl:=lab.Gettasmlabel;
  1100. end;
  1101. {****************************************************************************
  1102. Symbol table helper routines
  1103. ****************************************************************************}
  1104. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1105. var
  1106. i : integer;
  1107. begin
  1108. i:=pos('.',s);
  1109. { allow unit.identifier }
  1110. if i>0 then
  1111. begin
  1112. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1113. if assigned(srsym) then
  1114. begin
  1115. if (srsym.typ=unitsym) and
  1116. (srsym.owner.unitid=0) then
  1117. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1118. else
  1119. srsym:=nil;
  1120. end;
  1121. end
  1122. else
  1123. searchsym(s,srsym,srsymtable);
  1124. end;
  1125. Function SearchType(const hs:string;var size:aint): Boolean;
  1126. var
  1127. srsym : tsym;
  1128. srsymtable : tsymtable;
  1129. begin
  1130. result:=false;
  1131. size:=0;
  1132. asmsearchsym(hs,srsym,srsymtable);
  1133. if assigned(srsym) and
  1134. (srsym.typ=typesym) then
  1135. begin
  1136. size:=ttypesym(srsym).restype.def.size;
  1137. result:=true;
  1138. end;
  1139. end;
  1140. Function SearchRecordType(const s:string): boolean;
  1141. var
  1142. srsym : tsym;
  1143. srsymtable : tsymtable;
  1144. Begin
  1145. SearchRecordType:=false;
  1146. { Check the constants in symtable }
  1147. asmsearchsym(s,srsym,srsymtable);
  1148. if srsym <> nil then
  1149. Begin
  1150. case srsym.typ of
  1151. typesym :
  1152. begin
  1153. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1154. begin
  1155. SearchRecordType:=true;
  1156. exit;
  1157. end;
  1158. end;
  1159. end;
  1160. end;
  1161. end;
  1162. Function SearchIConstant(const s:string; var l:aint): boolean;
  1163. {**********************************************************************}
  1164. { Description: Searches for a CONSTANT of name s in either the local }
  1165. { symbol list, then in the global symbol list, and returns the value }
  1166. { of that constant in l. Returns TRUE if successfull, if not found, }
  1167. { or if the constant is not of correct type, then returns FALSE }
  1168. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1169. { respectively. }
  1170. {**********************************************************************}
  1171. var
  1172. srsym : tsym;
  1173. srsymtable : tsymtable;
  1174. Begin
  1175. SearchIConstant:=false;
  1176. { check for TRUE or FALSE reserved words first }
  1177. if s = 'TRUE' then
  1178. Begin
  1179. SearchIConstant:=TRUE;
  1180. l:=1;
  1181. exit;
  1182. end;
  1183. if s = 'FALSE' then
  1184. Begin
  1185. SearchIConstant:=TRUE;
  1186. l:=0;
  1187. exit;
  1188. end;
  1189. { Check the constants in symtable }
  1190. asmsearchsym(s,srsym,srsymtable);
  1191. if srsym <> nil then
  1192. Begin
  1193. case srsym.typ of
  1194. constsym :
  1195. begin
  1196. if tconstsym(srsym).consttyp=constord then
  1197. Begin
  1198. l:=tconstsym(srsym).value.valueord;
  1199. SearchIConstant:=TRUE;
  1200. exit;
  1201. end;
  1202. end;
  1203. enumsym:
  1204. Begin
  1205. l:=tenumsym(srsym).value;
  1206. SearchIConstant:=TRUE;
  1207. exit;
  1208. end;
  1209. end;
  1210. end;
  1211. end;
  1212. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint):boolean;
  1213. { search and returns the offset and size of records/objects of the base }
  1214. { with field name setup in field. }
  1215. { returns FALSE if not found. }
  1216. { used when base is a variable or a typed constant name. }
  1217. var
  1218. st : tsymtable;
  1219. harrdef : tarraydef;
  1220. sym : tsym;
  1221. srsymtable : tsymtable;
  1222. i : longint;
  1223. base : string;
  1224. Begin
  1225. GetRecordOffsetSize:=FALSE;
  1226. Offset:=0;
  1227. Size:=0;
  1228. i:=pos('.',s);
  1229. if i=0 then
  1230. i:=255;
  1231. base:=Copy(s,1,i-1);
  1232. delete(s,1,i);
  1233. if base='SELF' then
  1234. st:=current_procinfo.procdef._class.symtable
  1235. else
  1236. begin
  1237. asmsearchsym(base,sym,srsymtable);
  1238. st:=nil;
  1239. { we can start with a var,type,typedconst }
  1240. if assigned(sym) then
  1241. case sym.typ of
  1242. varsym :
  1243. with Tvarsym(sym).vartype do
  1244. case def.deftype of
  1245. recorddef :
  1246. st:=trecorddef(def).symtable;
  1247. objectdef :
  1248. st:=tobjectdef(def).symtable;
  1249. end;
  1250. typesym :
  1251. with Ttypesym(sym).restype do
  1252. case def.deftype of
  1253. recorddef :
  1254. st:=trecorddef(def).symtable;
  1255. objectdef :
  1256. st:=tobjectdef(def).symtable;
  1257. end;
  1258. typedconstsym :
  1259. with Ttypedconstsym(sym).typedconsttype do
  1260. case def.deftype of
  1261. recorddef :
  1262. st:=trecorddef(def).symtable;
  1263. objectdef :
  1264. st:=tobjectdef(def).symtable;
  1265. end;
  1266. end
  1267. else
  1268. s:='';
  1269. end;
  1270. { now walk all recordsymtables }
  1271. while assigned(st) and (s<>'') do
  1272. begin
  1273. { load next field in base }
  1274. i:=pos('.',s);
  1275. if i=0 then
  1276. i:=255;
  1277. base:=Copy(s,1,i-1);
  1278. delete(s,1,i);
  1279. if st.symtabletype=objectsymtable then
  1280. sym:=search_class_member(tobjectdef(st.defowner),base)
  1281. else
  1282. sym:=tsym(st.search(base));
  1283. if not assigned(sym) then
  1284. begin
  1285. GetRecordOffsetSize:=false;
  1286. exit;
  1287. end;
  1288. st:=nil;
  1289. case sym.typ of
  1290. varsym :
  1291. with Tvarsym(sym) do
  1292. begin
  1293. inc(Offset,fieldoffset);
  1294. size:=getsize;
  1295. with vartype do
  1296. case def.deftype of
  1297. arraydef :
  1298. begin
  1299. { for arrays try to get the element size, take care of
  1300. multiple indexes }
  1301. harrdef:=tarraydef(def);
  1302. while assigned(harrdef.elementtype.def) and
  1303. (harrdef.elementtype.def.deftype=arraydef) do
  1304. harrdef:=tarraydef(harrdef.elementtype.def);
  1305. size:=harrdef.elesize;
  1306. end;
  1307. recorddef :
  1308. st:=trecorddef(def).symtable;
  1309. objectdef :
  1310. st:=tobjectdef(def).symtable;
  1311. end;
  1312. end;
  1313. end;
  1314. end;
  1315. GetRecordOffsetSize:=(s='');
  1316. end;
  1317. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1318. var
  1319. sym : tsym;
  1320. srsymtable : tsymtable;
  1321. hs : string;
  1322. Begin
  1323. hl:=nil;
  1324. SearchLabel:=false;
  1325. { Check for pascal labels, which are case insensetive }
  1326. hs:=upper(s);
  1327. asmsearchsym(hs,sym,srsymtable);
  1328. if sym=nil then
  1329. exit;
  1330. case sym.typ of
  1331. labelsym :
  1332. begin
  1333. hl:=tlabelsym(sym).lab;
  1334. if emit then
  1335. tlabelsym(sym).defined:=true
  1336. else
  1337. tlabelsym(sym).used:=true;
  1338. SearchLabel:=true;
  1339. end;
  1340. end;
  1341. end;
  1342. {*************************************************************************}
  1343. { Instruction Generation Utilities }
  1344. {*************************************************************************}
  1345. Procedure ConcatString(p : TAAsmoutput;s:string);
  1346. {*********************************************************************}
  1347. { PROCEDURE ConcatString(s:string); }
  1348. { Description: This routine adds the character chain pointed to in }
  1349. { s to the instruction linked list. }
  1350. {*********************************************************************}
  1351. Var
  1352. pc: PChar;
  1353. Begin
  1354. getmem(pc,length(s)+1);
  1355. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1356. end;
  1357. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1358. {*********************************************************************}
  1359. { PROCEDURE ConcatPasString(s:string); }
  1360. { Description: This routine adds the character chain pointed to in }
  1361. { s to the instruction linked list, contrary to ConcatString it }
  1362. { uses a pascal style string, so it conserves null characters. }
  1363. {*********************************************************************}
  1364. Begin
  1365. p.concat(Tai_string.Create(s));
  1366. end;
  1367. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1368. {*********************************************************************}
  1369. { PROCEDURE ConcatDirect(s:string) }
  1370. { Description: This routine output the string directly to the asm }
  1371. { output, it is only sed when writing special labels in AT&T mode, }
  1372. { and should not be used without due consideration, since it may }
  1373. { cause problems. }
  1374. {*********************************************************************}
  1375. Var
  1376. pc: PChar;
  1377. Begin
  1378. getmem(pc,length(s)+1);
  1379. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1380. end;
  1381. Procedure ConcatConstant(p: TAAsmoutput; value: aint; constsize:byte);
  1382. {*********************************************************************}
  1383. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1384. { Description: This routine adds the value constant to the current }
  1385. { instruction linked list. }
  1386. { maxvalue -> indicates the size of the data to initialize: }
  1387. { $ff -> create a byte node. }
  1388. { $ffff -> create a word node. }
  1389. { $ffffffff -> create a dword node. }
  1390. {*********************************************************************}
  1391. var
  1392. rangelo,rangehi : int64;
  1393. Begin
  1394. case constsize of
  1395. 1 :
  1396. begin
  1397. p.concat(Tai_const.Create_8bit(byte(value)));
  1398. rangelo:=low(shortint);
  1399. rangehi:=high(byte);
  1400. end;
  1401. 2 :
  1402. begin
  1403. p.concat(Tai_const.Create_16bit(word(value)));
  1404. rangelo:=low(smallint);
  1405. rangehi:=high(word);
  1406. end;
  1407. 4 :
  1408. begin
  1409. p.concat(Tai_const.Create_32bit(longint(value)));
  1410. rangelo:=low(longint);
  1411. rangehi:=high(cardinal);
  1412. end;
  1413. 8 :
  1414. begin
  1415. p.concat(Tai_const.Create_64bit(int64(value)));
  1416. rangelo:=0;
  1417. rangehi:=0;
  1418. end;
  1419. else
  1420. internalerror(200405011);
  1421. end;
  1422. { check for out of bounds }
  1423. if (rangelo<>0) and
  1424. ((value>rangehi) or (value<rangelo)) then
  1425. Message(asmr_e_constant_out_of_bounds);
  1426. end;
  1427. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:aint);
  1428. begin
  1429. p.concat(Tai_const.Createname(sym,AT_DATA,l));
  1430. end;
  1431. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1432. {***********************************************************************}
  1433. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1434. { Description: This routine adds the value constant to the current }
  1435. { instruction linked list. }
  1436. { real_typ -> indicates the type of the real data to initialize: }
  1437. { s32real -> create a single node. }
  1438. { s64real -> create a double node. }
  1439. { s80real -> create an extended node. }
  1440. { s64bit -> create a comp node. }
  1441. { f32bit -> create a fixed node. (not used normally) }
  1442. {***********************************************************************}
  1443. Begin
  1444. case real_typ of
  1445. s32real : p.concat(Tai_real_32bit.Create(value));
  1446. s64real :
  1447. {$ifdef ARM}
  1448. if aktfputype in [fpu_fpa,fpu_fpa10,fpu_fpa11] then
  1449. p.concat(Tai_real_64bit.Create_hiloswapped(value))
  1450. else
  1451. {$endif ARM}
  1452. p.concat(Tai_real_64bit.Create(value));
  1453. s80real : p.concat(Tai_real_80bit.Create(value));
  1454. s64comp : p.concat(Tai_comp_64bit.Create(trunc(value)));
  1455. end;
  1456. end;
  1457. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1458. {*********************************************************************}
  1459. { PROCEDURE ConcatLabel }
  1460. { Description: This routine either emits a label or a labeled }
  1461. { instruction to the linked list of instructions. }
  1462. {*********************************************************************}
  1463. begin
  1464. p.concat(Tai_label.Create(l));
  1465. end;
  1466. procedure ConcatAlign(p:TAAsmoutput;l:aint);
  1467. {*********************************************************************}
  1468. { PROCEDURE ConcatPublic }
  1469. { Description: This routine emits an global definition to the }
  1470. { linked list of instructions.(used by AT&T styled asm) }
  1471. {*********************************************************************}
  1472. begin
  1473. p.concat(Tai_align.Create(l));
  1474. end;
  1475. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1476. {*********************************************************************}
  1477. { PROCEDURE ConcatPublic }
  1478. { Description: This routine emits an global definition to the }
  1479. { linked list of instructions.(used by AT&T styled asm) }
  1480. {*********************************************************************}
  1481. begin
  1482. p.concat(Tai_symbol.Createname_global(s,AT_FUNCTION,0));
  1483. end;
  1484. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1485. {*********************************************************************}
  1486. { PROCEDURE ConcatLocal }
  1487. { Description: This routine emits an local definition to the }
  1488. { linked list of instructions. }
  1489. {*********************************************************************}
  1490. begin
  1491. p.concat(Tai_symbol.Createname(s,AT_FUNCTION,0));
  1492. end;
  1493. Procedure ConcatGlobalBss(const s : string;size : aint);
  1494. {*********************************************************************}
  1495. { PROCEDURE ConcatGlobalBss }
  1496. { Description: This routine emits an global datablock to the }
  1497. { linked list of instructions. }
  1498. {*********************************************************************}
  1499. begin
  1500. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1501. end;
  1502. Procedure ConcatLocalBss(const s : string;size : aint);
  1503. {*********************************************************************}
  1504. { PROCEDURE ConcatLocalBss }
  1505. { Description: This routine emits a local datablcok to the }
  1506. { linked list of instructions. }
  1507. {*********************************************************************}
  1508. begin
  1509. bssSegment.concat(Tai_datablock.Create(s,size));
  1510. end;
  1511. end.
  1512. {
  1513. $Log$
  1514. Revision 1.92 2004-10-24 11:44:28 peter
  1515. * small regvar fixes
  1516. * loadref parameter removed from concatcopy,incrrefcount,etc
  1517. Revision 1.91 2004/10/15 09:14:17 mazen
  1518. - remove $IFDEF DELPHI and related code
  1519. - remove $IFDEF FPCPROCVAR and related code
  1520. Revision 1.90 2004/10/08 17:09:43 peter
  1521. * tvarsym.varregable added, split vo_regable from varoptions
  1522. Revision 1.89 2004/09/27 15:14:34 peter
  1523. * reference local varsyms can't be regvar
  1524. Revision 1.88 2004/06/20 08:55:30 florian
  1525. * logs truncated
  1526. Revision 1.87 2004/06/16 20:07:09 florian
  1527. * dwarf branch merged
  1528. Revision 1.86 2004/05/06 20:30:51 florian
  1529. * m68k compiler compilation fixed
  1530. Revision 1.85.2.7 2004/05/25 21:38:53 peter
  1531. * assembler reader/writer updates
  1532. Revision 1.85.2.6 2004/05/18 20:24:03 florian
  1533. * fixed crash with unknown symbols
  1534. Revision 1.85.2.5 2004/05/02 00:13:16 peter
  1535. * remove limit of 15 digits
  1536. }