Skip to content

Commit

Permalink
Don't create special objects for NULL, just use NULL directly.
Browse files Browse the repository at this point in the history
Also move null check into PycRef for nullable references.
  • Loading branch information
zrax committed Jul 5, 2017
1 parent 1329626 commit b9dd99d
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 155 deletions.
2 changes: 0 additions & 2 deletions ASTNode.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "ASTNode.h"

PycRef<ASTNode> Node_NULL = (ASTNode*)0;

/* ASTNodeList */
void ASTNodeList::removeLast()
{
Expand Down
15 changes: 6 additions & 9 deletions ASTNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ class ASTNode {
void delRef() { internalDelRef(this); }
};

/* A NULL node for comparison */
extern PycRef<ASTNode> Node_NULL;


class ASTNodeList : public ASTNode {
public:
Expand Down Expand Up @@ -157,8 +154,8 @@ class ASTSlice : public ASTBinary {
SLICE0, SLICE1, SLICE2, SLICE3
};

ASTSlice(int op, PycRef<ASTNode> left = Node_NULL,
PycRef<ASTNode> right = Node_NULL)
ASTSlice(int op, PycRef<ASTNode> left = NULL,
PycRef<ASTNode> right = NULL)
: ASTBinary(left, right, op, NODE_SLICE) { }
};

Expand Down Expand Up @@ -258,16 +255,16 @@ class ASTCall : public ASTNode {

ASTCall(PycRef<ASTNode> func, pparam_t pparams, kwparam_t kwparams)
: ASTNode(NODE_CALL), m_func(func), m_pparams(pparams), m_kwparams(kwparams),
m_var(Node_NULL), m_kw(Node_NULL) { }
m_var(), m_kw() { }

PycRef<ASTNode> func() const { return m_func; }
const pparam_t& pparams() const { return m_pparams; }
const kwparam_t& kwparams() const { return m_kwparams; }
PycRef<ASTNode> var() const { return m_var; }
PycRef<ASTNode> kw() const { return m_kw; }

bool hasVar() const { return m_var != Node_NULL; }
bool hasKW() const { return m_kw != Node_NULL; }
bool hasVar() const { return m_var != NULL; }
bool hasKW() const { return m_kw != NULL; }

void setVar(PycRef<ASTNode> var) { m_var = var; }
void setKW(PycRef<ASTNode> kw) { m_kw = kw; }
Expand Down Expand Up @@ -363,7 +360,7 @@ class ASTSubscr : public ASTNode {

class ASTPrint : public ASTNode {
public:
ASTPrint(PycRef<ASTNode> value, PycRef<ASTNode> stream = Node_NULL)
ASTPrint(PycRef<ASTNode> value, PycRef<ASTNode> stream = NULL)
: ASTNode(NODE_PRINT), m_value(value), m_stream(stream) { }

PycRef<ASTNode> value() const { return m_value; }
Expand Down
226 changes: 113 additions & 113 deletions ASTree.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions FastStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class FastStack {

void pop() {
if (m_ptr > -1)
m_stack[m_ptr--] = Node_NULL;
m_stack[m_ptr--] = NULL;
}

PycRef<ASTNode> top() const
{
if (m_ptr > -1)
return m_stack[m_ptr];
else
return Node_NULL;
return NULL;
}

void replace(const FastStack& copy)
Expand Down
5 changes: 5 additions & 0 deletions bytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ bool Pyc::IsCompareArg(int opcode)

void print_const(PycRef<PycObject> obj, PycModule* mod)
{
if (obj == NULL) {
fprintf(pyc_output, "<NULL>");
return;
}

switch (obj->type()) {
case PycObject::TYPE_STRING:
OutputString(obj.cast<PycString>(), (mod->majorVer() == 3) ? 'b' : 0);
Expand Down
4 changes: 2 additions & 2 deletions pyc_numeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void PycLong::load(PycData* stream, PycModule*)

bool PycLong::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj->type())
if (type() != obj.type())
return false;

PycRef<PycLong> longObj = obj.cast<PycLong>();
Expand Down Expand Up @@ -110,7 +110,7 @@ void PycFloat::load(PycData* stream, PycModule*)

bool PycFloat::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj->type())
if (type() != obj.type())
return false;

PycRef<PycFloat> floatObj = obj.cast<PycFloat>();
Expand Down
4 changes: 2 additions & 2 deletions pyc_numeric.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PycInt : public PycObject {

bool isEqual(PycRef<PycObject> obj) const
{
return (type() == obj->type()) &&
return (type() == obj.type()) &&
(m_value == obj.cast<PycInt>()->m_value);
}

Expand Down Expand Up @@ -85,7 +85,7 @@ class PycCFloat : public PycObject {

bool isEqual(PycRef<PycObject> obj) const
{
return (type() == obj->type()) &&
return (type() == obj.type()) &&
(m_value == obj.cast<PycCFloat>()->m_value);
}

Expand Down
7 changes: 3 additions & 4 deletions pyc_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "data.h"
#include <cstdio>

PycRef<PycObject> Pyc_NULL = (PycObject*)0;
PycRef<PycObject> Pyc_None = new PycObject(PycObject::TYPE_NONE);
PycRef<PycObject> Pyc_Ellipsis = new PycObject(PycObject::TYPE_ELLIPSIS);
PycRef<PycObject> Pyc_StopIteration = new PycObject(PycObject::TYPE_STOPITER);
Expand All @@ -16,7 +15,7 @@ PycRef<PycObject> CreateObject(int type)
{
switch (type) {
case PycObject::TYPE_NULL:
return Pyc_NULL;
return NULL;
case PycObject::TYPE_NONE:
return Pyc_None;
case PycObject::TYPE_FALSE:
Expand Down Expand Up @@ -65,7 +64,7 @@ PycRef<PycObject> CreateObject(int type)
return new PycSet(type);
default:
fprintf(stderr, "CreateObject: Got unsupported type 0x%X\n", type);
return Pyc_NULL;
return NULL;
}
}

Expand All @@ -79,7 +78,7 @@ PycRef<PycObject> LoadObject(PycData* stream, PycModule* mod)
obj = mod->getRef(index);
} else {
obj = CreateObject(type & 0x7F);
if (obj != Pyc_NULL) {
if (obj != NULL) {
if (type & 0x80)
mod->refObject(obj);
obj->load(stream, mod);
Expand Down
19 changes: 10 additions & 9 deletions pyc_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ class PycRef {
_Obj* operator->() const { return m_obj; }
operator _Obj*() const { return m_obj; }

inline int type() const;

/* This is just for coding convenience -- no type checking is done! */
template <class _Cast>
PycRef<_Cast> cast() const { return static_cast<_Cast*>(m_obj); }

bool isIdent(const _Obj* obj) { return m_obj == obj; }

private:
_Obj* m_obj;
};
Expand Down Expand Up @@ -106,33 +110,30 @@ class PycObject {
PycObject(int type = TYPE_UNKNOWN) : m_refs(0), m_type(type) { }
virtual ~PycObject() { }

int type() const { return internalGetType(this); }
int type() const { return m_type; }

virtual bool isEqual(PycRef<PycObject> obj) const
{ return (this == (PycObject*)obj); }
{ return obj.isIdent(this); }

virtual void load(PycData*, PycModule*) { }

private:
int m_refs;
int m_type;

// Hack to make clang happy :(
static int internalGetType(const PycObject *obj)
{
return obj ? obj->m_type : TYPE_NULL;
}

public:
void addRef() { ++m_refs; }
void delRef() { if (--m_refs == 0) delete this; }
};

template <class _Obj>
int PycRef<_Obj>::type() const
{ return m_obj ? m_obj->type() : PycObject::TYPE_NULL; }

PycRef<PycObject> CreateObject(int type);
PycRef<PycObject> LoadObject(PycData* stream, PycModule* mod);

/* Static Singleton objects */
extern PycRef<PycObject> Pyc_NULL;
extern PycRef<PycObject> Pyc_None;
extern PycRef<PycObject> Pyc_Ellipsis;
extern PycRef<PycObject> Pyc_StopIteration;
Expand Down
12 changes: 6 additions & 6 deletions pyc_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void PycTuple::load(PycData* stream, PycModule* mod)

bool PycTuple::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj->type())
if (type() != obj.type())
return false;

PycRef<PycTuple> tupleObj = obj.cast<PycTuple>();
Expand All @@ -44,7 +44,7 @@ void PycList::load(PycData* stream, PycModule* mod)

bool PycList::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj->type())
if (type() != obj.type())
return false;

PycRef<PycList> listObj = obj.cast<PycList>();
Expand All @@ -67,7 +67,7 @@ void PycDict::load(PycData* stream, PycModule* mod)
PycRef<PycObject> key, val;
for (;;) {
key = LoadObject(stream, mod);
if (key == Pyc_NULL)
if (key == NULL)
break;
val = LoadObject(stream, mod);
m_keys.push_back(key);
Expand All @@ -77,7 +77,7 @@ void PycDict::load(PycData* stream, PycModule* mod)

bool PycDict::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj->type())
if (type() != obj.type())
return false;

PycRef<PycDict> dictObj = obj.cast<PycDict>();
Expand Down Expand Up @@ -111,7 +111,7 @@ PycRef<PycObject> PycDict::get(PycRef<PycObject> key) const
return *vi;
++ki, ++vi;
}
return Pyc_NULL; // Disassembly shouldn't get non-existant keys
return NULL; // Disassembly shouldn't get non-existant keys
}


Expand All @@ -125,7 +125,7 @@ void PycSet::load(PycData* stream, PycModule* mod)

bool PycSet::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj->type())
if (type() != obj.type())
return false;

PycRef<PycSet> setObj = obj.cast<PycSet>();
Expand Down
2 changes: 1 addition & 1 deletion pyc_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void PycString::load(PycData* stream, PycModule* mod)

bool PycString::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj->type())
if (type() != obj.type())
return false;

PycRef<PycString> strObj = obj.cast<PycString>();
Expand Down
15 changes: 10 additions & 5 deletions pycdas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ static void iprintf(int indent, const char* fmt, ...)

void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
{
if (obj == NULL) {
iprintf(indent, "<NULL>");
return;
}

switch (obj->type()) {
case PycObject::TYPE_CODE:
case PycObject::TYPE_CODE2:
Expand All @@ -81,31 +86,31 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
iprintf(indent + 1, "Flags: 0x%08X", codeObj->flags());
print_coflags(codeObj->flags());

if (codeObj->names() != Pyc_NULL) {
if (codeObj->names() != NULL) {
iprintf(indent + 1, "[Names]\n");
for (int i=0; i<codeObj->names()->size(); i++)
output_object(codeObj->names()->get(i), mod, indent + 2);
}

if (codeObj->varNames() != Pyc_NULL) {
if (codeObj->varNames() != NULL) {
iprintf(indent + 1, "[Var Names]\n");
for (int i=0; i<codeObj->varNames()->size(); i++)
output_object(codeObj->varNames()->get(i), mod, indent + 2);
}

if (codeObj->freeVars() != Pyc_NULL) {
if (codeObj->freeVars() != NULL) {
iprintf(indent + 1, "[Free Vars]\n");
for (int i=0; i<codeObj->freeVars()->size(); i++)
output_object(codeObj->freeVars()->get(i), mod, indent + 2);
}

if (codeObj->cellVars() != Pyc_NULL) {
if (codeObj->cellVars() != NULL) {
iprintf(indent + 1, "[Cell Vars]\n");
for (int i=0; i<codeObj->cellVars()->size(); i++)
output_object(codeObj->cellVars()->get(i), mod, indent + 2);
}

if (codeObj->consts() != Pyc_NULL) {
if (codeObj->consts() != NULL) {
iprintf(indent + 1, "[Constants]\n");
for (int i=0; i<codeObj->consts()->size(); i++)
output_object(codeObj->consts()->get(i), mod, indent + 2);
Expand Down

0 comments on commit b9dd99d

Please sign in to comment.