asmutils.pas 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. {
  2. $Id$
  3. Copyright (c) 1998 Carl Eric Codere
  4. This unit implements some support routines for assembler parsing
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. **********************************************************************}
  17. Unit AsmUtils;
  18. {*************************************************************************}
  19. { This unit implements some objects as well as utilities which will be }
  20. { used by all inline assembler parsers (non-processor specific). }
  21. { }
  22. { Main routines/objects herein: }
  23. { o Object TExprParse is a simple expression parser to resolve assembler }
  24. { expressions. (Based generally on some code by Thai Tran from SWAG). }
  25. { o Object TInstruction is a simple object used for instructions }
  26. { o Record TOperand is a simple record used to store information on }
  27. { each operand. }
  28. { o String conversion routines from octal,binary and hex to decimal. }
  29. { o A linked list object/record for local labels }
  30. { o Routines for retrieving symbols (local and global) }
  31. { o Object for a linked list of strings (with duplicate strings not }
  32. { allowed). }
  33. { o Non-processor dependant routines for adding instructions to the }
  34. { instruction list. }
  35. {*************************************************************************}
  36. {--------------------------------------------------------------------}
  37. { LEFT TO DO: }
  38. { o Fix the remaining bugs in the expression parser, such as with }
  39. { 4+-3 }
  40. { o Add support for local typed constants search. }
  41. { o Add support for private/protected fields in method assembler }
  42. { routines. }
  43. {--------------------------------------------------------------------}
  44. Interface
  45. Uses
  46. symtable,aasm,hcodegen,verbose,systems,globals,files,strings,
  47. cobjects,
  48. {$ifdef i386}
  49. i386;
  50. {$endif}
  51. {$ifdef m68k}
  52. m68k;
  53. {$endif}
  54. Const
  55. RPNMax = 10; { I think you only need 4, but just to be safe }
  56. OpMax = 25;
  57. maxoperands = 3; { Maximum operands for assembler instructions }
  58. Type
  59. {---------------------------------------------------------------------}
  60. { Label Management types }
  61. {---------------------------------------------------------------------}
  62. PAsmLabel = ^TAsmLabel;
  63. { Each local label has this structure associated with it }
  64. TAsmLabel = record
  65. name: PString; { pointer to a pascal string name of label }
  66. lab: PLabel; { pointer to a label as defined in FPC }
  67. emitted: boolean; { as the label itself been emitted ? }
  68. next: PAsmLabel; { next node }
  69. end;
  70. TAsmLabelList = Object
  71. public
  72. First: PAsmLabel;
  73. Constructor Init;
  74. Destructor Done;
  75. Procedure Insert(s:string; lab: PLabel; emitted: boolean);
  76. Function Search(const s: string): PAsmLabel;
  77. private
  78. Last: PAsmLabel;
  79. Function NewPasStr(s:string): PString;
  80. end;
  81. {---------------------------------------------------------------------}
  82. { Instruction management types }
  83. {---------------------------------------------------------------------}
  84. toperandtype = (OPR_NONE,OPR_REFERENCE,OPR_CONSTANT,OPR_REGISTER,OPR_LABINSTR,
  85. OPR_REGLIST,OPR_SYMBOL);
  86. { When the TReference field isintvalue = TRUE }
  87. { then offset points to an ABSOLUTE address }
  88. { otherwise isintvalue should always be false }
  89. { Special cases: }
  90. { For the M68k Target, size is UNUSED, the }
  91. { opcode determines the size of the }
  92. { instruction. }
  93. { DIVS/DIVU/MULS/MULU of the form dn,dn:dn }
  94. { is stored as three operands!! }
  95. { Each instruction operand can be of this type }
  96. TOperand = record
  97. size: topsize;
  98. opinfo: longint; { ao_xxxx flags }
  99. overriden : boolean; { indicates if the opcode has been overriden }
  100. { by a pseudo-opcode such as DWORD PTR }
  101. case operandtype:toperandtype of
  102. { the size of the opr_none field should be at least equal to each }
  103. { other field as to facilitate initialization. }
  104. OPR_NONE: (l: array[1..sizeof(treference)] of byte);
  105. OPR_REFERENCE: (ref:treference);
  106. OPR_CONSTANT: (val: longint);
  107. OPR_REGISTER: (reg:tregister);
  108. OPR_LABINSTR: (hl: plabel);
  109. { Register list such as in the movem instruction }
  110. OPR_REGLIST: (list: set of tregister);
  111. OPR_SYMBOL : (symbol:pstring);
  112. end;
  113. TInstruction = object
  114. public
  115. operands: array[1..maxoperands] of TOperand;
  116. { if numops = zero, a size may still be valid in operands[1] }
  117. { it still should be checked. }
  118. numops: byte;
  119. { set to TRUE if the instruction is labeled. }
  120. labeled: boolean;
  121. { This is used for instructions such A_CMPSB... etc, to determine }
  122. { the size of the instruction. }
  123. stropsize: topsize;
  124. procedure init;
  125. procedure done;
  126. { sets up the prefix field with the instruction pointed to in s }
  127. procedure addprefix(tok: tasmop);
  128. { sets up the instruction with the instruction pointed to in s }
  129. procedure addinstr(tok: tasmop);
  130. { get the current instruction of this object }
  131. function getinstruction: tasmop;
  132. { get the current prefix of this instruction }
  133. function getprefix: tasmop;
  134. private
  135. prefix: tasmop;
  136. instruction: tasmop;
  137. end;
  138. {---------------------------------------------------------------------}
  139. { Expression parser types }
  140. {---------------------------------------------------------------------}
  141. { expression parser error codes }
  142. texpr_error =
  143. (zero_divide, { divide by zero. }
  144. stack_overflow, { stack overflow. }
  145. stack_underflow, { stack underflow. }
  146. invalid_number, { invalid conversion }
  147. invalid_op); { invalid operator }
  148. TExprOperator = record
  149. ch: char; { operator }
  150. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  151. end;
  152. String15 = String[15];
  153. {**********************************************************************}
  154. { The following operators are supported: }
  155. { '+' : addition }
  156. { '-' : subtraction }
  157. { '*' : multiplication }
  158. { '/' : modulo division }
  159. { '^' : exclusive or }
  160. { '<' : shift left }
  161. { '>' : shift right }
  162. { '&' : bitwise and }
  163. { '|' : bitwise or }
  164. { '~' : bitwise complement }
  165. { '%' : modulo division }
  166. { nnn: longint numbers }
  167. { ( and ) parenthesis }
  168. {**********************************************************************}
  169. TExprParse = Object
  170. public
  171. Constructor Init;
  172. Destructor Done;
  173. Function Evaluate(Expr: String): longint;
  174. Procedure Error(anerror: texpr_error); virtual;
  175. Function Priority(_Operator: Char): Integer; virtual;
  176. private
  177. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  178. RPNTop : Integer;
  179. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  180. OpTop : Integer;
  181. Procedure RPNPush(Num: Longint);
  182. Function RPNPop: Longint;
  183. Procedure RPNCalc(token: String15; prefix: boolean);
  184. Procedure OpPush(_Operator: char; prefix: boolean);
  185. { In reality returns TExprOperaotr }
  186. Procedure OpPop(var _Operator:TExprOperator);
  187. end;
  188. {---------------------------------------------------------------------}
  189. { String routines }
  190. {---------------------------------------------------------------------}
  191. {*********************************************************************}
  192. { PROCEDURE PadZero; }
  193. { Description: Makes sure that the string specified is of the given }
  194. { length, by padding it with binary zeros, or truncating if necessary}
  195. { Remark: The return value is determined BEFORE any eventual padding.}
  196. { Return Value: TRUE = if length of string s was <= then n }
  197. { FALSE = if length of string s was > then n }
  198. {*********************************************************************}
  199. Function PadZero(Var s: String; n: byte): Boolean;
  200. { Converts an Hex digit string to a Decimal string }
  201. { Returns '' if there was an error. }
  202. Function HexToDec(const S:String): String;
  203. { Converts a binary digit string to a Decimal string }
  204. { Returns '' if there was an error. }
  205. Function BinaryToDec(const S:String): String;
  206. { Converts an octal digit string to a Decimal string }
  207. { Returns '' if there was an error. }
  208. Function OctalToDec(const S:String): String;
  209. { Converts a string containing C styled escape sequences to }
  210. { a pascal style string. }
  211. Function EscapeToPascal(const s:string): string;
  212. Procedure ConcatPasString(p : paasmoutput;s:string);
  213. { Writes the string s directly to the assembler output }
  214. Procedure ConcatDirect(p : paasmoutput;s:string);
  215. {---------------------------------------------------------------------}
  216. { Symbol helper routines }
  217. {---------------------------------------------------------------------}
  218. Procedure SetOperandSize(var instr:TInstruction;operandnum,size:longint);
  219. Function GetVarOffsetSize(const base,field:string;Var Offset: longint;var Size:longint):boolean;
  220. Function GetTypeOffsetSize(const base,field: string;Var Offset: longint;var Size:longint):boolean;
  221. Function SearchIConstant(const s:string; var l:longint): boolean;
  222. Function SearchLabel(const s: string; var hl: plabel): boolean;
  223. Function CreateVarInstr(var Instr: TInstruction; const hs:string;
  224. operandnum:byte):boolean;
  225. {*********************************************************************}
  226. { FUNCTION NewPasStr(s:string): PString }
  227. { Description: This routine allocates a string on the heap and }
  228. { returns a pointer to the allocated string. }
  229. { }
  230. { Remarks: The string allocated should not be modified, since it's }
  231. { length will be less then 255. }
  232. { Remarks: It is assumed that HeapError will be called if an }
  233. { allocation fails. }
  234. {*********************************************************************}
  235. Function newpasstr(s: string): Pointer;
  236. Procedure SetupResult(Var Instr:TInstruction; operandnum: byte);
  237. {$ifdef i386}
  238. Procedure FWaitWarning;
  239. {$endif}
  240. {---------------------------------------------------------------------}
  241. { Instruction generation routines }
  242. {---------------------------------------------------------------------}
  243. { swaps in the case of a 2/3 operand opcode the destination and the }
  244. { source as to put it in AT&T style instruction format. }
  245. Procedure SwapOperands(Var instr: TInstruction);
  246. Procedure ConcatLabel(p : paasmoutput;op : tasmop;var l : plabel);
  247. Procedure ConcatConstant(p : paasmoutput;value: longint; maxvalue: longint);
  248. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  249. Procedure ConcatString(p : paasmoutput;s:string);
  250. Procedure ConcatPublic(p:paasmoutput;const s : string);
  251. Procedure ConcatLocal(p:paasmoutput;const s : string);
  252. Procedure ConcatGlobalBss(const s : string;size : longint);
  253. Procedure ConcatLocalBss(const s : string;size : longint);
  254. { add to list of external labels }
  255. Procedure ConcatExternal(const s : string;typ : texternal_typ);
  256. { add to internal list of labels }
  257. Procedure ConcatInternal(const s : string;typ : texternal_typ);
  258. Implementation
  259. {*************************************************************************}
  260. { Expression Parser }
  261. {*************************************************************************}
  262. Constructor TExprParse.Init;
  263. Begin
  264. end;
  265. Procedure TExprParse.Error(anerror:texpr_error);
  266. var
  267. t : tmsgconst;
  268. Begin
  269. case anerror of
  270. zero_divide: t:=assem_f_ev_zero_divide;
  271. stack_overflow: t:=assem_f_ev_stack_overflow;
  272. stack_underflow: t:=assem_f_ev_stack_underflow;
  273. invalid_number: t:=assem_f_ev_invalid_number;
  274. invalid_op: t:=assem_f_ev_invalid_op;
  275. else
  276. t:=assem_f_ev_unknown;
  277. end;
  278. Message(t);
  279. end;
  280. Procedure TExprParse.RPNPush(Num : longint); { Add an operand to the top of the RPN stack }
  281. begin
  282. if RPNTop < RPNMax then
  283. begin
  284. Inc(RPNTop);
  285. RPNStack[RPNTop] := Num;
  286. end
  287. else
  288. Error(stack_overflow); { Put some error handler here }
  289. end;
  290. Function TExprParse.RPNPop : longint; { Get the operand at the top of the RPN stack }
  291. begin
  292. if RPNTop > 0 then
  293. begin
  294. RPNPop := RPNStack[RPNTop];
  295. Dec(RPNTop);
  296. end
  297. else { Put some error handler here }
  298. Error(stack_underflow);
  299. end;
  300. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  301. Var
  302. Temp : longint;
  303. LocalError : Integer;
  304. begin
  305. { Write(Token, ' '); This just outputs the RPN expression }
  306. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  307. Case Token[1] of { Handle operators }
  308. '+' : Begin
  309. if prefix then
  310. else
  311. RPNPush(RPNPop + RPNPop);
  312. end;
  313. '-' : Begin
  314. if prefix then
  315. RPNPush(-(RPNPop))
  316. else
  317. RPNPush(RPNPop - RPNPop);
  318. end;
  319. '*' : RPNPush(RPNPop * RPNPop);
  320. '&' : RPNPush(RPNPop AND RPNPop);
  321. '|' : RPNPush(RPNPop OR RPNPop);
  322. '~' : RPNPush(NOT RPNPop);
  323. '<' : RPNPush(RPNPop SHL RPNPop);
  324. '>' : RPNPush(RPNPop SHR RPNPop);
  325. '%' : begin
  326. Temp := RPNPop;
  327. if Temp <> 0 then
  328. RPNPush(RPNPop mod Temp)
  329. else Error(zero_divide); { Handle divide by zero error }
  330. end;
  331. '^' : RPNPush(RPNPop XOR RPNPop);
  332. '/' :
  333. begin
  334. Temp := RPNPop;
  335. if Temp <> 0 then
  336. RPNPush(RPNPop div Temp)
  337. else Error(zero_divide);{ Handle divide by 0 error }
  338. end;
  339. end
  340. else
  341. begin { Convert String to number and add to stack }
  342. if token='-2147483648' then
  343. begin
  344. temp:=$80000000;
  345. localerror:=0;
  346. end
  347. else
  348. Val(Token, Temp, LocalError);
  349. if LocalError = 0 then
  350. RPNPush(Temp)
  351. else Error(invalid_number);{ Handle error }
  352. end;
  353. end;
  354. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean); { Add an operator onto top of the stack }
  355. begin
  356. if OpTop < OpMax then
  357. begin
  358. Inc(OpTop);
  359. OpStack[OpTop].ch := _Operator;
  360. OpStack[OpTop].is_prefix := prefix;
  361. end
  362. else Error(stack_overflow); { Put some error handler here }
  363. end;
  364. Procedure TExprParse.OpPop(var _Operator:TExprOperator); { Get operator at the top of the stack }
  365. begin
  366. if OpTop > 0 then
  367. begin
  368. _Operator := OpStack[OpTop];
  369. Dec(OpTop);
  370. end
  371. else Error(stack_underflow); { Put some error handler here }
  372. end;
  373. Function TExprParse.Priority(_Operator : Char) : Integer; { Return priority of operator }
  374. { The greater the priority, the higher the precedence }
  375. begin
  376. Case _Operator OF
  377. '(' : Priority := 0;
  378. '+', '-' : Priority := 1;
  379. '*', '/','%','<','>' : Priority := 2;
  380. '|','&','^','~': Priority := 0;
  381. else Error(invalid_op);{ More error handling }
  382. end;
  383. end;
  384. Function TExprParse.Evaluate(Expr : String):longint;
  385. Var
  386. I : Integer;
  387. Token : String15;
  388. opr: TExprOperator;
  389. begin
  390. OpTop := 0; { Reset stacks }
  391. RPNTop := 0;
  392. Token := '';
  393. For I := 1 to Length(Expr) DO
  394. begin
  395. if Expr[I] in ['0'..'9'] then
  396. begin { Build multi-digit numbers }
  397. Token := Token + Expr[I];
  398. if I = Length(Expr) then { Send last one to calculator }
  399. RPNCalc(Token,false);
  400. end
  401. else
  402. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  403. begin
  404. if Token <> '' then
  405. begin { Send last built number to calc. }
  406. RPNCalc(Token,false);
  407. Token := '';
  408. end;
  409. Case Expr[I] OF
  410. '(' : OpPush('(',false);
  411. ')' : begin
  412. While OpStack[OpTop].ch <> '(' DO
  413. Begin
  414. OpPop(opr);
  415. RPNCalc(opr.ch,opr.is_prefix);
  416. end;
  417. OpPop(opr); { Pop off and ignore the '(' }
  418. end;
  419. '+','-','~' : Begin
  420. { workaround for -2147483648 }
  421. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  422. begin
  423. token:='-';
  424. expr[i]:='+';
  425. end;
  426. { if start of expression then surely a prefix }
  427. { or if previous char was also an operator }
  428. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  429. OpPush(Expr[I],true)
  430. else
  431. Begin
  432. { Evaluate all higher priority operators }
  433. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  434. Begin
  435. OpPop(opr);
  436. RPNCalc(opr.ch,opr.is_prefix);
  437. end;
  438. OpPush(Expr[I],false);
  439. End;
  440. end;
  441. '*', '/',
  442. '^','|','&',
  443. '%','<','>' : begin
  444. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  445. Begin
  446. OpPop(opr);
  447. RPNCalc(opr.ch,opr.is_prefix);
  448. end;
  449. OpPush(Expr[I],false);
  450. end;
  451. end; { Case }
  452. end
  453. else
  454. Error(invalid_op); { Handle bad input error }
  455. end;
  456. { Pop off the remaining operators }
  457. While OpTop > 0 do
  458. Begin
  459. OpPop(opr);
  460. RPNCalc(opr.ch,opr.is_prefix);
  461. end;
  462. { The result is stored on the top of the stack }
  463. Evaluate := RPNPop;
  464. end;
  465. Destructor TExprParse.Done;
  466. Begin
  467. end;
  468. {*************************************************************************}
  469. { String conversions/utils }
  470. {*************************************************************************}
  471. Function newpasstr(s: string): Pointer;
  472. Var
  473. StrPtr: PString;
  474. Begin
  475. GetMem(StrPtr, length(s)+1);
  476. Move(s,StrPtr^,length(s)+1);
  477. newpasstr:= Strptr;
  478. end;
  479. Function EscapeToPascal(const s:string): string;
  480. { converts a C styled string - which contains escape }
  481. { characters to a pascal style string. }
  482. var
  483. i,j: word;
  484. str: string;
  485. temp: string;
  486. value: byte;
  487. code: integer;
  488. Begin
  489. str:='';
  490. i:=1;
  491. j:=1;
  492. repeat
  493. if s[i] = '\' then
  494. Begin
  495. Inc(i);
  496. if i > 255 then
  497. Begin
  498. EscapeToPascal:=str;
  499. exit;
  500. end;
  501. case s[i] of
  502. '\': insert('\',str,j);
  503. 'b': insert(#08,str,j);
  504. 'f': insert(#12,str,j);
  505. 'n': insert(#10,str,j);
  506. 'r': insert(#13,str,j);
  507. 't': insert(#09,str,j);
  508. '"': insert('"',str,j);
  509. { octal number }
  510. '0'..'7': Begin
  511. temp:=s[i];
  512. temp:=temp+s[i+1];
  513. temp:=temp+s[i+2];
  514. inc(i,2);
  515. val(octaltodec(temp),value,code);
  516. if (code <> 0) then
  517. Message(assem_w_invalid_numeric);
  518. insert(chr(value),str,j);
  519. end;
  520. { hexadecimal number }
  521. 'x': Begin
  522. temp:=s[i+1];
  523. temp:=temp+s[i+2];
  524. inc(i,2);
  525. val(hextodec(temp),value,code);
  526. if (code <> 0) then
  527. Message(assem_w_invalid_numeric);
  528. insert(chr(value),str,j);
  529. end;
  530. else
  531. Begin
  532. Message1(assem_e_escape_seq_ignored,s[i]);
  533. insert(s[i],str,j);
  534. end;
  535. end; {end case }
  536. Inc(i);
  537. end
  538. else
  539. Begin
  540. Insert(s[i],str,j);
  541. Inc(i);
  542. if i > 255 then
  543. Begin
  544. EscapeToPascal:=str;
  545. exit;
  546. end;
  547. end;
  548. Inc(j);
  549. until (i > length(s)) or (j > 255);
  550. EscapeToPascal:=str;
  551. end;
  552. Function OctalToDec(const S:String): String;
  553. { Converts an octal string to a Decimal string }
  554. { Returns '' if there was an error. }
  555. var vs: longint;
  556. c: byte;
  557. st: string;
  558. Begin
  559. vs := 0;
  560. for c:=1 to length(s) do
  561. begin
  562. case s[c] of
  563. '0': vs:=vs shl 3;
  564. '1': vs:=vs shl 3+1;
  565. '2': vs:=vs shl 3+2;
  566. '3': vs:=vs shl 3+3;
  567. '4': vs:=vs shl 3+4;
  568. '5': vs:=vs shl 3+5;
  569. '6': vs:=vs shl 3+6;
  570. '7': vs:=vs shl 3+7;
  571. else
  572. begin
  573. OctalToDec := '';
  574. exit;
  575. end;
  576. end;
  577. end;
  578. str(vs,st);
  579. OctalToDec := st;
  580. end;
  581. Function BinaryToDec(const S:String): String;
  582. { Converts a binary string to a Decimal string }
  583. { Returns '' if there was an error. }
  584. var vs: longint;
  585. c: byte;
  586. st: string;
  587. Begin
  588. vs := 0;
  589. for c:=1 to length(s) do
  590. begin
  591. if s[c] = '0' then
  592. vs:=vs shl 1
  593. else
  594. if s[c]='1' then
  595. vs:=vs shl 1+1
  596. else
  597. begin
  598. BinaryToDec := '';
  599. exit;
  600. end;
  601. end;
  602. str(vs,st);
  603. BinaryToDec := st;
  604. end;
  605. Function HexToDec(const S:String): String;
  606. var vs: longint;
  607. c: byte;
  608. st: string;
  609. Begin
  610. vs := 0;
  611. for c:=1 to length(s) do
  612. begin
  613. case upcase(s[c]) of
  614. '0': vs:=vs shl 4;
  615. '1': vs:=vs shl 4+1;
  616. '2': vs:=vs shl 4+2;
  617. '3': vs:=vs shl 4+3;
  618. '4': vs:=vs shl 4+4;
  619. '5': vs:=vs shl 4+5;
  620. '6': vs:=vs shl 4+6;
  621. '7': vs:=vs shl 4+7;
  622. '8': vs:=vs shl 4+8;
  623. '9': vs:=vs shl 4+9;
  624. 'A': vs:=vs shl 4+10;
  625. 'B': vs:=vs shl 4+11;
  626. 'C': vs:=vs shl 4+12;
  627. 'D': vs:=vs shl 4+13;
  628. 'E': vs:=vs shl 4+14;
  629. 'F': vs:=vs shl 4+15;
  630. else
  631. begin
  632. HexToDec := '';
  633. exit;
  634. end;
  635. end;
  636. end;
  637. str(vs,st);
  638. HexToDec := st;
  639. end;
  640. Function PadZero(Var s: String; n: byte): Boolean;
  641. Begin
  642. PadZero := TRUE;
  643. { Do some error checking first }
  644. if Length(s) = n then
  645. exit
  646. else
  647. if Length(s) > n then
  648. Begin
  649. PadZero := FALSE;
  650. delete(s,n+1,length(s));
  651. exit;
  652. end
  653. else
  654. PadZero := TRUE;
  655. { Fill it up with the specified character }
  656. fillchar(s[length(s)+1],n-1,#0);
  657. {$ifndef TP}
  658. {$ifopt H+}
  659. setlength(s,n);
  660. {$else}
  661. s[0] := chr(n);
  662. {$endif}
  663. {$else}
  664. s[0] := chr(n);
  665. {$endif}
  666. end;
  667. {*************************************************************************}
  668. { Instruction utilities }
  669. {*************************************************************************}
  670. Procedure TInstruction.init;
  671. var
  672. k: integer;
  673. Begin
  674. numops := 0;
  675. labeled := FALSE;
  676. stropsize := S_NO;
  677. prefix := A_NONE;
  678. instruction := A_NONE;
  679. for k:=1 to maxoperands do
  680. begin
  681. operands[k].size := S_NO;
  682. operands[k].overriden := FALSE;
  683. operands[k].operandtype := OPR_NONE;
  684. { init to zeros }
  685. fillchar(operands[k].l, sizeof(operands[k].l),#0);
  686. end;
  687. end;
  688. Procedure TInstruction.addprefix(tok: tasmop);
  689. Begin
  690. if tok = A_NONE then
  691. Message(assem_e_syn_prefix_not_found);
  692. if Prefix = A_NONE then
  693. Prefix := tok
  694. else
  695. Message(assem_e_syn_try_add_more_prefix);
  696. end;
  697. Procedure TInstruction.addinstr(tok: tasmop);
  698. Begin
  699. if tok = A_NONE then
  700. Message(assem_e_syn_opcode_not_found);
  701. Instruction := tok;
  702. end;
  703. function TInstruction.getinstruction: tasmop;
  704. Begin
  705. getinstruction := Instruction;
  706. end;
  707. { get the current prefix of this instruction }
  708. function TInstruction.getprefix: tasmop;
  709. Begin
  710. getprefix := prefix;
  711. end;
  712. Procedure TInstruction.done;
  713. var
  714. k: integer;
  715. Begin
  716. for k:=1 to numops do
  717. begin
  718. if (operands[k].operandtype=OPR_REFERENCE) and
  719. assigned(operands[k].ref.symbol) then
  720. stringdispose(operands[k].ref.symbol);
  721. if (operands[k].operandtype=OPR_SYMBOL) and
  722. assigned(operands[k].symbol) then
  723. stringdispose(operands[k].symbol);
  724. end;
  725. end;
  726. {*************************************************************************}
  727. { Local label utilities }
  728. {*************************************************************************}
  729. Constructor TAsmLabelList.Init;
  730. Begin
  731. First := nil;
  732. Last := nil;
  733. end;
  734. Procedure TAsmLabelList.Insert(s:string; lab: PLabel; emitted: boolean);
  735. {*********************************************************************}
  736. { Description: Insert a node at the end of the list with lab and }
  737. { and the name in s. The name is allocated on the heap. }
  738. { Duplicates are not allowed. }
  739. { Indicate in emitted if this label itself has been emitted, or is it}
  740. { a simple labeled instruction? }
  741. {*********************************************************************}
  742. Begin
  743. if search(s) = nil then
  744. Begin
  745. if First = nil then
  746. Begin
  747. New(First);
  748. Last := First;
  749. end
  750. else
  751. Begin
  752. New(Last^.Next);
  753. Last := Last^.Next;
  754. end;
  755. Last^.name := NewPasStr(s);
  756. Last^.Lab := lab;
  757. Last^.Next := nil;
  758. Last^.emitted := emitted;
  759. end;
  760. end;
  761. Function TAsmLabelList.Search(const s: string): PAsmLabel;
  762. {*********************************************************************}
  763. { Description: This routine searches for a label named s in the }
  764. { linked list, returns a pointer to the label if found, otherwise }
  765. { returns nil. }
  766. {*********************************************************************}
  767. Var
  768. asmlab: PAsmLabel;
  769. Begin
  770. asmlab := First;
  771. if First = nil then
  772. Begin
  773. Search := nil;
  774. exit;
  775. end;
  776. While (asmlab^.name^ <> s) and (asmlab^.Next <> nil) do
  777. asmlab := asmlab^.Next;
  778. if asmlab^.name^ = s then
  779. search := asmlab
  780. else
  781. search := nil;
  782. end;
  783. Destructor TAsmLabelList.Done;
  784. {*********************************************************************}
  785. { Description: This routine takes care of deallocating all nodes }
  786. { in the linked list, as well as deallocating the string pointers }
  787. { of these nodes. }
  788. { }
  789. { Remark: The PLabel field is NOT freed, the compiler takes care of }
  790. { this. }
  791. {*********************************************************************}
  792. Var
  793. temp: PAsmLabel;
  794. temp1: PAsmLabel;
  795. Begin
  796. temp := First;
  797. while temp <> nil do
  798. Begin
  799. Freemem(Temp^.name, length(Temp^.name^)+1);
  800. Temp1 := Temp^.Next;
  801. Dispose(Temp);
  802. Temp := Temp1;
  803. { The plabel could be deleted here, but let us not do }
  804. { it, FPC will do it instead. }
  805. end;
  806. end;
  807. Function TAsmLabelList.newpasstr(s: string): PString;
  808. {*********************************************************************}
  809. { FUNCTION NewPasStr(s:string): PString }
  810. { Description: This routine allocates a string on the heap and }
  811. { returns a pointer to the allocated string. }
  812. { }
  813. { Remarks: The string allocated should not be modified, since it's }
  814. { length will be less then 255. }
  815. { Remarks: It is assumed that HeapError will be called if an }
  816. { allocation fails. }
  817. {*********************************************************************}
  818. Var
  819. StrPtr: PString;
  820. Begin
  821. GetMem(StrPtr, length(s)+1);
  822. Move(s,StrPtr^,length(s)+1);
  823. newpasstr:= Strptr;
  824. end;
  825. {*************************************************************************}
  826. { Symbol table helper routines }
  827. {*************************************************************************}
  828. Procedure SwapOperands(Var instr: TInstruction);
  829. Var
  830. tempopr: TOperand;
  831. Begin
  832. if instr.numops = 2 then
  833. Begin
  834. tempopr := instr.operands[1];
  835. instr.operands[1] := instr.operands[2];
  836. instr.operands[2] := tempopr;
  837. end
  838. else
  839. if instr.numops = 3 then
  840. Begin
  841. tempopr := instr.operands[1];
  842. instr.operands[1] := instr.operands[3];
  843. instr.operands[3] := tempopr;
  844. end;
  845. end;
  846. Function SearchIConstant(const s:string; var l:longint): boolean;
  847. {**********************************************************************}
  848. { Description: Searches for a CONSTANT of name s in either the local }
  849. { symbol list, then in the global symbol list, and returns the value }
  850. { of that constant in l. Returns TRUE if successfull, if not found, }
  851. { or if the constant is not of correct type, then returns FALSE }
  852. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  853. { respectively. }
  854. {**********************************************************************}
  855. var
  856. sym: psym;
  857. Begin
  858. SearchIConstant := FALSE;
  859. { check for TRUE or FALSE reserved words first }
  860. if s = 'TRUE' then
  861. Begin
  862. SearchIConstant := TRUE;
  863. l := 1;
  864. end
  865. else
  866. if s = 'FALSE' then
  867. Begin
  868. SearchIConstant := TRUE;
  869. l := 0;
  870. end
  871. else
  872. if assigned(aktprocsym) then
  873. Begin
  874. if assigned(aktprocsym^.definition) then
  875. Begin
  876. { Check the local constants }
  877. if assigned(aktprocsym^.definition^.localst) then
  878. sym := aktprocsym^.definition^.localst^.search(s)
  879. else
  880. sym := nil;
  881. if assigned(sym) then
  882. Begin
  883. if (sym^.typ = constsym) and
  884. (pconstsym(sym)^.consttype in [constord,constint,constchar,constbool]) then
  885. Begin
  886. l:=pconstsym(sym)^.value;
  887. SearchIConstant := TRUE;
  888. exit;
  889. end;
  890. end;
  891. end;
  892. end;
  893. { Check the global constants }
  894. getsym(s,false);
  895. if srsym <> nil then
  896. Begin
  897. case srsym^.typ of
  898. constsym :
  899. begin
  900. if (pconstsym(srsym)^.consttype in [constord,constint,constchar,constbool]) then
  901. Begin
  902. l:=pconstsym(srsym)^.value;
  903. SearchIConstant := TRUE;
  904. exit;
  905. end;
  906. end;
  907. end;
  908. end;
  909. end;
  910. Procedure SetupResult(Var Instr:TInstruction; operandnum: byte);
  911. {**********************************************************************}
  912. { Description: This routine changes the correct fields and correct }
  913. { offset in the reference, so that it points to the __RESULT or }
  914. { @Result variable (depending on the inline asm). }
  915. { Resturns a reference with all correct offset correctly set up. }
  916. { The Operand should already point to a treference on entry. }
  917. {**********************************************************************}
  918. Begin
  919. { replace by correct offset. }
  920. if assigned(procinfo.retdef) and
  921. (procinfo.retdef<>pdef(voiddef)) then
  922. begin
  923. instr.operands[operandnum].ref.offset := procinfo.retoffset;
  924. instr.operands[operandnum].ref.base := procinfo.framepointer;
  925. { always assume that the result is valid. }
  926. procinfo.funcret_is_valid:=true;
  927. end
  928. else
  929. Message(assem_e_invalid_symbol_ref);
  930. end;
  931. {$ifdef i386}
  932. Procedure FWaitWarning;
  933. begin
  934. if (target_info.target=target_i386_GO32V2) and (cs_fp_emulation in aktmoduleswitches) then
  935. Message(assem_w_fwait_emu_prob);
  936. end;
  937. {$endif i386}
  938. Procedure SetOperandSize(var instr:TInstruction;operandnum,size:longint);
  939. begin
  940. { the current size is NOT overriden if it already }
  941. { exists, such as in the case of a byte ptr, in }
  942. { front of the identifier. }
  943. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  944. Begin
  945. case size of
  946. 1: instr.operands[operandnum].size := S_B;
  947. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  948. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  949. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  950. extended_size: instr.operands[operandnum].size := S_FX;
  951. else
  952. { this is in the case where the instruction is LEA }
  953. { or something like that, in that case size is not }
  954. { important. }
  955. instr.operands[operandnum].size := S_NO;
  956. end; { end case }
  957. end;
  958. end;
  959. Function GetVarOffsetSize(const base,field:string;Var Offset: longint;var Size:longint):boolean;
  960. { search and returns the offset and size of records/objects of the base }
  961. { with field name setup in field. }
  962. { returns FALSE if not found. }
  963. { used when base is a variable or a typed constant name. }
  964. var
  965. sym:psym;
  966. p: psym;
  967. Begin
  968. GetVarOffsetSize := FALSE;
  969. Offset := 0;
  970. { local list }
  971. if assigned(aktprocsym) then
  972. begin
  973. if assigned(aktprocsym^.definition^.localst) then
  974. sym:=aktprocsym^.definition^.localst^.search(base)
  975. else
  976. sym:=nil;
  977. if assigned(sym) then
  978. begin
  979. { field of local record variable. }
  980. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef) then
  981. begin
  982. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  983. if assigned(pvarsym(p)) then
  984. Begin
  985. Offset := pvarsym(p)^.address;
  986. Size:=PVarsym(p)^.getsize;
  987. GetVarOffsetSize := TRUE;
  988. Exit;
  989. end;
  990. end
  991. else
  992. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=objectdef) then
  993. begin
  994. if assigned(pobjectdef(pvarsym(sym)^.definition)^.publicsyms) then
  995. begin
  996. p:=pvarsym(pobjectdef(pvarsym(sym)^.definition)^.publicsyms^.search(field));
  997. if assigned(pvarsym(p)) then
  998. Begin
  999. Offset := pvarsym(p)^.address;
  1000. Size:=PVarsym(p)^.getsize;
  1001. GetVarOffsetSize := TRUE;
  1002. Exit;
  1003. end;
  1004. end;
  1005. end;
  1006. end
  1007. else
  1008. begin
  1009. { field of local record parameter to routine. }
  1010. if assigned(aktprocsym^.definition^.parast) then
  1011. sym:=aktprocsym^.definition^.parast^.search(base)
  1012. else
  1013. sym:=nil;
  1014. if assigned(sym) then
  1015. begin
  1016. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef)
  1017. then
  1018. begin
  1019. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  1020. if assigned(p) then
  1021. Begin
  1022. Offset := pvarsym(p)^.address;
  1023. Size:=PVarsym(p)^.getsize;
  1024. GetVarOffsetSize := TRUE;
  1025. Exit;
  1026. end;
  1027. end { endif }
  1028. else
  1029. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=objectdef) then
  1030. begin
  1031. if assigned(pobjectdef(pvarsym(sym)^.definition)^.publicsyms) then
  1032. begin
  1033. p:=pvarsym(pobjectdef(pvarsym(sym)^.definition)^.publicsyms^.search(field));
  1034. if assigned(pvarsym(p)) then
  1035. Begin
  1036. Offset := pvarsym(p)^.address;
  1037. Size:=PVarsym(p)^.getsize;
  1038. GetVarOffsetSize := TRUE;
  1039. Exit;
  1040. end;
  1041. end;
  1042. end;
  1043. end;
  1044. end;
  1045. end; { endif assigned(aktprocsym) }
  1046. { not found.. .now look for global variables. }
  1047. getsym(base,false);
  1048. sym:=srsym;
  1049. if assigned(sym) then
  1050. Begin
  1051. { field of global record variable. }
  1052. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef) then
  1053. begin
  1054. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  1055. if assigned(p) then
  1056. Begin
  1057. Offset := pvarsym(p)^.address;
  1058. Size:=PVarsym(p)^.getsize;
  1059. GetVarOffsetSize := TRUE;
  1060. Exit;
  1061. end;
  1062. end
  1063. else
  1064. { field of global record type constant. }
  1065. if (sym^.typ=typedconstsym) and (ptypedconstsym(sym)^.definition^.deftype=recorddef)
  1066. then
  1067. begin
  1068. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  1069. if assigned(p) then
  1070. Begin
  1071. Offset := pvarsym(p)^.address;
  1072. Size:=PVarsym(p)^.getsize;
  1073. GetVarOffsetSize := TRUE;
  1074. Exit;
  1075. end;
  1076. end
  1077. else
  1078. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=objectdef) then
  1079. begin
  1080. if assigned(pobjectdef(pvarsym(sym)^.definition)^.publicsyms) then
  1081. begin
  1082. p:=pvarsym(pobjectdef(pvarsym(sym)^.definition)^.publicsyms^.search(field));
  1083. if assigned(pvarsym(p)) then
  1084. Begin
  1085. Offset := pvarsym(p)^.address;
  1086. Size:=PVarsym(p)^.getsize;
  1087. GetVarOffsetSize := TRUE;
  1088. Exit;
  1089. end;
  1090. end;
  1091. end;
  1092. end; { end looking for global variables .. }
  1093. end;
  1094. Function GetTypeOffsetSize(const base,field: string;Var Offset: longint;var Size:longint):boolean;
  1095. { search and returns the offset of records/objects of the base }
  1096. { with field name setup in field. }
  1097. { returns 0 if not found. }
  1098. { used when base is a variable or a typed constant name. }
  1099. var
  1100. sym:psym;
  1101. p: psym;
  1102. Begin
  1103. Offset := 0;
  1104. GetTypeOffsetSize := FALSE;
  1105. { local list }
  1106. if assigned(aktprocsym) then
  1107. begin
  1108. if assigned(aktprocsym^.definition^.localst) then
  1109. sym:=aktprocsym^.definition^.localst^.search(base)
  1110. else
  1111. sym:=nil;
  1112. if assigned(sym) then
  1113. begin
  1114. { field of local record type. }
  1115. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef) then
  1116. begin
  1117. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1118. if assigned(p) then
  1119. Begin
  1120. Offset := pvarsym(p)^.address;
  1121. Size:=PVarsym(p)^.getsize;
  1122. GetTypeOffsetSize := TRUE;
  1123. Exit;
  1124. end;
  1125. end;
  1126. end
  1127. else
  1128. begin
  1129. { field of local record type to routine. }
  1130. if assigned(aktprocsym^.definition^.parast) then
  1131. sym:=aktprocsym^.definition^.parast^.search(base)
  1132. else
  1133. sym:=nil;
  1134. if assigned(sym) then
  1135. begin
  1136. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef)
  1137. then
  1138. begin
  1139. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1140. if assigned(p) then
  1141. Begin
  1142. Offset := pvarsym(p)^.address;
  1143. GetTypeOffsetSize := TRUE;
  1144. Exit;
  1145. end;
  1146. end; { endif }
  1147. end; {endif }
  1148. end; { endif }
  1149. end;
  1150. { not found.. .now look for global types. }
  1151. getsym(base,false);
  1152. sym:=srsym;
  1153. if assigned(sym) then
  1154. Begin
  1155. { field of global record types. }
  1156. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef) then
  1157. begin
  1158. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1159. if assigned(p) then
  1160. Begin
  1161. Offset := pvarsym(p)^.address;
  1162. Size:=PVarsym(p)^.getsize;
  1163. GetTypeOffsetSize := TRUE;
  1164. Exit;
  1165. end
  1166. end
  1167. else
  1168. { public field names of objects }
  1169. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=objectdef)then
  1170. begin
  1171. if assigned(pobjectdef(ptypesym(sym)^.definition)^.publicsyms) then
  1172. Begin
  1173. p:=pobjectdef(ptypesym(sym)^.definition)^.publicsyms^.search(field);
  1174. if assigned(p) then
  1175. Begin
  1176. Offset := pvarsym(p)^.address;
  1177. Size:=PVarsym(p)^.getsize;
  1178. GetTypeOffsetSize := TRUE;
  1179. Exit;
  1180. end
  1181. end;
  1182. end;
  1183. end; { end looking for global variables .. }
  1184. end;
  1185. Function CreateVarInstr(var Instr: TInstruction; const hs:string;operandnum:byte): Boolean;
  1186. { search and sets up the correct fields in the Instr record }
  1187. { for the NON-constant identifier passed to the routine. }
  1188. { if not found returns FALSE. }
  1189. var
  1190. sym : psym;
  1191. l : longint;
  1192. Begin
  1193. CreateVarInstr := FALSE;
  1194. { are we in a routine ? }
  1195. if assigned(aktprocsym) then
  1196. begin
  1197. { search the local list for the name of this variable. }
  1198. if assigned(aktprocsym^.definition^.localst) then
  1199. sym:=aktprocsym^.definition^.localst^.search(hs)
  1200. else
  1201. sym:=nil;
  1202. if assigned(sym) then
  1203. begin
  1204. case sym^.typ of
  1205. varsym : begin
  1206. { we always assume in asm statements that }
  1207. { that the variable is valid. }
  1208. pvarsym(sym)^.is_valid:=1;
  1209. if pvarsym(sym)^.owner^.symtabletype=staticsymtable then
  1210. begin
  1211. if assigned(instr.operands[operandnum].ref.symbol) then
  1212. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1213. instr.operands[operandnum].ref.symbol:=newpasstr(pvarsym(sym)^.mangledname);
  1214. end
  1215. else
  1216. begin
  1217. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1218. instr.operands[operandnum].ref.offset := -(pvarsym(sym)^.address);
  1219. end;
  1220. { the current size is NOT overriden if it already }
  1221. { exists, such as in the case of a byte ptr, in }
  1222. { front of the identifier. }
  1223. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1224. Begin
  1225. case pvarsym(sym)^.getsize of
  1226. 1: instr.operands[operandnum].size := S_B;
  1227. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  1228. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  1229. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  1230. extended_size: instr.operands[operandnum].size := S_FX;
  1231. else
  1232. { this is in the case where the instruction is LEA }
  1233. { or something like that, in that case size is not }
  1234. { important. }
  1235. instr.operands[operandnum].size := S_NO;
  1236. end; { end case }
  1237. end;
  1238. { ok, finished for this var }
  1239. CreateVarInstr := TRUE;
  1240. Exit;
  1241. end;
  1242. typedconstsym : begin
  1243. { we always assume in asm statements that }
  1244. { that the variable is valid. }
  1245. if assigned(instr.operands[operandnum].ref.symbol) then
  1246. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1247. instr.operands[operandnum].ref.symbol:=newpasstr(pvarsym(sym)^.mangledname);
  1248. { the current size is NOT overriden if it already }
  1249. { exists, such as in the case of a byte ptr, in }
  1250. { front of the identifier. }
  1251. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1252. Begin
  1253. case ptypedconstsym(sym)^.getsize of
  1254. 1: instr.operands[operandnum].size := S_B;
  1255. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  1256. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  1257. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  1258. extended_size: instr.operands[operandnum].size := S_FX;
  1259. else
  1260. instr.operands[operandnum].size := S_NO;
  1261. end; { end case }
  1262. end;
  1263. { ok, finished for this var }
  1264. CreateVarInstr := TRUE;
  1265. Exit;
  1266. end;
  1267. constsym : begin
  1268. if pconstsym(sym)^.consttype in [constint,constchar,constbool] then
  1269. begin
  1270. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1271. instr.operands[operandnum].val:=pconstsym(sym)^.value;
  1272. CreateVarInstr := TRUE;
  1273. Exit;
  1274. end;
  1275. end;
  1276. typesym : begin
  1277. if ptypesym(sym)^.definition^.deftype in [recorddef,objectdef] then
  1278. begin
  1279. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1280. instr.operands[operandnum].val:=0;
  1281. CreateVarInstr := TRUE;
  1282. Exit;
  1283. end;
  1284. end;
  1285. procsym : begin
  1286. { free the memory before changing the symbol name. }
  1287. if assigned(instr.operands[operandnum].ref.symbol) then
  1288. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1289. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1290. instr.operands[operandnum].symbol:=newpasstr(pprocsym(sym)^.definition^.mangledname);
  1291. CreateVarInstr := TRUE;
  1292. Exit;
  1293. end
  1294. else
  1295. begin
  1296. Message(assem_e_unsupported_symbol_type);
  1297. exit;
  1298. end;
  1299. end;
  1300. end;
  1301. { now check for parameters passed to routine }
  1302. if assigned(aktprocsym^.definition^.parast) then
  1303. sym:=aktprocsym^.definition^.parast^.search(hs)
  1304. else
  1305. sym:=nil;
  1306. if assigned(sym) then
  1307. begin
  1308. case sym^.typ of
  1309. varsym : begin
  1310. l:=pvarsym(sym)^.address;
  1311. { set offset }
  1312. inc(l,aktprocsym^.definition^.parast^.call_offset);
  1313. pvarsym(sym)^.is_valid:=1;
  1314. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1315. instr.operands[operandnum].ref.offset := l;
  1316. { the current size is NOT overriden if it already }
  1317. { exists, such as in the case of a byte ptr, in }
  1318. { front of the identifier. }
  1319. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1320. Begin
  1321. case pvarsym(sym)^.getsize of
  1322. 1: instr.operands[operandnum].size := S_B;
  1323. 2: instr.operands[operandnum].size := S_W;
  1324. 4: instr.operands[operandnum].size := S_L;
  1325. 8: instr.operands[operandnum].size := S_IQ;
  1326. extended_size: instr.operands[operandnum].size := S_FX;
  1327. else
  1328. { this is in the case where the instruction is LEA }
  1329. { or something like that, in that case size is not }
  1330. { important. }
  1331. instr.operands[operandnum].size := S_NO;
  1332. end; { end case }
  1333. end; { endif }
  1334. CreateVarInstr := TRUE;
  1335. Exit;
  1336. end;
  1337. else
  1338. begin
  1339. Message(assem_e_unsupported_symbol_type);
  1340. exit;
  1341. end;
  1342. end; { case }
  1343. end; { endif }
  1344. end;
  1345. { not found.. .now look for global variables. }
  1346. getsym(hs,false);
  1347. sym:=srsym;
  1348. if assigned(sym) then
  1349. Begin
  1350. case sym^.typ of
  1351. varsym,
  1352. typedconstsym : Begin
  1353. { free the memory before changing the symbol name. }
  1354. if assigned(instr.operands[operandnum].ref.symbol) then
  1355. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1356. instr.operands[operandnum].ref.symbol:=newpasstr(sym^.mangledname);
  1357. { the current size is NOT overriden if it already }
  1358. { exists, such as in the case of a byte ptr, in }
  1359. { front of the identifier. }
  1360. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1361. Begin
  1362. case pvarsym(sym)^.getsize of
  1363. 1: instr.operands[operandnum].size := S_B;
  1364. 2: instr.operands[operandnum].size := S_W;
  1365. 4: instr.operands[operandnum].size := S_L;
  1366. 8: instr.operands[operandnum].size := S_IQ;
  1367. else
  1368. { this is in the case where the instruction is LEA }
  1369. { or something like that, in that case size is not }
  1370. { important. }
  1371. instr.operands[operandnum].size := S_NO;
  1372. end;
  1373. end
  1374. else
  1375. if (instr.operands[operandnum].size = S_NO) and (sym^.typ = typedconstsym) then
  1376. Begin
  1377. { only these are valid sizes, otherwise prefixes are }
  1378. { required. }
  1379. case ptypedconstsym(sym)^.definition^.size of
  1380. 1: instr.operands[operandnum].size := S_B;
  1381. 2: instr.operands[operandnum].size := S_W;
  1382. 4: instr.operands[operandnum].size := S_L;
  1383. 8: instr.operands[operandnum].size := S_IQ;
  1384. else
  1385. { this is in the case where the instruction is LEA }
  1386. { or something like that, in that case size is not }
  1387. { important. }
  1388. instr.operands[operandnum].size := S_NO;
  1389. end;
  1390. end; { endif }
  1391. CreateVarInstr := TRUE;
  1392. Exit;
  1393. end;
  1394. constsym : begin
  1395. if pconstsym(sym)^.consttype in [constint,constchar,constbool] then
  1396. begin
  1397. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1398. instr.operands[operandnum].val:=pconstsym(sym)^.value;
  1399. CreateVarInstr := TRUE;
  1400. Exit;
  1401. end;
  1402. end;
  1403. typesym : begin
  1404. if ptypesym(sym)^.definition^.deftype in [recorddef,objectdef] then
  1405. begin
  1406. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1407. instr.operands[operandnum].val:=0;
  1408. CreateVarInstr := TRUE;
  1409. Exit;
  1410. end;
  1411. end;
  1412. procsym : begin
  1413. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  1414. Message(assem_w_calling_overload_func);
  1415. { free the memory before changing the symbol name. }
  1416. if assigned(instr.operands[operandnum].ref.symbol) then
  1417. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1418. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1419. instr.operands[operandnum].size:=S_L;
  1420. instr.operands[operandnum].symbol:=newpasstr(pprocsym(sym)^.definition^.mangledname);
  1421. CreateVarInstr := TRUE;
  1422. Exit;
  1423. end;
  1424. else
  1425. begin
  1426. Message(assem_e_unsupported_symbol_type);
  1427. exit;
  1428. end;
  1429. end; {case}
  1430. end; { end looking for global variables .. }
  1431. end;
  1432. Function SearchLabel(const s: string; var hl: plabel): boolean;
  1433. {**********************************************************************}
  1434. { Description: Searches for a pascal label definition, first in the }
  1435. { local symbol list and then in the global symbol list. If found then }
  1436. { return pointer to label and return true, otherwise returns false. }
  1437. {**********************************************************************}
  1438. var
  1439. sym: psym;
  1440. Begin
  1441. SearchLabel := FALSE;
  1442. if assigned(aktprocsym) then
  1443. Begin
  1444. { Check the local constants }
  1445. if assigned(aktprocsym^.definition) then
  1446. Begin
  1447. if assigned(aktprocsym^.definition^.localst) then
  1448. sym := aktprocsym^.definition^.localst^.search(s)
  1449. else
  1450. sym := nil;
  1451. if assigned(sym) then
  1452. Begin
  1453. if (sym^.typ = labelsym) then
  1454. Begin
  1455. hl:=plabelsym(sym)^.number;
  1456. SearchLabel := TRUE;
  1457. exit;
  1458. end;
  1459. end;
  1460. end;
  1461. end;
  1462. { Check the global label symbols... }
  1463. getsym(s,false);
  1464. if srsym <> nil then
  1465. Begin
  1466. if (srsym^.typ=labelsym) then
  1467. Begin
  1468. hl:=plabelsym(srsym)^.number;
  1469. SearchLabel:= TRUE;
  1470. exit;
  1471. end;
  1472. end;
  1473. end;
  1474. {*************************************************************************}
  1475. { Instruction Generation Utilities }
  1476. {*************************************************************************}
  1477. Procedure ConcatString(p : paasmoutput;s:string);
  1478. {*********************************************************************}
  1479. { PROCEDURE ConcatString(s:string); }
  1480. { Description: This routine adds the character chain pointed to in }
  1481. { s to the instruction linked list. }
  1482. {*********************************************************************}
  1483. Var
  1484. pc: PChar;
  1485. Begin
  1486. getmem(pc,length(s)+1);
  1487. p^.concat(new(pai_string,init_length_pchar(strpcopy(pc,s),length(s))));
  1488. end;
  1489. Procedure ConcatPasString(p : paasmoutput;s:string);
  1490. {*********************************************************************}
  1491. { PROCEDURE ConcatPasString(s:string); }
  1492. { Description: This routine adds the character chain pointed to in }
  1493. { s to the instruction linked list, contrary to ConcatString it }
  1494. { uses a pascal style string, so it conserves null characters. }
  1495. {*********************************************************************}
  1496. Begin
  1497. p^.concat(new(pai_string,init(s)));
  1498. end;
  1499. Procedure ConcatDirect(p : paasmoutput;s:string);
  1500. {*********************************************************************}
  1501. { PROCEDURE ConcatDirect(s:string) }
  1502. { Description: This routine output the string directly to the asm }
  1503. { output, it is only sed when writing special labels in AT&T mode, }
  1504. { and should not be used without due consideration, since it may }
  1505. { cause problems. }
  1506. {*********************************************************************}
  1507. Var
  1508. pc: PChar;
  1509. Begin
  1510. getmem(pc,length(s)+1);
  1511. p^.concat(new(pai_direct,init(strpcopy(pc,s))));
  1512. end;
  1513. Procedure ConcatConstant(p: paasmoutput; value: longint; maxvalue: longint);
  1514. {*********************************************************************}
  1515. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1516. { Description: This routine adds the value constant to the current }
  1517. { instruction linked list. }
  1518. { maxvalue -> indicates the size of the data to initialize: }
  1519. { $ff -> create a byte node. }
  1520. { $ffff -> create a word node. }
  1521. { $ffffffff -> create a dword node. }
  1522. {*********************************************************************}
  1523. Begin
  1524. if value > maxvalue then
  1525. Begin
  1526. Message(assem_e_constant_out_of_bounds);
  1527. { assuming a value of maxvalue }
  1528. value := maxvalue;
  1529. end;
  1530. if maxvalue = $ff then
  1531. p^.concat(new(pai_const,init_8bit(byte(value))))
  1532. else
  1533. if maxvalue = $ffff then
  1534. p^.concat(new(pai_const,init_16bit(word(value))))
  1535. else
  1536. if maxvalue = $ffffffff then
  1537. p^.concat(new(pai_const,init_32bit(longint(value))));
  1538. end;
  1539. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  1540. {***********************************************************************}
  1541. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1542. { Description: This routine adds the value constant to the current }
  1543. { instruction linked list. }
  1544. { real_typ -> indicates the type of the real data to initialize: }
  1545. { s32real -> create a single node. }
  1546. { s64real -> create a double node. }
  1547. { s80real -> create an extended node. }
  1548. { s64bit -> create a comp node. }
  1549. { f32bit -> create a fixed node. (not used normally) }
  1550. {***********************************************************************}
  1551. Begin
  1552. case real_typ of
  1553. s32real : p^.concat(new(pai_single,init(value)));
  1554. s64real : p^.concat(new(pai_double,init(value)));
  1555. s80real : p^.concat(new(pai_extended,init(value)));
  1556. s64bit : p^.concat(new(pai_comp,init(value)));
  1557. f32bit : p^.concat(new(pai_const,init_32bit(trunc(value*$10000))));
  1558. end;
  1559. end;
  1560. Procedure ConcatLabel(p: paasmoutput;op : tasmop;var l : plabel);
  1561. {*********************************************************************}
  1562. { PROCEDURE ConcatLabel }
  1563. { Description: This routine either emits a label or a labeled }
  1564. { instruction to the linked list of instructions. }
  1565. {*********************************************************************}
  1566. begin
  1567. if op=A_LABEL then
  1568. p^.concat(new(pai_label,init(l)))
  1569. else
  1570. p^.concat(new(pai_labeled,init(op,l)))
  1571. end;
  1572. procedure ConcatPublic(p:paasmoutput;const s : string);
  1573. {*********************************************************************}
  1574. { PROCEDURE ConcatPublic }
  1575. { Description: This routine emits an global definition to the }
  1576. { linked list of instructions.(used by AT&T styled asm) }
  1577. {*********************************************************************}
  1578. begin
  1579. p^.concat(new(pai_symbol,init_global(s)));
  1580. { concat_internal(s,EXT_NEAR); done in aasm }
  1581. end;
  1582. procedure ConcatLocal(p:paasmoutput;const s : string);
  1583. {*********************************************************************}
  1584. { PROCEDURE ConcatLocal }
  1585. { Description: This routine emits an local definition to the }
  1586. { linked list of instructions. }
  1587. {*********************************************************************}
  1588. begin
  1589. p^.concat(new(pai_symbol,init(s)));
  1590. { concat_internal(s,EXT_NEAR); done in aasm }
  1591. end;
  1592. Procedure ConcatGlobalBss(const s : string;size : longint);
  1593. {*********************************************************************}
  1594. { PROCEDURE ConcatGlobalBss }
  1595. { Description: This routine emits an global datablock to the }
  1596. { linked list of instructions. }
  1597. {*********************************************************************}
  1598. begin
  1599. bsssegment^.concat(new(pai_datablock,init_global(s,size)));
  1600. { concat_internal(s,EXT_NEAR); done in aasm }
  1601. end;
  1602. Procedure ConcatLocalBss(const s : string;size : longint);
  1603. {*********************************************************************}
  1604. { PROCEDURE ConcatLocalBss }
  1605. { Description: This routine emits a local datablcok to the }
  1606. { linked list of instructions. }
  1607. {*********************************************************************}
  1608. begin
  1609. bsssegment^.concat(new(pai_datablock,init(s,size)));
  1610. { concat_internal(s,EXT_NEAR); done in aasm }
  1611. end;
  1612. { add to list of external labels }
  1613. Procedure ConcatExternal(const s : string;typ : texternal_typ);
  1614. {*********************************************************************}
  1615. { PROCEDURE ConcatExternal }
  1616. { Description: This routine emits an external definition to the }
  1617. { linked list of instructions.(used by AT&T styled asm) }
  1618. {*********************************************************************}
  1619. { check if in internal list and remove it there }
  1620. var p : pai_external;
  1621. begin
  1622. p:=search_assembler_symbol(internals,s,typ);
  1623. if p<>nil then internals^.remove(p);
  1624. concat_external(s,typ);
  1625. end;
  1626. { add to internal list of labels }
  1627. Procedure ConcatInternal(const s : string;typ : texternal_typ);
  1628. {*********************************************************************}
  1629. { PROCEDURE ConcatInternal }
  1630. { Description: This routine emits an internal definition of a symbol }
  1631. { (used by AT&T styled asm for undefined labels) }
  1632. {*********************************************************************}
  1633. begin
  1634. concat_internal(s,typ);
  1635. end;
  1636. end.
  1637. {
  1638. $Log$
  1639. Revision 1.15 1998-11-17 00:26:11 peter
  1640. * fixed for $H+
  1641. Revision 1.14 1998/11/05 23:48:17 peter
  1642. * recordtype.field support in constant expressions
  1643. * fixed imul for oa_imm8 which was not allowed
  1644. * fixed reading of local typed constants
  1645. * fixed comment reading which is not any longer a separator
  1646. Revision 1.13 1998/10/28 00:08:45 peter
  1647. + leal procsym,eax is now allowed
  1648. + constants are now handled also when starting an expression
  1649. + call *pointer is now allowed
  1650. Revision 1.12 1998/10/14 11:28:13 florian
  1651. * emitpushreferenceaddress gets now the asmlist as parameter
  1652. * m68k version compiles with -duseansistrings
  1653. Revision 1.11 1998/10/13 16:49:59 pierre
  1654. * undid some changes of Peter that made the compiler wrong
  1655. for m68k (I had to reinsert some ifdefs)
  1656. * removed several memory leaks under m68k
  1657. * removed the meory leaks for assembler readers
  1658. * cross compiling shoud work again better
  1659. ( crosscompiling sysamiga works
  1660. but as68k still complain about some code !)
  1661. Revision 1.10 1998/10/13 13:10:10 peter
  1662. * new style for m68k/i386 infos and enums
  1663. Revision 1.9 1998/09/24 17:54:15 carl
  1664. * bugfixes from fix branch
  1665. Revision 1.8.2.1 1998/09/24 17:46:25 carl
  1666. * support for objects in asm statements
  1667. Revision 1.8 1998/08/27 00:43:06 carl
  1668. +} now record offsets searches set the operand sizes
  1669. Revision 1.7 1998/08/18 20:51:32 peter
  1670. * fixed bug 42
  1671. Revision 1.6 1998/08/10 14:49:40 peter
  1672. + localswitches, moduleswitches, globalswitches splitting
  1673. Revision 1.5 1998/07/14 21:46:38 peter
  1674. * updated messages file
  1675. Revision 1.4 1998/06/04 23:51:31 peter
  1676. * m68k compiles
  1677. + .def file creation moved to gendef.pas so it could also be used
  1678. for win32
  1679. Revision 1.3 1998/05/31 14:13:30 peter
  1680. * fixed call bugs with assembler readers
  1681. + OPR_SYMBOL to hold a symbol in the asm parser
  1682. * fixed staticsymtable vars which were acessed through %ebp instead of
  1683. name
  1684. Revision 1.2 1998/04/29 10:33:43 pierre
  1685. + added some code for ansistring (not complete nor working yet)
  1686. * corrected operator overloading
  1687. * corrected nasm output
  1688. + started inline procedures
  1689. + added starstarn : use ** for exponentiation (^ gave problems)
  1690. + started UseTokenInfo cond to get accurate positions
  1691. }