473,670 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enumerators, Templates, and Type Safety

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
Jul 22 '05 #1
6 1807
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
Jul 22 '05 #2
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,R EG16::BP);

Add(REG8::BH,RE G8::DH);

Add(REG16::BX,R EG8::AL); //COMPILE ERROR
}
-JKop
Jul 22 '05 #3
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::REG1 6 x, REG16::REG16 y);

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

-JKop
Jul 22 '05 #4
"JKop" <NU**@NULL.NULL > wrote in message
news:5f******** *********@news. indigo.ie...
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,R EG16::BP);

Add(REG8::BH,RE G8::DH);

Add(REG16::BX,R EG8::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
Jul 22 '05 #5
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:vo******** ********@ord-read.news.verio .net...
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
Jul 22 '05 #6
Matt Taylor wrote:
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:vo******** ********@ord-read.news.verio .net...
[...]

[..] 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
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4641
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived class version from the base class pointer and still be able to use a vector with derived element pointers? class BaseElem { }; class DerivedElem1 { };
2
2161
by: kelvSYC | last post by:
I'm trying to program something along the lines of a "trading card" idea: I have a single copy of the "card" in the program, yet there may be multiple "instances" of the "card", with differing information (such as the owner of the "card") in each instance. struct Person; Person Bob; Person Joe;
2
2270
by: Steve Jorgensen | last post by:
I frequently find myself wanting to use class abstraction in VB/VBA code, and frankly, with the tacked-on class support in VB/VBA, there are some problems with trying to do that and have any type-safety as well. I thought I would share some of what I've come to think about this after dealing with it several times of late. First, an example. Let's say I have several classes, each with a string property called Name, and I have several...
18
2149
by: aarklon | last post by:
In the article http://en.wikipedia.org/wiki/Type_safety it is written as The archetypal type-unsafe language is C because (for example) it is possible for an integer to be viewed as a function pointer, which can then be jumped to and executed, causing errors such as segmentation faults, or (more insidiously) silent failures. Even though the semantics of the C language explicitly allow for these
11
22349
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the type specified. This is much similar like a macro expansion in C. can anyone please explain advantages of one over the other ? Thanks in advance -
23
2589
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624. I read the description, decided that it's exactly what I want, and ignored the warning. Now I'm trying to inherit using a template. Instead of "destructor could not be generated because a base class destructor is inaccessible", I now have an...
104
4562
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a feeling the answer I'm going to get back will be "no, because templates have taken on a life of their own since their original conception and now also affect compiler implementation" (read: not good, IMO. John
21
3608
by: Chad | last post by:
Okay, so like recently the whole idea of using a Union in C finally sunk into my skull. Seriously, I think it probably took me 2 years to catch on what a Union really is. Belated, I mentioned this too my ultra smart friend who just quit working as a CTO of a wireless company so he go complete his PhD in particle physics. Anyhow he mentioned that Unions in C are not typesafe. Now, how is it possible to violate type safety in Unions? ...
1
1492
muaddubby
by: muaddubby | last post by:
Hello all and happy new year. I've seen several posts floating around asking about string enumerators in C#, and generally speaking, they're not supported. I've come up with a way around it that lets you create a class which behaves exactly like the regular enumerators (it offers all the functionality a regular enumerator has), and you can store any type of string in it. The full article can be found here:...
0
8468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8901
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6213
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4209
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.