From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: * X-Spam-Status: No, score=1.3 required=5.0 tests=BAYES_00,INVALID_MSGID, MSGID_RANDY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1ea19776e3073a96 X-Google-Attributes: gid103376,public From: Jim Rogers Subject: Re: C/C++ programmer giving Ada95 a chance -- writing an emulator. Date: 2000/03/28 Message-ID: <8bqoeh$9gs$1@nnrp1.deja.com>#1/1 X-Deja-AN: 603466541 References: <38e148e2.5089627@news.shreve.net> <38E05E84.BB5507FC@gmx.net> X-Http-Proxy: 1.1 x23.deja.com:80 (Squid/1.1.22) for client 205.170.64.185 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Tue Mar 28 17:00:12 2000 GMT X-MyDeja-Info: XMYDJUIDada_daddy Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows 95; EXCITE) Date: 2000-03-28T00:00:00+00:00 List-Id: In article <38E05E84.BB5507FC@gmx.net>, > jross wrote: > > My first question is how do you define and write a dispatching > mechanism as you would in C++ using function pointers in a table for > execution speed efficiency, like this: > > --------------------------------- > -- the .h file > --------------------------------- > > class TACpu : public TCpu > { > ? define registers and much stuff (not shown) > > struct OpStruct // define the op code jump table structure > { > char Byte; // the opcode byte for reference only > void (TACpu::*Func)(); // executes opcode > }; > > static OpStruct OpTable[]; // the table in the .cpp file > > void Op_U(); // undefined > void Op_00(); // functions that execute the opcodes > void Op_01(); > void Op_02(); > void Op_03(); > // ? thru Op_FF > }; > > --------------------------------- > -- in the .cpp file > --------------------------------- > > TACpu::OpStruct TACpu::OpTable[] = { // opcode table > { 0x00, Op_00 }, > { 0x01, Op_01 }, > { 0x02, Op_U }, > { 0x03, Op_03 }, > // ? thru Op_FF > }; > > ... the Op_xx functions would go here > > now the code for calling the opcodes > > IR = Core[PA++]; // get the opcode, advance the program address > (this->*OpTable[IR].Func)(); // execute it > > ---------------- > > This is very efficient C++ code (could have been just as easy in plain > old C without the class encapsulation?) How would you do this in Ada > without using a Case Statement? > There are some differences in the Ada approach and the C++ approach. What you are asking is if Ada can be C++. The answer is that Ada is only moderately good at being C++. Some differences: Ada does not have an equivalent of a ++ operator. All your C++ functions would be implemented as Ada procedures. You can build a similar table of access to procedures in Ada. This would not be what Ada describes as a dispatching operation. Ada uses the term dispatching to refer to what in C++ would be calling a virtual function through the vtbl. The Ada specification for the procedures and access types would look something like: type Proc_Ptr is access procedure; type OpCode is range 16#00#..16#FF#; type Opcode_Table is array(OpCode) of Proc_Ptr; procedure Op_00; procedure Op_01; procedure Op_02; ... OpTable : Opcode_Table := ( 16#00# => Op_00'Access, 16#02# => Op_01'Access, 16#03# => Op_02'Access, ... ); IR : OpCode; function Core(Item : in System.Address) return OpCode; Now get the opcode, advance the program address, execute the opcode IR := Core(PA); PA := PA + 1; OpTable(IR).all; -- Jim Rogers Colorado Springs, Colorado USA Sent via Deja.com http://www.deja.com/ Before you buy.