Skip to content

Commit

Permalink
disasm: Separated Region
Browse files Browse the repository at this point in the history
  • Loading branch information
mefistotelis committed Jan 8, 2024
1 parent ffa4feb commit 258c6bb
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 99 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ swdisasm_SOURCES = \
le.cpp \
le_image.h \
le_image.cpp \
regions.h \
regions.cpp \
swdisasm.cpp \
util.h \
util.cpp
Expand Down
100 changes: 100 additions & 0 deletions src/regions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* swdisasm - LE disassembler for Syndicate Wars
*
* Copyright (C) 2010 Unavowed <unavowed@vexillium.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "regions.h"
#include "util.h"

using std::ios;

Region::Region (uint32_t address, uint32_t size, Region::Type type)
{
this->address = address;
this->size = size;
this->type = type;
}

Region::Region (void)
{
this->address = 0;
this->size = 0;
this->type = UNKNOWN;
}

Region::Region (const Region &other)
{
*this = other;
}

uint32_t
Region::get_address (void) const
{
return this->address;
}

size_t
Region::get_end_address (void) const
{
return (this->address + this->size);
}

Region::Type
Region::get_type (void) const
{
return this->type;
}

bool
Region::contains_address (uint32_t addr) const
{
return (this->address <= addr and addr < this->address + this->size);
}

size_t
Region::get_size (void) const
{
return this->size;
}

std::ostream &
operator<< (std::ostream &os, Region::Type type)
{
switch (type)
{
case Region::UNKNOWN: os << "unknown"; break;
case Region::CODE: os << "code"; break;
case Region::DATA: os << "data"; break;
case Region::VTABLE: os << "vtable"; break;
default: os << "(unknown)"; break;
}
return os;
}

std::ostream &
operator<< (std::ostream &os, const Region &reg)
{
PUSH_IOS_FLAGS (&os);

os.setf (ios::hex, ios::basefield);
os.setf (ios::showbase);

os << reg.get_type () << " at " << reg.get_address ()
<< ", size " << reg.get_size ();

return os;
}
58 changes: 58 additions & 0 deletions src/regions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* swdisasm - LE disassembler for Syndicate Wars
*
* Copyright (C) 2010 Unavowed <unavowed@vexillium.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <inttypes.h>
#include <iostream>
#include <map>

class Analyser;

class Region
{
protected:
friend class Analyser;

public:
enum Type
{
UNKNOWN,
CODE,
DATA,
VTABLE
};

protected:
uint32_t address;
uint32_t size;
Region::Type type;

public:
Region (uint32_t address, uint32_t size = 1, Region::Type type = UNKNOWN);
Region (void);
Region (const Region &other);

uint32_t get_address (void) const;
size_t get_end_address (void) const;
Region::Type get_type (void) const;
bool contains_address (uint32_t addr) const;
size_t get_size (void) const;
};

std::ostream &operator<< (std::ostream &os, Region::Type type);
std::ostream &operator<< (std::ostream &os, const Region &reg);
100 changes: 1 addition & 99 deletions src/swdisasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "instruction.h"
#include "le.h"
#include "le_image.h"
#include "regions.h"
#include "util.h"

using std::ios;
Expand All @@ -45,105 +46,6 @@ typedef LinearExecutable::ObjectHeader LEOH;
class Analyser;


class Region
{
protected:
friend class Analyser;

public:
enum Type
{
UNKNOWN,
CODE,
DATA,
VTABLE
};

protected:
uint32_t address;
uint32_t size;
Type type;

public:
Region (uint32_t address, uint32_t size = 1, Type type = UNKNOWN)
{
this->address = address;
this->size = size;
this->type = type;
}

Region (void)
{
this->address = 0;
this->size = 0;
this->type = UNKNOWN;
}

Region (const Region &other)
{
*this = other;
}

uint32_t
get_address (void) const
{
return this->address;
}

size_t
get_end_address (void) const
{
return (this->address + this->size);
}

Type
get_type (void) const
{
return this->type;
}

bool
contains_address (uint32_t addr) const
{
return (this->address <= addr and addr < this->address + this->size);
}

size_t
get_size (void) const
{
return this->size;
}
};

std::ostream &
operator<< (std::ostream &os, Region::Type type)
{
switch (type)
{
case Region::UNKNOWN: os << "unknown"; break;
case Region::CODE: os << "code"; break;
case Region::DATA: os << "data"; break;
case Region::VTABLE: os << "vtable"; break;
default: os << "(unknown)"; break;
}
return os;
}

std::ostream &
operator<< (std::ostream &os, const Region &reg)
{
PUSH_IOS_FLAGS (&os);

os.setf (ios::hex, ios::basefield);
os.setf (ios::showbase);

os << reg.get_type () << " at " << reg.get_address ()
<< ", size " << reg.get_size ();

return os;
}


class Label
{
public:
Expand Down

0 comments on commit 258c6bb

Please sign in to comment.