Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Enumerators, Templates, and Type Safety

Reply
Thread Tools

Enumerators, Templates, and Type Safety

 
 
Matt Taylor
Guest
Posts: n/a
 
      07-02-2004
I'm trying to write an x86 assembler in C++ for use in a debugger. What I'd
like do is to use template specialization to prevent invalid combinations
from compiling. Thus one could not accidentally add a 16-bit register and an
8-bit register since there is no encoding for this on the x86 architecture.

My trouble has stemmed from the fact that enumerators are only integers and
can be freely cast into other enumerators, and I was using them to attempt
type safety. I declared REG8, REG16, and REG32 enumerated types, but the
compiler will implicitly cast between them. So, my question is: is there any
way to create 3 distinct types that I can use in template specialization to
enforce assembler operand restrictions at compile-time?

-Matt


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-02-2004
Matt Taylor wrote:
> I'm trying to write an x86 assembler in C++ for use in a debugger. What I'd
> like do is to use template specialization to prevent invalid combinations
> from compiling. Thus one could not accidentally add a 16-bit register and an
> 8-bit register since there is no encoding for this on the x86 architecture.
>
> My trouble has stemmed from the fact that enumerators are only integers and
> can be freely cast into other enumerators, and I was using them to attempt
> type safety. I declared REG8, REG16, and REG32 enumerated types, but the
> compiler will implicitly cast between them. So, my question is: is there any
> way to create 3 distinct types that I can use in template specialization to
> enforce assembler operand restrictions at compile-time?


How about

class REG8 {} AL,AH,BL,BH,CL,CH,DL,DH;
class REG16 {} AX,BX,CX,DX,SI,DI,BP,SP,IP;
class REG32 {} EAX ...

Now you have types, you have objects. You can't easily use them
in a 'switch', but do you care? There is no conversion between
any of them, and you can specialise your templates based on them
as much as you can specialise any templates.

V
 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      07-03-2004
Victor Bazarov posted:

> Matt Taylor wrote:
>> I'm trying to write an x86 assembler in C++ for use in a debugger.
>> What I'd like do is to use template specialization to prevent invalid
>> combinations from compiling. Thus one could not accidentally add a
>> 16-bit register and an 8-bit register since there is no encoding for
>> this on the x86 architecture.
>>
>> My trouble has stemmed from the fact that enumerators are only
>> integers and can be freely cast into other enumerators, and I was
>> using them to attempt type safety. I declared REG8, REG16, and REG32
>> enumerated types, but the compiler will implicitly cast between them.
>> So, my question is: is there any way to create 3 distinct types that I
>> can use in template specialization to enforce assembler operand
>> restrictions at compile-time?

>
> How about
>
> class REG8 {} AL,AH,BL,BH,CL,CH,DL,DH;
> class REG16 {} AX,BX,CX,DX,SI,DI,BP,SP,IP;
> class REG32 {} EAX ...
>
> Now you have types, you have objects. You can't easily use them
> in a 'switch', but do you care? There is no conversion between
> any of them, and you can specialise your templates based on them
> as much as you can specialise any templates.
>
> V


or maybe

namespace REG8 {
enum REG8 {

AL,AH,BL,BH,CL,CH,DL,DH }}

namespace REG16 {
enum REG16 {
AX,BX,CX,DX,SI,DI,BP,SP,IP }}


void Add(REG16 x, REG16 y);

void Add(REG8 x, REG8 y);


int main()
{
Add(REG16::BX,REG16::BP);

Add(REG8::BH,REG8:H);

Add(REG16::BX,REG8::AL); //COMPILE ERROR
}


-JKop
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-03-2004
JKop posted:

> Victor Bazarov posted:
>
>> Matt Taylor wrote:
>>> I'm trying to write an x86 assembler in C++ for use in a debugger.
>>> What I'd like do is to use template specialization to prevent invalid
>>> combinations from compiling. Thus one could not accidentally add a
>>> 16-bit register and an 8-bit register since there is no encoding for
>>> this on the x86 architecture.
>>>
>>> My trouble has stemmed from the fact that enumerators are only
>>> integers and can be freely cast into other enumerators, and I was
>>> using them to attempt type safety. I declared REG8, REG16, and REG32
>>> enumerated types, but the compiler will implicitly cast between them.
>>> So, my question is: is there any way to create 3 distinct types that I
>>> can use in template specialization to enforce assembler operand
>>> restrictions at compile-time?

>>
>> How about
>>
>> class REG8 {} AL,AH,BL,BH,CL,CH,DL,DH;
>> class REG16 {} AX,BX,CX,DX,SI,DI,BP,SP,IP;
>> class REG32 {} EAX ...
>>
>> Now you have types, you have objects. You can't easily use them
>> in a 'switch', but do you care? There is no conversion between
>> any of them, and you can specialise your templates based on them
>> as much as you can specialise any templates.
>>
>> V

>
> or maybe
>
> namespace REG8 {
> enum REG8 {
>
> AL,AH,BL,BH,CL,CH,DL,DH }}
>
> namespace REG16 {
> enum REG16 {
> AX,BX,CX,DX,SI,DI,BP,SP,IP }}
>
>
> void Add(REG16 x, REG16 y);
>
> void Add(REG8 x, REG8 y);


Opps!

void Add(REG16::REG16 x, REG16::REG16 y);

void Add(REG8::REG8 x, REG8::REG8 y);

-JKop
 
Reply With Quote
 
Matt Taylor
Guest
Posts: n/a
 
      07-07-2004
"JKop" <(E-Mail Removed)> wrote in message
news:5fvFc.3745$(E-Mail Removed)...
> Victor Bazarov posted:
>
> > Matt Taylor wrote:
> >> I'm trying to write an x86 assembler in C++ for use in a debugger.
> >> What I'd like do is to use template specialization to prevent invalid
> >> combinations from compiling. Thus one could not accidentally add a
> >> 16-bit register and an 8-bit register since there is no encoding for
> >> this on the x86 architecture.
> >>
> >> My trouble has stemmed from the fact that enumerators are only
> >> integers and can be freely cast into other enumerators, and I was
> >> using them to attempt type safety. I declared REG8, REG16, and REG32
> >> enumerated types, but the compiler will implicitly cast between them.
> >> So, my question is: is there any way to create 3 distinct types that I
> >> can use in template specialization to enforce assembler operand
> >> restrictions at compile-time?

> >
> > How about
> >
> > class REG8 {} AL,AH,BL,BH,CL,CH,DL,DH;
> > class REG16 {} AX,BX,CX,DX,SI,DI,BP,SP,IP;
> > class REG32 {} EAX ...
> >
> > Now you have types, you have objects. You can't easily use them
> > in a 'switch', but do you care? There is no conversion between
> > any of them, and you can specialise your templates based on them
> > as much as you can specialise any templates.
> >
> > V

>
> or maybe
>
> namespace REG8 {
> enum REG8 {
>
> AL,AH,BL,BH,CL,CH,DL,DH }}
>
> namespace REG16 {
> enum REG16 {
> AX,BX,CX,DX,SI,DI,BP,SP,IP }}
>
>
> void Add(REG16 x, REG16 y);
>
> void Add(REG8 x, REG8 y);
>
>
> int main()
> {
> Add(REG16::BX,REG16::BP);
>
> Add(REG8::BH,REG8:H);
>
> Add(REG16::BX,REG8::AL); //COMPILE ERROR
> }


What about template specialization, e.g. Add<REG16::BX, REG8::AL>()? I
suppose I can live with passing parameters, but I'd hoped to use
specialization to force the compiler to inline everything.

-Matt


 
Reply With Quote
 
Matt Taylor
Guest
Posts: n/a
 
      07-13-2004
"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:vojFc.1491$(E-Mail Removed)...
> Matt Taylor wrote:
> > I'm trying to write an x86 assembler in C++ for use in a debugger. What

I'd
> > like do is to use template specialization to prevent invalid

combinations
> > from compiling. Thus one could not accidentally add a 16-bit register

and an
> > 8-bit register since there is no encoding for this on the x86

architecture.
> >
> > My trouble has stemmed from the fact that enumerators are only integers

and
> > can be freely cast into other enumerators, and I was using them to

attempt
> > type safety. I declared REG8, REG16, and REG32 enumerated types, but the
> > compiler will implicitly cast between them. So, my question is: is there

any
> > way to create 3 distinct types that I can use in template specialization

to
> > enforce assembler operand restrictions at compile-time?

>
> How about
>
> class REG8 {} AL,AH,BL,BH,CL,CH,DL,DH;
> class REG16 {} AX,BX,CX,DX,SI,DI,BP,SP,IP;
> class REG32 {} EAX ...
>
> Now you have types, you have objects. You can't easily use them
> in a 'switch', but do you care? There is no conversion between
> any of them, and you can specialise your templates based on them
> as much as you can specialise any templates.


This seems to work pretty well. I actually ended up creating a generic class
that represents each register type, i.e. REG8, REG16, and REG32. They are
templated with an integer, i.e. EAX = REG32<0>. Now I can use partial
specialization to easily select between register types. Thanks!

-Matt


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-13-2004
Matt Taylor wrote:
> "Victor Bazarov" <(E-Mail Removed)> wrote in message
> news:vojFc.1491$(E-Mail Removed)...
>
>>[...]

> [..] I actually ended up creating a generic class
> that represents each register type, i.e. REG8, REG16, and REG32. They are
> templated with an integer, i.e. EAX = REG32<0>. Now I can use partial
> specialization to easily select between register types. Thanks!


It's very gratifying to interact with somebody who understands what
you're talking about and uses it successfully. Thank you. -- V
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
type safety and reinterpret_cast<> Noah Roberts C++ 27 11-08-2006 01:44 PM
Implementing Interfaces and Type Safety (OOP Newbie in Perl) Veli-Pekka Tštilš Perl Misc 7 08-20-2005 10:02 AM
newbie question on "Type Safety" Raymond Du ASP .Net 1 06-21-2004 11:42 AM
type safety in java 1.5 (bug or hole?) York Werres Java 24 05-25-2004 06:05 PM
Runtime type-safety (for linked lists) Dave C Programming 2 09-29-2003 09:42 AM



Advertisments