2012-04-24 01:22:36 +00:00
|
|
|
/*
|
|
|
|
Copyright 2012 Wolfgang Thaller.
|
|
|
|
|
|
|
|
This file is part of Retro68.
|
|
|
|
|
|
|
|
Retro68 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.
|
|
|
|
|
|
|
|
Retro68 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 Retro68. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-04-13 18:43:27 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <functional>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fstream>
|
2014-09-27 15:14:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2012-04-13 18:43:27 +00:00
|
|
|
|
|
|
|
using namespace std::placeholders;
|
|
|
|
|
2014-09-17 00:11:26 +00:00
|
|
|
#ifdef USE_BOOST_REGEX
|
|
|
|
#include <boost/regex.hpp>
|
|
|
|
namespace rx = boost;
|
|
|
|
#else
|
|
|
|
#include <regex>
|
|
|
|
namespace rx = std;
|
|
|
|
#endif
|
|
|
|
|
2012-04-13 18:43:27 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
std::vector<std::string> argv2;
|
|
|
|
std::vector<const char*> argv3;
|
|
|
|
|
|
|
|
std::copy(argv, argv+argc, std::back_inserter(argv2));
|
|
|
|
argv2[0] += ".real";
|
|
|
|
|
|
|
|
std::string inputFileName;
|
|
|
|
std::string tempFileName;
|
|
|
|
|
|
|
|
char tempFileTemplate[] = "/tmp/filteredXXXXXX.s";
|
|
|
|
int fd = mkstemps(tempFileTemplate, 2);
|
|
|
|
tempFileName = tempFileTemplate;
|
|
|
|
|
|
|
|
for(auto& p : argv2)
|
|
|
|
{
|
|
|
|
if(p.substr(p.length()-2) == ".s")
|
|
|
|
{
|
|
|
|
inputFileName = p;
|
|
|
|
p = tempFileName;
|
2014-09-17 00:11:26 +00:00
|
|
|
//std::cerr << "Temp file: " << tempFileName << std::endl;
|
2012-04-13 18:43:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::ifstream in(inputFileName);
|
|
|
|
std::ofstream out(tempFileName);
|
|
|
|
|
2014-09-17 00:11:26 +00:00
|
|
|
rx::regex rts("\trts");
|
2014-09-19 00:11:50 +00:00
|
|
|
rx::regex instruction("\t[a-z]+.*");
|
|
|
|
rx::regex macsbug("# macsbug symbol");
|
2014-09-24 20:05:01 +00:00
|
|
|
rx::regex rtd("\trtd #([0-9]+)");
|
2014-09-19 00:11:50 +00:00
|
|
|
|
|
|
|
bool hadRts = false;
|
|
|
|
bool macsbugSymbol = false, macsbugSymbol1;
|
2012-04-13 18:43:27 +00:00
|
|
|
while(in)
|
|
|
|
{
|
|
|
|
std::string line;
|
|
|
|
std::getline(in, line);
|
|
|
|
if(!in)
|
|
|
|
break;
|
|
|
|
|
2014-09-19 00:11:50 +00:00
|
|
|
macsbugSymbol1 = false;
|
|
|
|
|
2014-09-17 00:11:26 +00:00
|
|
|
rx::smatch match;
|
2014-09-24 23:58:24 +00:00
|
|
|
// ******* 1. rtd hack
|
2014-09-19 00:11:50 +00:00
|
|
|
//
|
2014-09-24 23:58:24 +00:00
|
|
|
// GCC currently generates rtd instructions when returning
|
|
|
|
// from pascal functions. This instruction is not supported on the original 68000,
|
|
|
|
// so let's replace it.
|
|
|
|
// This should really be done by GCC itself.
|
2014-09-19 00:11:50 +00:00
|
|
|
|
2014-09-24 23:58:24 +00:00
|
|
|
if(rx::regex_match(line, match, rtd))
|
2014-09-24 20:05:01 +00:00
|
|
|
{
|
|
|
|
out << "\tmove.l (%a7)+, %a0\n";
|
2014-09-25 20:50:22 +00:00
|
|
|
out << "\tlea " + match[1].str() + "(%a7), %a7\n";
|
2014-09-24 20:05:01 +00:00
|
|
|
out << "\tjmp (%a0)\n";
|
|
|
|
hadRts = true;
|
|
|
|
}
|
|
|
|
|
2014-09-19 00:11:50 +00:00
|
|
|
// ******* 2. strip unneeded extra rts from "# macsbug symbol" paragraphs
|
|
|
|
//
|
2014-09-27 15:14:45 +00:00
|
|
|
// GCC is patched to add something like this to the end of every function:
|
2014-09-19 00:11:50 +00:00
|
|
|
// # macsbug symbol
|
|
|
|
// rts
|
|
|
|
// .byte 132
|
|
|
|
// .ascii "main"
|
|
|
|
// .align 2,0
|
|
|
|
// .short 0
|
|
|
|
// .size main, .-main
|
|
|
|
//
|
|
|
|
// The rts is usually redundant as most functions already end in RTS.
|
|
|
|
// The following removes the RTS if there has already been an RTS immediately before.
|
|
|
|
else if(rx::regex_match(line, macsbug))
|
2012-04-13 18:43:27 +00:00
|
|
|
{
|
|
|
|
out << line << std::endl;
|
2014-09-19 00:11:50 +00:00
|
|
|
macsbugSymbol1 = true;
|
|
|
|
}
|
|
|
|
else if(rx::regex_match(line, rts))
|
2012-04-13 18:43:27 +00:00
|
|
|
{
|
2014-09-19 00:11:50 +00:00
|
|
|
if(!macsbugSymbol || !hadRts)
|
|
|
|
out << line << std::endl;
|
|
|
|
hadRts = true;
|
2012-04-13 18:43:27 +00:00
|
|
|
}
|
2014-09-19 00:11:50 +00:00
|
|
|
else if(rx::regex_match(line, instruction))
|
2012-04-13 18:43:27 +00:00
|
|
|
{
|
2014-09-19 00:11:50 +00:00
|
|
|
hadRts = false;
|
2012-04-13 18:43:27 +00:00
|
|
|
out << line << std::endl;
|
2014-09-19 00:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pass through everything else.
|
2012-04-13 18:43:27 +00:00
|
|
|
else
|
|
|
|
out << line << std::endl;
|
2014-09-19 00:11:50 +00:00
|
|
|
|
|
|
|
macsbugSymbol = macsbugSymbol1;
|
2012-04-13 18:43:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
2014-09-17 00:02:22 +00:00
|
|
|
std::transform(argv2.begin(), argv2.end(), std::back_inserter(argv3),
|
|
|
|
[](const std::string& str) { return str.c_str(); });
|
|
|
|
|
2012-04-13 18:43:27 +00:00
|
|
|
argv3.push_back(NULL);
|
|
|
|
|
2014-09-27 15:14:45 +00:00
|
|
|
pid_t pid = fork();
|
|
|
|
if(pid > 0)
|
|
|
|
{
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
unlink(tempFileName.c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if(pid == 0)
|
|
|
|
{
|
|
|
|
execvp(argv3[0], const_cast<char*const*>( argv3.data() ));
|
|
|
|
perror("execvp");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
perror("fork");
|
2012-04-13 18:43:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|