rautils.pas 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  20. Interface
  21. Uses
  22. cutils,cclasses,
  23. globtype,aasm,cpubase,
  24. symconst,symbase,symtype,symdef;
  25. Const
  26. RPNMax = 10; { I think you only need 4, but just to be safe }
  27. OpMax = 25;
  28. maxoperands = 3; { Maximum operands for assembler instructions }
  29. {---------------------------------------------------------------------
  30. Local Label Management
  31. ---------------------------------------------------------------------}
  32. Type
  33. { Each local label has this structure associated with it }
  34. TLocalLabel = class(TNamedIndexItem)
  35. Emitted : boolean;
  36. constructor Create(const n:string);
  37. function Gettasmlabel:tasmlabel;
  38. private
  39. lab : tasmlabel;
  40. end;
  41. TLocalLabelList = class(TDictionary)
  42. procedure CheckEmitted;
  43. end;
  44. var
  45. LocalLabelList : TLocalLabelList;
  46. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  47. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  48. {---------------------------------------------------------------------
  49. Instruction management
  50. ---------------------------------------------------------------------}
  51. type
  52. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,
  53. OPR_REFERENCE,OPR_REGISTER,OPR_REGLIST);
  54. TOprRec = record
  55. case typ:TOprType of
  56. OPR_NONE : ();
  57. OPR_CONSTANT : (val:longint);
  58. OPR_SYMBOL : (symbol:tasmsymbol;symofs:longint);
  59. OPR_REFERENCE : (ref:treference);
  60. OPR_REGISTER : (reg:tregister);
  61. {$ifdef m68k}
  62. OPR_REGLIST : (reglist:pregisterlist);
  63. {$else not m68k}
  64. OPR_REGLIST : ();
  65. {$endif m68k}
  66. end;
  67. TOperand = class
  68. size : topsize;
  69. hastype, { if the operand has typecasted variable }
  70. hasvar : boolean; { if the operand is loaded with a variable }
  71. opr : TOprRec;
  72. constructor create;
  73. destructor destroy;override;
  74. Procedure BuildOperand;virtual;
  75. Procedure SetSize(_size:longint;force:boolean);
  76. Procedure SetCorrectSize(opcode:tasmop);virtual;
  77. Function SetupResult:boolean;virtual;
  78. Function SetupSelf:boolean;
  79. Function SetupOldEBP:boolean;
  80. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  81. Function SetupDirectVar(const hs:string): Boolean;
  82. Procedure InitRef;
  83. end;
  84. TInstruction = class
  85. opcode : tasmop;
  86. opsize : topsize;
  87. condition : tasmcond;
  88. ops : byte;
  89. labeled : boolean;
  90. operands : array[1..maxoperands] of toperand;
  91. constructor create;
  92. destructor destroy;override;
  93. Procedure InitOperands;virtual;
  94. Procedure BuildOpcode;virtual;
  95. procedure ConcatInstruction(p:TAAsmoutput);virtual;
  96. Procedure Swatoperands;
  97. end;
  98. {---------------------------------------------------------------------}
  99. { Expression parser types }
  100. {---------------------------------------------------------------------}
  101. TExprOperator = record
  102. ch: char; { operator }
  103. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  104. end;
  105. String15 = String[15];
  106. {**********************************************************************}
  107. { The following operators are supported: }
  108. { '+' : addition }
  109. { '-' : subtraction }
  110. { '*' : multiplication }
  111. { '/' : modulo division }
  112. { '^' : exclusive or }
  113. { '<' : shift left }
  114. { '>' : shift right }
  115. { '&' : bitwise and }
  116. { '|' : bitwise or }
  117. { '~' : bitwise complement }
  118. { '%' : modulo division }
  119. { nnn: longint numbers }
  120. { ( and ) parenthesis }
  121. {**********************************************************************}
  122. TExprParse = class
  123. public
  124. Constructor create;
  125. Destructor Destroy;override;
  126. Function Evaluate(Expr: String): longint;
  127. Function Priority(_Operator: Char): longint;
  128. private
  129. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  130. RPNTop : longint;
  131. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  132. OpTop : longint;
  133. Procedure RPNPush(Num: Longint);
  134. Function RPNPop: Longint;
  135. Procedure RPNCalc(token: String15; prefix: boolean);
  136. Procedure OpPush(_Operator: char; prefix: boolean);
  137. { In reality returns TExprOperaotr }
  138. Procedure OpPop(var _Operator:TExprOperator);
  139. end;
  140. { Evaluate an expression string to a longint }
  141. Function CalculateExpression(const expression: string): longint;
  142. {---------------------------------------------------------------------}
  143. { String routines }
  144. {---------------------------------------------------------------------}
  145. Function ValDecimal(const S:String):longint;
  146. Function ValOctal(const S:String):longint;
  147. Function ValBinary(const S:String):longint;
  148. Function ValHexaDecimal(const S:String):longint;
  149. Function PadZero(Var s: String; n: byte): Boolean;
  150. Function EscapeToPascal(const s:string): string;
  151. {---------------------------------------------------------------------
  152. Symbol helper routines
  153. ---------------------------------------------------------------------}
  154. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  155. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  156. Function SearchType(const hs:string): Boolean;
  157. Function SearchRecordType(const s:string): boolean;
  158. Function SearchIConstant(const s:string; var l:longint): boolean;
  159. {---------------------------------------------------------------------
  160. Instruction generation routines
  161. ---------------------------------------------------------------------}
  162. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  163. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  164. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  165. Procedure ConcatConstant(p : TAAsmoutput;value: longint; maxvalue: longint);
  166. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  167. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  168. Procedure ConcatString(p : TAAsmoutput;s:string);
  169. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  170. Procedure ConcatPublic(p:TAAsmoutput;const s : string);
  171. Procedure ConcatLocal(p:TAAsmoutput;const s : string);
  172. Procedure ConcatGlobalBss(const s : string;size : longint);
  173. Procedure ConcatLocalBss(const s : string;size : longint);
  174. Implementation
  175. uses
  176. {$ifdef delphi}
  177. sysutils,
  178. {$else}
  179. strings,
  180. {$endif}
  181. types,systems,verbose,globals,
  182. symsym,symtable,cpuasm,
  183. cgbase;
  184. {*************************************************************************
  185. TExprParse
  186. *************************************************************************}
  187. Constructor TExprParse.create;
  188. Begin
  189. end;
  190. Procedure TExprParse.RPNPush(Num : longint);
  191. { Add an operand to the top of the RPN stack }
  192. begin
  193. if RPNTop < RPNMax then
  194. begin
  195. Inc(RPNTop);
  196. RPNStack[RPNTop]:=Num;
  197. end
  198. else
  199. Message(asmr_e_expr_illegal);
  200. end;
  201. Function TExprParse.RPNPop : longint;
  202. { Get the operand at the top of the RPN stack }
  203. begin
  204. if RPNTop > 0 then
  205. begin
  206. RPNPop:=RPNStack[RPNTop];
  207. Dec(RPNTop);
  208. end
  209. else
  210. Message(asmr_e_expr_illegal);
  211. end;
  212. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  213. Var
  214. Temp : longint;
  215. n1,n2 : longint;
  216. LocalError : Integer;
  217. begin
  218. { Handle operators }
  219. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  220. Case Token[1] of
  221. '+' :
  222. Begin
  223. if not prefix then
  224. RPNPush(RPNPop + RPNPop);
  225. end;
  226. '-' :
  227. Begin
  228. if prefix then
  229. RPNPush(-(RPNPop))
  230. else
  231. begin
  232. n1:=RPNPop;
  233. n2:=RPNPop;
  234. RPNPush(n2 - n1);
  235. end;
  236. end;
  237. '*' : RPNPush(RPNPop * RPNPop);
  238. '&' :
  239. begin
  240. n1:=RPNPop;
  241. n2:=RPNPop;
  242. RPNPush(n2 and n1);
  243. end;
  244. '|' :
  245. begin
  246. n1:=RPNPop;
  247. n2:=RPNPop;
  248. RPNPush(n2 or n1);
  249. end;
  250. '~' : RPNPush(NOT RPNPop);
  251. '<' :
  252. begin
  253. n1:=RPNPop;
  254. n2:=RPNPop;
  255. RPNPush(n2 SHL n1);
  256. end;
  257. '>' :
  258. begin
  259. n1:=RPNPop;
  260. n2:=RPNPop;
  261. RPNPush(n2 SHR n1);
  262. end;
  263. '%' :
  264. begin
  265. Temp:=RPNPop;
  266. if Temp <> 0 then
  267. RPNPush(RPNPop mod Temp)
  268. else
  269. begin
  270. Message(asmr_e_expr_zero_divide);
  271. { push 1 for error recovery }
  272. RPNPush(1);
  273. end;
  274. end;
  275. '^' : RPNPush(RPNPop XOR RPNPop);
  276. '/' :
  277. begin
  278. Temp:=RPNPop;
  279. if Temp <> 0 then
  280. RPNPush(RPNPop div Temp)
  281. else
  282. begin
  283. Message(asmr_e_expr_zero_divide);
  284. { push 1 for error recovery }
  285. RPNPush(1);
  286. end;
  287. end;
  288. end
  289. else
  290. begin
  291. { Convert String to number and add to stack }
  292. Val(Token, Temp, LocalError);
  293. if LocalError = 0 then
  294. RPNPush(Temp)
  295. else
  296. begin
  297. Message(asmr_e_expr_illegal);
  298. { push 1 for error recovery }
  299. RPNPush(1);
  300. end;
  301. end;
  302. end;
  303. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  304. { Add an operator onto top of the stack }
  305. begin
  306. if OpTop < OpMax then
  307. begin
  308. Inc(OpTop);
  309. OpStack[OpTop].ch:=_Operator;
  310. OpStack[OpTop].is_prefix:=prefix;
  311. end
  312. else
  313. Message(asmr_e_expr_illegal);
  314. end;
  315. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  316. { Get operator at the top of the stack }
  317. begin
  318. if OpTop > 0 then
  319. begin
  320. _Operator:=OpStack[OpTop];
  321. Dec(OpTop);
  322. end
  323. else
  324. Message(asmr_e_expr_illegal);
  325. end;
  326. Function TExprParse.Priority(_Operator : Char) : longint;
  327. { Return priority of operator }
  328. { The greater the priority, the higher the precedence }
  329. begin
  330. Case _Operator OF
  331. '(' :
  332. Priority:=0;
  333. '+', '-' :
  334. Priority:=1;
  335. '*', '/','%','<','>' :
  336. Priority:=2;
  337. '|','&','^','~' :
  338. Priority:=0;
  339. else
  340. Message(asmr_e_expr_illegal);
  341. end;
  342. end;
  343. Function TExprParse.Evaluate(Expr : String):longint;
  344. Var
  345. I : LongInt;
  346. Token : String15;
  347. opr : TExprOperator;
  348. begin
  349. Evaluate:=0;
  350. { Reset stacks }
  351. OpTop :=0;
  352. RPNTop:=0;
  353. Token :='';
  354. { nothing to do ? }
  355. if Expr='' then
  356. exit;
  357. For I:=1 to Length(Expr) DO
  358. begin
  359. if Expr[I] in ['0'..'9'] then
  360. begin { Build multi-digit numbers }
  361. Token:=Token + Expr[I];
  362. if I = Length(Expr) then { Send last one to calculator }
  363. RPNCalc(Token,false);
  364. end
  365. else
  366. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  367. begin
  368. if Token <> '' then
  369. begin { Send last built number to calc. }
  370. RPNCalc(Token,false);
  371. Token:='';
  372. end;
  373. Case Expr[I] OF
  374. '(' : OpPush('(',false);
  375. ')' : begin
  376. While OpStack[OpTop].ch <> '(' DO
  377. Begin
  378. OpPop(opr);
  379. RPNCalc(opr.ch,opr.is_prefix);
  380. end;
  381. OpPop(opr); { Pop off and ignore the '(' }
  382. end;
  383. '+','-','~' : Begin
  384. { workaround for -2147483648 }
  385. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  386. begin
  387. token:='-';
  388. expr[i]:='+';
  389. end;
  390. { if start of expression then surely a prefix }
  391. { or if previous char was also an operator }
  392. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  393. OpPush(Expr[I],true)
  394. else
  395. Begin
  396. { Evaluate all higher priority operators }
  397. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  398. Begin
  399. OpPop(opr);
  400. RPNCalc(opr.ch,opr.is_prefix);
  401. end;
  402. OpPush(Expr[I],false);
  403. End;
  404. end;
  405. '*', '/',
  406. '^','|','&',
  407. '%','<','>' : begin
  408. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  409. Begin
  410. OpPop(opr);
  411. RPNCalc(opr.ch,opr.is_prefix);
  412. end;
  413. OpPush(Expr[I],false);
  414. end;
  415. end; { Case }
  416. end
  417. else
  418. Message(asmr_e_expr_illegal); { Handle bad input error }
  419. end;
  420. { Pop off the remaining operators }
  421. While OpTop > 0 do
  422. Begin
  423. OpPop(opr);
  424. RPNCalc(opr.ch,opr.is_prefix);
  425. end;
  426. { The result is stored on the top of the stack }
  427. Evaluate:=RPNPop;
  428. end;
  429. Destructor TExprParse.Destroy;
  430. Begin
  431. end;
  432. Function CalculateExpression(const expression: string): longint;
  433. var
  434. expr: TExprParse;
  435. Begin
  436. expr:=TExprParse.create;
  437. CalculateExpression:=expr.Evaluate(expression);
  438. expr.Free;
  439. end;
  440. {*************************************************************************}
  441. { String conversions/utils }
  442. {*************************************************************************}
  443. Function EscapeToPascal(const s:string): string;
  444. { converts a C styled string - which contains escape }
  445. { characters to a pascal style string. }
  446. var
  447. i,len : longint;
  448. hs : string;
  449. temp : string;
  450. c : char;
  451. Begin
  452. hs:='';
  453. len:=0;
  454. i:=0;
  455. while (i<length(s)) and (len<255) do
  456. begin
  457. Inc(i);
  458. if (s[i]='\') and (i<length(s)) then
  459. Begin
  460. inc(i);
  461. case s[i] of
  462. '\' :
  463. c:='\';
  464. 'b':
  465. c:=#8;
  466. 'f':
  467. c:=#12;
  468. 'n':
  469. c:=#10;
  470. 'r':
  471. c:=#13;
  472. 't':
  473. c:=#9;
  474. '"':
  475. c:='"';
  476. '0'..'7':
  477. Begin
  478. temp:=s[i];
  479. temp:=temp+s[i+1];
  480. temp:=temp+s[i+2];
  481. inc(i,2);
  482. c:=chr(ValOctal(temp));
  483. end;
  484. 'x':
  485. Begin
  486. temp:=s[i+1];
  487. temp:=temp+s[i+2];
  488. inc(i,2);
  489. c:=chr(ValHexaDecimal(temp));
  490. end;
  491. else
  492. Begin
  493. Message1(asmr_e_escape_seq_ignored,s[i]);
  494. c:=s[i];
  495. end;
  496. end;
  497. end
  498. else
  499. c:=s[i];
  500. inc(len);
  501. hs[len]:=c;
  502. end;
  503. hs[0]:=chr(len);
  504. EscapeToPascal:=hs;
  505. end;
  506. Function ValDecimal(const S:String):longint;
  507. { Converts a decimal string to longint }
  508. var
  509. vs,c : longint;
  510. Begin
  511. vs:=0;
  512. for c:=1 to length(s) do
  513. begin
  514. vs:=vs*10;
  515. if s[c] in ['0'..'9'] then
  516. inc(vs,ord(s[c])-ord('0'))
  517. else
  518. begin
  519. Message1(asmr_e_error_converting_decimal,s);
  520. ValDecimal:=0;
  521. exit;
  522. end;
  523. end;
  524. ValDecimal:=vs;
  525. end;
  526. Function ValOctal(const S:String):longint;
  527. { Converts an octal string to longint }
  528. var
  529. vs,c : longint;
  530. Begin
  531. vs:=0;
  532. for c:=1 to length(s) do
  533. begin
  534. vs:=vs shl 3;
  535. if s[c] in ['0'..'7'] then
  536. inc(vs,ord(s[c])-ord('0'))
  537. else
  538. begin
  539. Message1(asmr_e_error_converting_octal,s);
  540. ValOctal:=0;
  541. exit;
  542. end;
  543. end;
  544. ValOctal:=vs;
  545. end;
  546. Function ValBinary(const S:String):longint;
  547. { Converts a binary string to longint }
  548. var
  549. vs,c : longint;
  550. Begin
  551. vs:=0;
  552. for c:=1 to length(s) do
  553. begin
  554. vs:=vs shl 1;
  555. if s[c] in ['0'..'1'] then
  556. inc(vs,ord(s[c])-ord('0'))
  557. else
  558. begin
  559. Message1(asmr_e_error_converting_binary,s);
  560. ValBinary:=0;
  561. exit;
  562. end;
  563. end;
  564. ValBinary:=vs;
  565. end;
  566. Function ValHexadecimal(const S:String):longint;
  567. { Converts a binary string to longint }
  568. var
  569. vs,c : longint;
  570. Begin
  571. vs:=0;
  572. for c:=1 to length(s) do
  573. begin
  574. vs:=vs shl 4;
  575. case s[c] of
  576. '0'..'9' :
  577. inc(vs,ord(s[c])-ord('0'));
  578. 'A'..'F' :
  579. inc(vs,ord(s[c])-ord('A')+10);
  580. 'a'..'f' :
  581. inc(vs,ord(s[c])-ord('a')+10);
  582. else
  583. begin
  584. Message1(asmr_e_error_converting_hexadecimal,s);
  585. ValHexadecimal:=0;
  586. exit;
  587. end;
  588. end;
  589. end;
  590. ValHexadecimal:=vs;
  591. end;
  592. Function PadZero(Var s: String; n: byte): Boolean;
  593. Begin
  594. PadZero:=TRUE;
  595. { Do some error checking first }
  596. if Length(s) = n then
  597. exit
  598. else
  599. if Length(s) > n then
  600. Begin
  601. PadZero:=FALSE;
  602. delete(s,n+1,length(s));
  603. exit;
  604. end
  605. else
  606. PadZero:=TRUE;
  607. { Fill it up with the specified character }
  608. fillchar(s[length(s)+1],n-1,#0);
  609. s[0]:=chr(n);
  610. end;
  611. {****************************************************************************
  612. TOperand
  613. ****************************************************************************}
  614. constructor TOperand.Create;
  615. begin
  616. size:=S_NO;
  617. hastype:=false;
  618. hasvar:=false;
  619. FillChar(Opr,sizeof(Opr),0);
  620. end;
  621. destructor TOperand.destroy;
  622. begin
  623. end;
  624. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  625. begin
  626. end;
  627. Procedure TOperand.SetSize(_size:longint;force:boolean);
  628. begin
  629. if force or
  630. ((size = S_NO) and (_size<=extended_size)) then
  631. Begin
  632. case _size of
  633. 1 : size:=S_B;
  634. 2 : size:=S_W{ could be S_IS};
  635. 4 : size:=S_L{ could be S_IL or S_FS};
  636. 8 : size:=S_IQ{ could be S_D or S_FL};
  637. extended_size : size:=S_FX;
  638. end;
  639. end;
  640. end;
  641. Function TOperand.SetupResult:boolean;
  642. Begin
  643. SetupResult:=false;
  644. { replace by correct offset. }
  645. if (not is_void(aktprocdef.rettype.def)) then
  646. begin
  647. if (m_tp7 in aktmodeswitches) and
  648. ret_in_acc(aktprocdef.rettype.def) then
  649. begin
  650. Message(asmr_e_cannot_use_RESULT_here);
  651. exit;
  652. end;
  653. opr.ref.offset:=procinfo^.return_offset;
  654. opr.ref.base:= procinfo^.framepointer;
  655. opr.ref.options:=ref_parafixup;
  656. { always assume that the result is valid. }
  657. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  658. { increase reference count, this is also used to check
  659. if the result variable is actually used or not }
  660. inc(tfuncretsym(aktprocdef.funcretsym).refcount);
  661. SetupResult:=true;
  662. end
  663. else
  664. Message(asmr_e_void_function);
  665. end;
  666. Function TOperand.SetupSelf:boolean;
  667. Begin
  668. SetupSelf:=false;
  669. if assigned(procinfo^._class) then
  670. Begin
  671. opr.typ:=OPR_REFERENCE;
  672. opr.ref.offset:=procinfo^.selfpointer_offset;
  673. opr.ref.base:=procinfo^.framepointer;
  674. opr.ref.options:=ref_selffixup;
  675. SetupSelf:=true;
  676. end
  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 lexlevel>normal_function_level then
  684. Begin
  685. opr.typ:=OPR_REFERENCE;
  686. opr.ref.offset:=procinfo^.framepointer_offset;
  687. opr.ref.base:=procinfo^.framepointer;
  688. opr.ref.options:=ref_parafixup;
  689. SetupOldEBP:=true;
  690. end
  691. else
  692. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  693. end;
  694. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  695. { search and sets up the correct fields in the Instr record }
  696. { for the NON-constant identifier passed to the routine. }
  697. { if not found returns FALSE. }
  698. var
  699. sym : tsym;
  700. srsymtable : tsymtable;
  701. harrdef : tarraydef;
  702. l : longint;
  703. Begin
  704. SetupVar:=false;
  705. asmsearchsym(s,sym,srsymtable);
  706. if sym = nil then
  707. exit;
  708. case sym.typ of
  709. varsym :
  710. begin
  711. { we always assume in asm statements that }
  712. { that the variable is valid. }
  713. tvarsym(sym).varstate:=vs_used;
  714. inc(tvarsym(sym).refs);
  715. case tvarsym(sym).owner.symtabletype of
  716. objectsymtable :
  717. begin
  718. { this is not allowed, because we don't know if the self
  719. register is still free, and loading it first is also
  720. not possible, because this could break code }
  721. { Be TP/Delphi compatible in Delphi or TP modes }
  722. if (m_tp7 in aktmodeswitches) or
  723. (m_delphi in aktmodeswitches) then
  724. begin
  725. opr.typ:=OPR_CONSTANT;
  726. opr.val:=tvarsym(sym).address;
  727. end
  728. { I do not agree here people using method vars should ensure
  729. that %esi is valid there }
  730. else
  731. begin
  732. opr.ref.base:=self_pointer;
  733. opr.ref.offset:=tvarsym(sym).address;
  734. end;
  735. hasvar:=true;
  736. SetupVar:=true;
  737. Exit;
  738. end;
  739. globalsymtable,
  740. staticsymtable :
  741. opr.ref.symbol:=newasmsymbol(tvarsym(sym).mangledname);
  742. parasymtable :
  743. begin
  744. { if we only want the offset we don't have to care
  745. the base will be zeroed after ! }
  746. if (lexlevel=tvarsym(sym).owner.symtablelevel) or
  747. { this below is wrong because there are two parast
  748. for global functions one of interface the second of
  749. implementation
  750. if (tvarsym(sym).owner=procinfo^.def.parast) or }
  751. GetOffset then
  752. begin
  753. opr.ref.base:=procinfo^.framepointer;
  754. end
  755. else
  756. begin
  757. if (procinfo^.framepointer=stack_pointer) and
  758. assigned(procinfo^.parent) and
  759. (lexlevel=tvarsym(sym).owner.symtablelevel+1) and
  760. { same problem as above !!
  761. (procinfo^.parent^.sym.definition.parast=tvarsym(sym).owner) and }
  762. (lexlevel>normal_function_level) then
  763. opr.ref.base:=procinfo^.parent^.framepointer
  764. else
  765. message1(asmr_e_local_para_unreachable,s);
  766. end;
  767. opr.ref.offset:=tvarsym(sym).address;
  768. if (lexlevel=tvarsym(sym).owner.symtablelevel) then
  769. begin
  770. opr.ref.offsetfixup:=aktprocdef.parast.address_fixup;
  771. opr.ref.options:=ref_parafixup;
  772. end
  773. else
  774. begin
  775. opr.ref.offsetfixup:=0;
  776. opr.ref.options:=ref_none;
  777. end;
  778. if (tvarsym(sym).varspez=vs_var) or
  779. ((tvarsym(sym).varspez=vs_const) and
  780. push_addr_param(tvarsym(sym).vartype.def)) then
  781. SetSize(target_info.size_of_pointer,false);
  782. end;
  783. localsymtable :
  784. begin
  785. if (vo_is_external in tvarsym(sym).varoptions) then
  786. opr.ref.symbol:=newasmsymbol(tvarsym(sym).mangledname)
  787. else
  788. begin
  789. { if we only want the offset we don't have to care
  790. the base will be zeroed after ! }
  791. if (lexlevel=tvarsym(sym).owner.symtablelevel) or
  792. {if (tvarsym(sym).owner=procinfo^.def.localst) or}
  793. GetOffset then
  794. opr.ref.base:=procinfo^.framepointer
  795. else
  796. begin
  797. if (procinfo^.framepointer=stack_pointer) and
  798. assigned(procinfo^.parent) and
  799. (lexlevel=tvarsym(sym).owner.symtablelevel+1) and
  800. {(procinfo^.parent^.sym.definition.localst=tvarsym(sym).owner) and}
  801. (lexlevel>normal_function_level) then
  802. opr.ref.base:=procinfo^.parent^.framepointer
  803. else
  804. message1(asmr_e_local_para_unreachable,s);
  805. end;
  806. opr.ref.offset:=-(tvarsym(sym).address);
  807. if (lexlevel=tvarsym(sym).owner.symtablelevel) then
  808. begin
  809. opr.ref.offsetfixup:=aktprocdef.localst.address_fixup;
  810. opr.ref.options:=ref_localfixup;
  811. end
  812. else
  813. begin
  814. opr.ref.offsetfixup:=0;
  815. opr.ref.options:=ref_none;
  816. end;
  817. end;
  818. if (tvarsym(sym).varspez in [vs_var,vs_out]) or
  819. ((tvarsym(sym).varspez=vs_const) and
  820. push_addr_param(tvarsym(sym).vartype.def)) then
  821. SetSize(target_info.size_of_pointer,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. opr.ref.symbol:=newasmsymbol(ttypedconstsym(sym).mangledname);
  848. case ttypedconstsym(sym).typedconsttype.def.deftype of
  849. orddef,
  850. enumdef,
  851. pointerdef,
  852. floatdef :
  853. SetSize(ttypedconstsym(sym).getsize,false);
  854. arraydef :
  855. begin
  856. { for arrays try to get the element size, take care of
  857. multiple indexes }
  858. harrdef:=tarraydef(ttypedconstsym(sym).typedconsttype.def);
  859. while assigned(harrdef.elementtype.def) and
  860. (harrdef.elementtype.def.deftype=arraydef) do
  861. harrdef:=tarraydef(harrdef.elementtype.def);
  862. SetSize(harrdef.elesize,false);
  863. end;
  864. end;
  865. hasvar:=true;
  866. SetupVar:=true;
  867. Exit;
  868. end;
  869. constsym :
  870. begin
  871. if tconstsym(sym).consttyp in [constint,constchar,constbool] then
  872. begin
  873. opr.typ:=OPR_CONSTANT;
  874. opr.val:=tconstsym(sym).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. opr.typ:=OPR_CONSTANT;
  884. opr.val:=0;
  885. SetupVar:=TRUE;
  886. Exit;
  887. end;
  888. end;
  889. procsym :
  890. begin
  891. if assigned(tprocsym(sym).defs^.next) then
  892. Message(asmr_w_calling_overload_func);
  893. l:=opr.ref.offset;
  894. opr.typ:=OPR_SYMBOL;
  895. opr.symbol:=newasmsymbol(tprocsym(sym).defs^.def.mangledname);
  896. opr.symofs:=l;
  897. hasvar:=true;
  898. SetupVar:=TRUE;
  899. Exit;
  900. end;
  901. else
  902. begin
  903. Message(asmr_e_unsupported_symbol_type);
  904. exit;
  905. end;
  906. end;
  907. end;
  908. { looks for internal names of variables and routines }
  909. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  910. var
  911. p : tasmsymbol;
  912. begin
  913. SetupDirectVar:=false;
  914. p:=getasmsymbol(hs);
  915. if assigned(p) then
  916. begin
  917. opr.ref.symbol:=p;
  918. hasvar:=true;
  919. SetupDirectVar:=true;
  920. end;
  921. end;
  922. procedure TOperand.InitRef;
  923. {*********************************************************************}
  924. { Description: This routine first check if the opcode is of }
  925. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  926. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  927. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  928. { to point to the default segment. }
  929. {*********************************************************************}
  930. Begin
  931. case opr.typ of
  932. OPR_REFERENCE:
  933. exit;
  934. OPR_NONE: ;
  935. else
  936. Message(asmr_e_invalid_operand_type);
  937. end;
  938. opr.typ := OPR_REFERENCE;
  939. reset_reference(opr.ref);
  940. end;
  941. procedure TOperand.BuildOperand;
  942. begin
  943. abstract;
  944. end;
  945. {****************************************************************************
  946. TInstruction
  947. ****************************************************************************}
  948. constructor TInstruction.create;
  949. Begin
  950. Opcode:=A_NONE;
  951. Opsize:=S_NO;
  952. Condition:=C_NONE;
  953. Ops:=0;
  954. InitOperands;
  955. Labeled:=false;
  956. end;
  957. destructor TInstruction.destroy;
  958. var
  959. i : longint;
  960. Begin
  961. for i:=1 to 3 do
  962. Operands[i].free;
  963. end;
  964. procedure TInstruction.InitOperands;
  965. var
  966. i : longint;
  967. begin
  968. for i:=1 to 3 do
  969. Operands[i].create;
  970. end;
  971. Procedure TInstruction.Swatoperands;
  972. Var
  973. p : toperand;
  974. Begin
  975. case Ops of
  976. 2 :
  977. begin
  978. p:=Operands[1];
  979. Operands[1]:=Operands[2];
  980. Operands[2]:=p;
  981. end;
  982. 3 :
  983. begin
  984. p:=Operands[1];
  985. Operands[1]:=Operands[3];
  986. Operands[3]:=p;
  987. end;
  988. end;
  989. end;
  990. procedure TInstruction.ConcatInstruction(p:TAAsmOutput);
  991. begin
  992. abstract;
  993. end;
  994. procedure TInstruction.BuildOpcode;
  995. begin
  996. abstract;
  997. end;
  998. {***************************************************************************
  999. TLocalLabel
  1000. ***************************************************************************}
  1001. constructor TLocalLabel.create(const n:string);
  1002. begin
  1003. inherited CreateName(n);
  1004. lab:=nil;
  1005. emitted:=false;
  1006. end;
  1007. function TLocalLabel.Gettasmlabel:tasmlabel;
  1008. begin
  1009. if not assigned(lab) then
  1010. begin
  1011. getlabel(lab);
  1012. { this label is forced to be used so it's always written }
  1013. inc(lab.refs);
  1014. end;
  1015. Gettasmlabel:=lab;
  1016. end;
  1017. {***************************************************************************
  1018. TLocalLabelList
  1019. ***************************************************************************}
  1020. procedure LocalLabelEmitted(p:tnamedindexitem);
  1021. begin
  1022. if not TLocalLabel(p).emitted then
  1023. Message1(asmr_e_unknown_label_identifier,p.name);
  1024. end;
  1025. procedure TLocalLabelList.CheckEmitted;
  1026. begin
  1027. ForEach_Static({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted)
  1028. end;
  1029. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1030. var
  1031. lab : TLocalLabel;
  1032. Begin
  1033. CreateLocalLabel:=true;
  1034. { Check if it already is defined }
  1035. lab:=TLocalLabel(LocalLabellist.Search(s));
  1036. if not assigned(lab) then
  1037. begin
  1038. lab:=TLocalLabel.Create(s);
  1039. LocalLabellist.Insert(lab);
  1040. end;
  1041. { set emitted flag and check for dup syms }
  1042. if emit then
  1043. begin
  1044. if lab.Emitted then
  1045. begin
  1046. Message1(asmr_e_dup_local_sym,lab.Name);
  1047. CreateLocalLabel:=false;
  1048. end;
  1049. lab.Emitted:=true;
  1050. end;
  1051. hl:=lab.Gettasmlabel;
  1052. end;
  1053. {****************************************************************************
  1054. Symbol table helper routines
  1055. ****************************************************************************}
  1056. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1057. var
  1058. i : integer;
  1059. begin
  1060. i:=pos('.',s);
  1061. { allow unit.identifier }
  1062. if i>0 then
  1063. begin
  1064. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1065. if assigned(srsym) then
  1066. begin
  1067. if (srsym.typ=unitsym) and
  1068. (srsym.owner.unitid=0) then
  1069. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1070. else
  1071. srsym:=nil;
  1072. end;
  1073. end
  1074. else
  1075. searchsym(s,srsym,srsymtable);
  1076. end;
  1077. Function SearchType(const hs:string): Boolean;
  1078. var
  1079. srsym : tsym;
  1080. srsymtable : tsymtable;
  1081. begin
  1082. asmsearchsym(hs,srsym,srsymtable);
  1083. SearchType:=assigned(srsym) and
  1084. (srsym.typ=typesym);
  1085. end;
  1086. Function SearchRecordType(const s:string): boolean;
  1087. var
  1088. srsym : tsym;
  1089. srsymtable : tsymtable;
  1090. Begin
  1091. SearchRecordType:=false;
  1092. { Check the constants in symtable }
  1093. asmsearchsym(s,srsym,srsymtable);
  1094. if srsym <> nil then
  1095. Begin
  1096. case srsym.typ of
  1097. typesym :
  1098. begin
  1099. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1100. begin
  1101. SearchRecordType:=true;
  1102. exit;
  1103. end;
  1104. end;
  1105. end;
  1106. end;
  1107. end;
  1108. Function SearchIConstant(const s:string; var l:longint): boolean;
  1109. {**********************************************************************}
  1110. { Description: Searches for a CONSTANT of name s in either the local }
  1111. { symbol list, then in the global symbol list, and returns the value }
  1112. { of that constant in l. Returns TRUE if successfull, if not found, }
  1113. { or if the constant is not of correct type, then returns FALSE }
  1114. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1115. { respectively. }
  1116. {**********************************************************************}
  1117. var
  1118. srsym : tsym;
  1119. srsymtable : tsymtable;
  1120. Begin
  1121. SearchIConstant:=false;
  1122. { check for TRUE or FALSE reserved words first }
  1123. if s = 'TRUE' then
  1124. Begin
  1125. SearchIConstant:=TRUE;
  1126. l:=1;
  1127. exit;
  1128. end;
  1129. if s = 'FALSE' then
  1130. Begin
  1131. SearchIConstant:=TRUE;
  1132. l:=0;
  1133. exit;
  1134. end;
  1135. { Check the constants in symtable }
  1136. asmsearchsym(s,srsym,srsymtable);
  1137. if srsym <> nil then
  1138. Begin
  1139. case srsym.typ of
  1140. constsym :
  1141. begin
  1142. if (tconstsym(srsym).consttyp in [constord,constint,constchar,constbool]) then
  1143. Begin
  1144. l:=tconstsym(srsym).valueord;
  1145. SearchIConstant:=TRUE;
  1146. exit;
  1147. end;
  1148. end;
  1149. enumsym:
  1150. Begin
  1151. l:=tenumsym(srsym).value;
  1152. SearchIConstant:=TRUE;
  1153. exit;
  1154. end;
  1155. end;
  1156. end;
  1157. end;
  1158. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1159. { search and returns the offset and size of records/objects of the base }
  1160. { with field name setup in field. }
  1161. { returns FALSE if not found. }
  1162. { used when base is a variable or a typed constant name. }
  1163. var
  1164. st : tsymtable;
  1165. harrdef : tarraydef;
  1166. sym : tsym;
  1167. srsymtable : tsymtable;
  1168. i : longint;
  1169. base : string;
  1170. Begin
  1171. GetRecordOffsetSize:=FALSE;
  1172. Offset:=0;
  1173. Size:=0;
  1174. i:=pos('.',s);
  1175. if i=0 then
  1176. i:=255;
  1177. base:=Copy(s,1,i-1);
  1178. delete(s,1,i);
  1179. if base='SELF' then
  1180. st:=procinfo^._class.symtable
  1181. else
  1182. begin
  1183. asmsearchsym(base,sym,srsymtable);
  1184. st:=nil;
  1185. { we can start with a var,type,typedconst }
  1186. case sym.typ of
  1187. varsym :
  1188. begin
  1189. case tvarsym(sym).vartype.def.deftype of
  1190. recorddef :
  1191. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1192. objectdef :
  1193. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1194. end;
  1195. end;
  1196. typesym :
  1197. begin
  1198. case ttypesym(sym).restype.def.deftype of
  1199. recorddef :
  1200. st:=trecorddef(ttypesym(sym).restype.def).symtable;
  1201. objectdef :
  1202. st:=tobjectdef(ttypesym(sym).restype.def).symtable;
  1203. end;
  1204. end;
  1205. typedconstsym :
  1206. begin
  1207. case ttypedconstsym(sym).typedconsttype.def.deftype of
  1208. recorddef :
  1209. st:=trecorddef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1210. objectdef :
  1211. st:=tobjectdef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1212. end;
  1213. end;
  1214. end;
  1215. end;
  1216. { now walk all recordsymtables }
  1217. while assigned(st) and (s<>'') do
  1218. begin
  1219. { load next field in base }
  1220. i:=pos('.',s);
  1221. if i=0 then
  1222. i:=255;
  1223. base:=Copy(s,1,i-1);
  1224. delete(s,1,i);
  1225. if st.symtabletype=objectsymtable then
  1226. sym:=search_class_member(tobjectdef(st.defowner),base)
  1227. else
  1228. sym:=tsym(st.search(base));
  1229. if not assigned(sym) then
  1230. begin
  1231. GetRecordOffsetSize:=false;
  1232. exit;
  1233. end;
  1234. st:=nil;
  1235. case sym.typ of
  1236. varsym :
  1237. begin
  1238. inc(Offset,tvarsym(sym).address);
  1239. Size:=tvarsym(sym).getsize;
  1240. case tvarsym(sym).vartype.def.deftype of
  1241. arraydef :
  1242. begin
  1243. { for arrays try to get the element size, take care of
  1244. multiple indexes }
  1245. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  1246. while assigned(harrdef.elementtype.def) and
  1247. (harrdef.elementtype.def.deftype=arraydef) do
  1248. harrdef:=tarraydef(harrdef.elementtype.def);
  1249. size:=harrdef.elesize;
  1250. end;
  1251. recorddef :
  1252. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1253. objectdef :
  1254. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1255. end;
  1256. end;
  1257. end;
  1258. end;
  1259. GetRecordOffsetSize:=(s='');
  1260. end;
  1261. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1262. var
  1263. sym : tsym;
  1264. srsymtable : tsymtable;
  1265. hs : string;
  1266. Begin
  1267. hl:=nil;
  1268. SearchLabel:=false;
  1269. { Check for pascal labels, which are case insensetive }
  1270. hs:=upper(s);
  1271. asmsearchsym(hs,sym,srsymtable);
  1272. if sym=nil then
  1273. exit;
  1274. case sym.typ of
  1275. labelsym :
  1276. begin
  1277. hl:=tlabelsym(sym).lab;
  1278. if emit then
  1279. tlabelsym(sym).defined:=true
  1280. else
  1281. tlabelsym(sym).used:=true;
  1282. SearchLabel:=true;
  1283. exit;
  1284. end;
  1285. end;
  1286. end;
  1287. {*************************************************************************}
  1288. { Instruction Generation Utilities }
  1289. {*************************************************************************}
  1290. Procedure ConcatString(p : TAAsmoutput;s:string);
  1291. {*********************************************************************}
  1292. { PROCEDURE ConcatString(s:string); }
  1293. { Description: This routine adds the character chain pointed to in }
  1294. { s to the instruction linked list. }
  1295. {*********************************************************************}
  1296. Var
  1297. pc: PChar;
  1298. Begin
  1299. getmem(pc,length(s)+1);
  1300. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1301. end;
  1302. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1303. {*********************************************************************}
  1304. { PROCEDURE ConcatPasString(s:string); }
  1305. { Description: This routine adds the character chain pointed to in }
  1306. { s to the instruction linked list, contrary to ConcatString it }
  1307. { uses a pascal style string, so it conserves null characters. }
  1308. {*********************************************************************}
  1309. Begin
  1310. p.concat(Tai_string.Create(s));
  1311. end;
  1312. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1313. {*********************************************************************}
  1314. { PROCEDURE ConcatDirect(s:string) }
  1315. { Description: This routine output the string directly to the asm }
  1316. { output, it is only sed when writing special labels in AT&T mode, }
  1317. { and should not be used without due consideration, since it may }
  1318. { cause problems. }
  1319. {*********************************************************************}
  1320. Var
  1321. pc: PChar;
  1322. Begin
  1323. getmem(pc,length(s)+1);
  1324. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1325. end;
  1326. Procedure ConcatConstant(p: TAAsmoutput; value: longint; maxvalue: longint);
  1327. {*********************************************************************}
  1328. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1329. { Description: This routine adds the value constant to the current }
  1330. { instruction linked list. }
  1331. { maxvalue -> indicates the size of the data to initialize: }
  1332. { $ff -> create a byte node. }
  1333. { $ffff -> create a word node. }
  1334. { $ffffffff -> create a dword node. }
  1335. {*********************************************************************}
  1336. Begin
  1337. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1338. Begin
  1339. Message(asmr_e_constant_out_of_bounds);
  1340. { assuming a value of maxvalue }
  1341. value:=maxvalue;
  1342. end;
  1343. if maxvalue = $ff then
  1344. p.concat(Tai_const.Create_8bit(byte(value)))
  1345. else
  1346. if maxvalue = $ffff then
  1347. p.concat(Tai_const.Create_16bit(word(value)))
  1348. else
  1349. if maxvalue = longint($ffffffff) then
  1350. p.concat(Tai_const.Create_32bit(longint(value)));
  1351. end;
  1352. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  1353. begin
  1354. p.concat(Tai_const_symbol.Createname_offset(sym,l));
  1355. end;
  1356. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1357. {***********************************************************************}
  1358. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1359. { Description: This routine adds the value constant to the current }
  1360. { instruction linked list. }
  1361. { real_typ -> indicates the type of the real data to initialize: }
  1362. { s32real -> create a single node. }
  1363. { s64real -> create a double node. }
  1364. { s80real -> create an extended node. }
  1365. { s64bit -> create a comp node. }
  1366. { f32bit -> create a fixed node. (not used normally) }
  1367. {***********************************************************************}
  1368. Begin
  1369. case real_typ of
  1370. s32real : p.concat(Tai_real_32bit.Create(value));
  1371. s64real : p.concat(Tai_real_64bit.Create(value));
  1372. s80real : p.concat(Tai_real_80bit.Create(value));
  1373. s64comp : p.concat(Tai_comp_64bit.Create(value));
  1374. end;
  1375. end;
  1376. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1377. {*********************************************************************}
  1378. { PROCEDURE ConcatLabel }
  1379. { Description: This routine either emits a label or a labeled }
  1380. { instruction to the linked list of instructions. }
  1381. {*********************************************************************}
  1382. begin
  1383. p.concat(Tai_label.Create(l));
  1384. end;
  1385. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  1386. {*********************************************************************}
  1387. { PROCEDURE ConcatPublic }
  1388. { Description: This routine emits an global definition to the }
  1389. { linked list of instructions.(used by AT&T styled asm) }
  1390. {*********************************************************************}
  1391. begin
  1392. p.concat(Tai_align.Create(l));
  1393. end;
  1394. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1395. {*********************************************************************}
  1396. { PROCEDURE ConcatPublic }
  1397. { Description: This routine emits an global definition to the }
  1398. { linked list of instructions.(used by AT&T styled asm) }
  1399. {*********************************************************************}
  1400. begin
  1401. p.concat(Tai_symbol.Createname_global(s,0));
  1402. end;
  1403. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1404. {*********************************************************************}
  1405. { PROCEDURE ConcatLocal }
  1406. { Description: This routine emits an local definition to the }
  1407. { linked list of instructions. }
  1408. {*********************************************************************}
  1409. begin
  1410. p.concat(Tai_symbol.Createname(s,0));
  1411. end;
  1412. Procedure ConcatGlobalBss(const s : string;size : longint);
  1413. {*********************************************************************}
  1414. { PROCEDURE ConcatGlobalBss }
  1415. { Description: This routine emits an global datablock to the }
  1416. { linked list of instructions. }
  1417. {*********************************************************************}
  1418. begin
  1419. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1420. end;
  1421. Procedure ConcatLocalBss(const s : string;size : longint);
  1422. {*********************************************************************}
  1423. { PROCEDURE ConcatLocalBss }
  1424. { Description: This routine emits a local datablcok to the }
  1425. { linked list of instructions. }
  1426. {*********************************************************************}
  1427. begin
  1428. bssSegment.concat(Tai_datablock.Create(s,size));
  1429. end;
  1430. end.
  1431. {
  1432. $Log$
  1433. Revision 1.26 2002-01-24 18:25:50 peter
  1434. * implicit result variable generation for assembler routines
  1435. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1436. Revision 1.25 2001/11/02 22:58:06 peter
  1437. * procsym definition rewrite
  1438. Revision 1.24 2001/09/02 21:18:28 peter
  1439. * split constsym.value in valueord,valueordptr,valueptr. The valueordptr
  1440. is used for holding target platform pointer values. As those can be
  1441. bigger than the source platform.
  1442. Revision 1.23 2001/08/26 13:36:48 florian
  1443. * some cg reorganisation
  1444. * some PPC updates
  1445. Revision 1.22 2001/08/12 17:57:07 peter
  1446. * under development flag for targets
  1447. Revision 1.21 2001/08/06 21:40:48 peter
  1448. * funcret moved from tprocinfo to tprocdef
  1449. Revision 1.20 2001/04/18 22:01:58 peter
  1450. * registration of targets and assemblers
  1451. Revision 1.19 2001/04/13 20:06:05 peter
  1452. * allow unit.identifier in asm readers
  1453. Revision 1.18 2001/04/13 01:22:13 peter
  1454. * symtable change to classes
  1455. * range check generation and errors fixed, make cycle DEBUG=1 works
  1456. * memory leaks fixed
  1457. Revision 1.17 2001/04/02 21:20:34 peter
  1458. * resulttype rewrite
  1459. Revision 1.16 2001/03/11 22:58:50 peter
  1460. * getsym redesign, removed the globals srsym,srsymtable
  1461. Revision 1.15 2001/02/26 19:44:54 peter
  1462. * merged generic m68k updates from fixes branch
  1463. Revision 1.14 2000/12/25 00:07:28 peter
  1464. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1465. tlinkedlist objects)
  1466. Revision 1.13 2000/12/07 17:19:43 jonas
  1467. * new constant handling: from now on, hex constants >$7fffffff are
  1468. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1469. and became $ffffffff80000000), all constants in the longint range
  1470. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1471. are cardinals and the rest are int64's.
  1472. * added lots of longint typecast to prevent range check errors in the
  1473. compiler and rtl
  1474. * type casts of symbolic ordinal constants are now preserved
  1475. * fixed bug where the original resulttype wasn't restored correctly
  1476. after doing a 64bit rangecheck
  1477. Revision 1.12 2000/11/29 00:30:38 florian
  1478. * unused units removed from uses clause
  1479. * some changes for widestrings
  1480. Revision 1.11 2000/11/06 22:30:30 peter
  1481. * more fixes
  1482. Revision 1.10 2000/11/04 14:25:21 florian
  1483. + merged Attila's changes for interfaces, not tested yet
  1484. Revision 1.9 2000/10/31 22:30:13 peter
  1485. * merged asm result patch part 2
  1486. Revision 1.8 2000/10/31 22:02:51 peter
  1487. * symtable splitted, no real code changes
  1488. Revision 1.7 2000/10/08 10:26:33 peter
  1489. * merged @result fix from Pierre
  1490. Revision 1.6 2000/09/24 21:19:51 peter
  1491. * delphi compile fixes
  1492. Revision 1.5 2000/09/24 15:06:26 peter
  1493. * use defines.inc
  1494. Revision 1.4 2000/08/27 16:11:52 peter
  1495. * moved some util functions from globals,cobjects to cutils
  1496. * splitted files into finput,fmodule
  1497. Revision 1.3 2000/08/06 10:42:29 peter
  1498. * merged patches name generation in lib and asm constant eval
  1499. Revision 1.2 2000/07/13 11:32:48 michael
  1500. + removed logs
  1501. }