Commited the ZSNES development toolkit.

This commit is contained in:
n-a-c-h
2005-06-01 22:54:24 +00:00
parent 8eeb084ab1
commit 97690f8d3f
7 changed files with 481 additions and 0 deletions

View File

@@ -0,0 +1,139 @@
/*
Copyright (C) 2005 Nach, grinvader ( http://www.zsnes.com )
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
This is part of a toolkit used to assist in ZSNES development
This program tells us if an assembly file has extra EXTSYMs.
*/
#include <iostream>
#include <fstream>
#include <set>
#include <list>
#include <queue>
#include "strutil.h"
using namespace std;
#include "fileutil.h"
set<string> ignore_include_file;
void handle_file(const char *filename)
{
queue<string> included_files;
set<string> extsyms;
set<string> used_vars;
list<string> not_used_extsyms;
included_files.push(filename);
do
{
string fname = included_files.front();
included_files.pop();
ifstream file(fname.c_str(), ios::in);
if (file)
{
char line[500];
//Build lists
for (size_t i = 0; file.getline(line, 500); i++)
{
vector<string> tokens;
char *p = line;
while (isspace(*p)) { p++; }
if (!strncasecmp(p, "%include ", strlen("%include ")))
{
p += strlen("%include ");
Tokenize(p, tokens, ";");
string not_commented = tokens[0];
tokens.clear();
Tokenize(not_commented, tokens, "/\" \t");
included_files.push(*(tokens.end()-1));
}
else if (!strncasecmp(p, "extsym ", strlen("extsym ")))
{
p += strlen("extsym ");
Tokenize(p, tokens, ";");
string not_commented = tokens[0];
tokens.clear();
Tokenize(not_commented, tokens, ", ");
for (vector<string>::iterator i = tokens.begin(); i != tokens.end(); i++)
{
extsyms.insert(*i);
}
}
else if (*p && (*p != ';'))
{
Tokenize(p, tokens, ";");
if (tokens.size())
{
string not_commented = tokens[0];
tokens.clear();
Tokenize(not_commented, tokens, ", :[]+-*/\t");
for (vector<string>::iterator i = tokens.begin()+1; i != tokens.end(); i++)
{
if (!isdigit(i[0][0]) && (i[0][0] != '$'))
{
used_vars.insert(*i);
}
}
}
}
}
}
else if (ignore_include_file.find(fname) == ignore_include_file.end())
{
cout << "Error opening: " << fname << endl;
}
} while(!included_files.empty());
set_difference(extsyms.begin(), extsyms.end(), used_vars.begin(), used_vars.end(), back_inserter(not_used_extsyms));
if (not_used_extsyms.size())
{
cout << "Extra EXTSYMs found in " << filename << ":" << endl;
for (list<string>::iterator i = not_used_extsyms.begin(); i != not_used_extsyms.end(); i++)
{
cout << " " << *i << "\n";
}
cout << endl;
}
}
void extra_check(const char *filename, struct stat& stat_buffer)
{
if (extension_match(filename, ".asm"))
{
handle_file(filename);
}
}
int main(size_t argc, char **argv)
{
for (char **i = argv+1; *i; i++)
{
ignore_include_file.insert(*i);
}
parse_dir(".", extra_check);
return(0);
}

View File

@@ -0,0 +1,63 @@
/*
Copyright (C) 2005 Nach, grinvader ( http://www.zsnes.com )
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
This is part of a toolkit used to assist in ZSNES development
*/
#include <unistd.h>
#include <dirent.h>
#include "fileutil.h"
bool parse_dir(const char *dir_loc, void (*func)(const char *, struct stat&))
{
if (!chdir(dir_loc)) //chdir() returns 0 on success
{
DIR *curDir = opendir(".");
dirent *curFile;
while ((curFile = readdir(curDir)))
{
char *filename = curFile->d_name;
if (!strcmp(filename, ".") || !strcmp(filename, ".."))
{
continue;
}
struct stat stat_buffer;
if (stat(filename, &stat_buffer)) { continue; }
//Directory
if (S_ISDIR(stat_buffer.st_mode))
{
if (parse_dir(filename, func))
{
chdir("..");
}
continue;
}
func(filename, stat_buffer);
}
closedir(curDir);
return(true);
}
return(false);
}

View File

@@ -0,0 +1,62 @@
/*
Copyright (C) 2005 Nach, grinvader ( http://www.zsnes.com )
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
This is part of a toolkit used to assist in ZSNES development
*/
#ifndef FILEUTIL_H
#define FILEUTIL_H
#include <string.h>
#include <sys/stat.h>
bool parse_dir(const char *, void (*func)(const char *, struct stat&));
inline bool extension_match(const char *filename, const char *ext)
{
size_t filen_len = strlen(filename);
size_t ext_len = strlen(ext);
return((filen_len > ext_len) && !strcasecmp(filename+filen_len-ext_len, ext));
}
inline bool is_c_file(const char *filename)
{
return(extension_match(filename, ".c") ||
extension_match(filename, ".h"));
}
inline bool is_cpp_file(const char *filename)
{
return(extension_match(filename, ".cpp"));
}
inline bool is_psr_file(const char *filename)
{
return(extension_match(filename, ".psr"));
}
inline bool is_asm_file(const char *filename)
{
return(extension_match(filename, ".asm") ||
extension_match(filename, ".inc") ||
extension_match(filename, ".mac"));
}
#endif

View File

@@ -0,0 +1,82 @@
/*
Copyright (C) 2005 Nach, grinvader ( http://www.zsnes.com )
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
This is part of a toolkit used to assist in ZSNES development
This program tells us is a variable is declared in the wrong section.
*/
#include <iostream>
#include <fstream>
using namespace std;
#include "fileutil.h"
#define LINE_LENGTH 500
void handle_file(const char *filename)
{
enum sections { sec_unknown, sec_bss, sec_data, sec_text };
ifstream file(filename, ios::in);
if (file)
{
char line[LINE_LENGTH];
sections cur_section = sec_unknown;
for (size_t i = 0; file.getline(line, LINE_LENGTH); i++)
{
if (!strcasecmp(line, "SECTION .BSS")) { cur_section = sec_bss; }
if (!strcasecmp(line, "SECTION .DATA")) { cur_section = sec_data; }
if (!strcasecmp(line, "SECTION .text")) { cur_section = sec_text; }
if ((cur_section != sec_bss) &&
(strstr(line, " resd ") || strstr(line, " resw ") || strstr(line, " resb ") ||
(strstr(line, ",resd ") || strstr(line, ",resw ") || strstr(line, ",resb ")) ))
{
cout << filename << ": line " << i << ": Error, resx in non BSS section. \"" << line << "\"" << endl;
}
if ((cur_section != sec_data) &&
(strstr(line, " dd ") || strstr(line, " dw ") || strstr(line, " db ") ||
(strstr(line, ",dd ") || strstr(line, ",dw ") || strstr(line, ",db ")) ))
{
cout << filename << ": line " << i << ": Error, dx in non DATA section. \"" << line << "\"" << endl;
}
}
}
else
{
cout << "Error opening: " << filename << endl;
}
}
void section_test(const char *filename, struct stat& stat_buffer)
{
if (is_asm_file(filename))
{
handle_file(filename);
}
}
int main()
{
parse_dir(".", section_test);
return(0);
}

View File

@@ -0,0 +1,57 @@
/*
Copyright (C) 2005 Nach, grinvader ( http://www.zsnes.com )
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
This is part of a toolkit used to assist in ZSNES development
This program tells us how far our port progress is.
*/
#include <iostream>
#include <string>
using namespace std;
#include "fileutil.h"
size_t c_count = 0;
size_t cpp_count = 0;
size_t asm_count = 0;
size_t psr_count = 0;
void size_tally(const char *filename, struct stat& stat_buffer)
{
if (is_c_file(filename)) { c_count += stat_buffer.st_size; }
else if (is_asm_file(filename)) { asm_count += stat_buffer.st_size; }
else if (is_cpp_file(filename)) { cpp_count += stat_buffer.st_size; }
else if (is_psr_file(filename)) { psr_count += stat_buffer.st_size; }
}
int main()
{
parse_dir(".", size_tally);
cout << "Total C files use " << c_count << " bytes.\n"
<< "Total C++ files use " << cpp_count << " bytes.\n"
<< "Total Assembly files use " << asm_count << " bytes.\n"
<< "PSR file uses " << psr_count << " bytes.\n"
<< "\n"
<< "ASM ratio: " << (float)(asm_count*100)/(float)(asm_count+c_count+cpp_count+psr_count)
<< "%" << endl;
return(0);
}

View File

@@ -0,0 +1,46 @@
/*
Copyright (C) 2005 Nach, grinvader ( http://www.zsnes.com )
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
This is part of a toolkit used to assist in ZSNES development
*/
#include "strutil.h"
using namespace std;
void Tokenize(const string& str, vector<string>& tokens, const string& delimiters)
{
//Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
//Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
//Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
//Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
//Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}

32
zsnes/src/tools/strutil.h Normal file
View File

@@ -0,0 +1,32 @@
/*
Copyright (C) 2005 Nach, grinvader ( http://www.zsnes.com )
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
This is part of a toolkit used to assist in ZSNES development
*/
#ifndef STRUTIL_H
#define STRUTIL_H
#include <string>
#include <vector>
void Tokenize(const std::string&, std::vector<std::string>&, const std::string&);
#endif