1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/*
Title: poly_specific.cpp - Poly/ML specific RTS calls.
Copyright (c) 2006, 2015-17 David C. J. Matthews
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* This module is used for various run-time calls that are either in the
PolyML structure or otherwise specific to Poly/ML. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#elif defined(_WIN32)
#include "winconfig.h"
#else
#error "No configuration file"
#endif
#ifdef HAVE_ASSERT_H
#include <assert.h>
#define ASSERT(x) assert(x)
#else
#define ASSERT(x) 0
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include "globals.h"
#include "poly_specific.h"
#include "arb.h"
#include "mpoly.h"
#include "sys.h"
#include "machine_dep.h"
#include "polystring.h"
#include "run_time.h"
#include "version.h"
#include "save_vec.h"
#include "exporter.h"
#include "version.h"
#include "sharedata.h"
#include "memmgr.h"
#include "processes.h"
#include "savestate.h"
#include "statistics.h"
#include "../polystatistics.h"
#include "gc.h"
#include "rtsentry.h"
extern "C" {
POLYEXTERNALSYMBOL POLYUNSIGNED PolySpecificGeneral(PolyObject *threadId, PolyWord code, PolyWord arg);
POLYEXTERNALSYMBOL POLYUNSIGNED PolyGetABI();
POLYEXTERNALSYMBOL POLYUNSIGNED PolyLockMutableCode(PolyObject * threadId, PolyWord byteSeg);
POLYEXTERNALSYMBOL POLYUNSIGNED PolyCopyByteVecToCode(PolyObject *threadId, PolyWord byteVec);
}
#define SAVE(x) taskData->saveVec.push(x)
static const char *poly_runtime_system_copyright =
"Copyright (c) 2002-17 David C.J. Matthews, CUTS and contributors.";
#ifndef GIT_VERSION
#define GIT_VERSION ""
#endif
Handle poly_dispatch_c(TaskData *taskData, Handle args, Handle code)
{
unsigned c = get_C_unsigned(taskData, DEREFWORDHANDLE(code));
switch (c)
{
case 9: // Return the GIT version if appropriate
{
return SAVE(C_string_to_Poly(taskData, GIT_VERSION));
}
case 10: // Return the RTS version string.
{
const char *version;
switch (machineDependent->MachineArchitecture())
{
case MA_Interpreted: version = "Portable-" TextVersion; break;
case MA_I386: version = "I386-" TextVersion; break;
case MA_X86_64: version = "X86_64-" TextVersion; break;
default: version = "Unknown-" TextVersion; break;
}
return SAVE(C_string_to_Poly(taskData, version));
}
case 11: // Return the RTS copyright string
// Is this used??
return SAVE(C_string_to_Poly(taskData, poly_runtime_system_copyright));
case 12: // Return the architecture
// Is this used??
{
const char *arch;
switch (machineDependent->MachineArchitecture())
{
case MA_Interpreted: arch = "Interpreted"; break;
case MA_I386: arch = "I386"; break;
case MA_X86_64: arch = "X86_64"; break;
default: arch = "Unknown"; break;
}
return SAVE(C_string_to_Poly(taskData, arch));
}
case 19: // Return the RTS argument help string.
return SAVE(C_string_to_Poly(taskData, RTSArgHelp()));
case 20: // Write a saved state file.
return SaveState(taskData, args);
case 21: // Load a saved state file and any ancestors.
return LoadState(taskData, false, args);
case 22: // Show the hierarchy.
return ShowHierarchy(taskData);
case 23: // Change the name of the immediate parent stored in a child
return RenameParent(taskData, args);
case 24: // Return the name of the immediate parent stored in a child
return ShowParent(taskData, args);
case 27: // Get number of user statistics available
return Make_arbitrary_precision(taskData, N_PS_USER);
case 28: // Set an entry in the user stats table.
{
unsigned index = get_C_unsigned(taskData, DEREFHANDLE(args)->Get(0));
if (index >= N_PS_USER)
raise_exception0(taskData, EXC_subscript);
POLYSIGNED value = getPolySigned(taskData, DEREFHANDLE(args)->Get(1));
globalStats.setUserCounter(index, value);
Make_arbitrary_precision(taskData, 0);
}
case 29: // Get local statistics.
return globalStats.getLocalStatistics(taskData);
case 30: // Get remote statistics. The argument is the process ID to get the statistics.
return globalStats.getRemoteStatistics(taskData, getPolyUnsigned(taskData, DEREFHANDLE(args)));
case 31: // Store a module
return StoreModule(taskData, args);
case 32: // Load a module
return LoadModule(taskData, args);
case 33: // Load hierarchy. This provides a complete list of children and parents.
return LoadState(taskData, true, args);
case 34: // Return the system directory for modules. This is configured differently
// in Unix and in Windows.
#if (defined(MODULEDIR))
return SAVE(C_string_to_Poly(taskData, MODULEDIR));
#elif (defined(_WIN32) && ! defined(__CYGWIN__))
{
// This registry key is configured when Poly/ML is installed using the installer.
// It gives the path to the Poly/ML installation directory. We return the
// Modules subdirectory.
HKEY hk;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\PolyML.exe"), 0,
KEY_QUERY_VALUE, &hk) == ERROR_SUCCESS)
{
DWORD valSize;
if (RegQueryValueEx(hk, _T("Path"), 0, NULL, NULL, &valSize) == ERROR_SUCCESS)
{
#define MODULEDIR _T("Modules")
TempString buff((TCHAR*)malloc(valSize + (_tcslen(MODULEDIR) + 1)*sizeof(TCHAR)));
DWORD dwType;
if (RegQueryValueEx(hk, _T("Path"), 0, &dwType, (LPBYTE)(LPTSTR)buff, &valSize) == ERROR_SUCCESS)
{
RegCloseKey(hk);
// The registry entry should end with a backslash.
_tcscat(buff, MODULEDIR);
return SAVE(C_string_to_Poly(taskData, buff));
}
}
RegCloseKey(hk);
}
return SAVE(C_string_to_Poly(taskData, ""));
}
#else
return SAVE(C_string_to_Poly(taskData, ""));
#endif
case 106: // Lock a mutable code segment and return the executable address.
// Legacy - used by bootstrap code only
{
PolyObject *codeObj = args->WordP();
if (! codeObj->IsCodeObject() || ! codeObj->IsMutable())
raise_fail(taskData, "Not mutable code area");
POLYUNSIGNED segLength = codeObj->Length();
codeObj->SetLengthWord(segLength, F_CODE_OBJ);
machineDependent->FlushInstructionCache(codeObj, segLength * sizeof(PolyWord));
// In the future it may be necessary to return a different address here.
// N.B. The code area should only have execute permission in the native
// code version, not the interpreted version.
return args; // Return the original address.
}
case 107: // Copy a byte segment into the code area and make it mutable code
// Legacy - used by bootstrap code only
{
if (! args->WordP()->IsByteObject())
raise_fail(taskData, "Not byte data area");
while (true)
{
PolyObject *result = gMem.AllocCodeSpace(args->WordP());
if (result != 0)
return taskData->saveVec.push(result);
// Could not allocate - must GC.
if (! QuickGC(taskData, args->WordP()->Length()))
raise_fail(taskData, "Insufficient memory");
}
}
case 108:
// Return the ABI. For 64-bit we need to know if this is Windows.
// Legacy - used by bootstrap code only
#if (SIZEOF_VOIDP == 8)
#ifdef _WIN32
return taskData->saveVec.push(TAGGED(2));
#else
return taskData->saveVec.push(TAGGED(1));
#endif
#else
return taskData->saveVec.push(TAGGED(0));
#endif
default:
{
char msg[100];
sprintf(msg, "Unknown poly-specific function: %d", c);
raise_exception_string(taskData, EXC_Fail, msg);
return 0;
}
}
}
// General interface to poly-specific. Ideally the various cases will be made into
// separate functions.
POLYUNSIGNED PolySpecificGeneral(PolyObject *threadId, PolyWord code, PolyWord arg)
{
TaskData *taskData = TaskData::FindTaskForId(threadId);
ASSERT(taskData != 0);
taskData->PreRTSCall();
Handle reset = taskData->saveVec.mark();
Handle pushedCode = taskData->saveVec.push(code);
Handle pushedArg = taskData->saveVec.push(arg);
Handle result = 0;
try {
result = poly_dispatch_c(taskData, pushedArg, pushedCode);
} catch (...) { } // If an ML exception is raised
taskData->saveVec.reset(reset);
taskData->PostRTSCall();
if (result == 0) return TAGGED(0).AsUnsigned();
else return result->Word().AsUnsigned();
}
// Return the ABI - i.e. the calling conventions used when calling external functions.
POLYEXTERNALSYMBOL POLYUNSIGNED PolyGetABI()
{
// Return the ABI. For 64-bit we need to know if this is Windows.
#if (SIZEOF_VOIDP == 8)
#ifdef _WIN32
return TAGGED(2).AsUnsigned(); // 64-bit Windows
#else
return TAGGED(1).AsUnsigned(); // 64-bit Unix
#endif
#else
return TAGGED(0).AsUnsigned(); // 32-bit Unix and Windows
#endif
}
// Code generation - Code is initially allocated in a byte segment. When all the
// values have been set apart from any addresses the byte segment is copied into
// a mutable code segment.
POLYEXTERNALSYMBOL POLYUNSIGNED PolyCopyByteVecToCode(PolyObject * threadId, PolyWord byteVec)
{
TaskData *taskData = TaskData::FindTaskForId(threadId);
ASSERT(taskData != 0);
taskData->PreRTSCall();
Handle reset = taskData->saveVec.mark();
Handle pushedArg = taskData->saveVec.push(byteVec);
PolyObject *result = 0;
try {
if (!pushedArg->WordP()->IsByteObject())
raise_fail(taskData, "Not byte data area");
do {
result = gMem.AllocCodeSpace(pushedArg->WordP());
if (result == 0)
{
// Could not allocate - must GC.
if (!QuickGC(taskData, pushedArg->WordP()->Length()))
raise_fail(taskData, "Insufficient memory");
}
} while (result == 0);
}
catch (...) {} // If an ML exception is raised
taskData->saveVec.reset(reset);
taskData->PostRTSCall();
return ((PolyWord)result).AsUnsigned();
}
// Code generation - Lock a mutable code segment and return the original address.
// Currently this does not allocate so other than the exception it could
// be a fast call.
POLYEXTERNALSYMBOL POLYUNSIGNED PolyLockMutableCode(PolyObject * threadId, PolyWord byteSeg)
{
TaskData *taskData = TaskData::FindTaskForId(threadId);
ASSERT(taskData != 0);
taskData->PreRTSCall();
Handle reset = taskData->saveVec.mark();
Handle pushedArg = taskData->saveVec.push(byteSeg);
Handle result = 0;
try {
PolyObject *codeObj = pushedArg->WordP();
if (!codeObj->IsCodeObject() || !codeObj->IsMutable())
raise_fail(taskData, "Not mutable code area");
POLYUNSIGNED segLength = codeObj->Length();
codeObj->SetLengthWord(segLength, F_CODE_OBJ);
// This is really a legacy of the PPC code-generator.
machineDependent->FlushInstructionCache(codeObj, segLength * sizeof(PolyWord));
// In the future it may be necessary to return a different address here.
// N.B. The code area should only have execute permission in the native
// code version, not the interpreted version.
result = pushedArg; // Return the original address.
}
catch (...) {} // If an ML exception is raised
taskData->saveVec.reset(reset);
taskData->PostRTSCall();
if (result == 0) return TAGGED(0).AsUnsigned();
else return result->Word().AsUnsigned();
}
struct _entrypts polySpecificEPT[] =
{
{ "PolySpecificGeneral", (polyRTSFunction)&PolySpecificGeneral},
{ "PolyGetABI", (polyRTSFunction)&PolyGetABI },
{ "PolyCopyByteVecToCode", (polyRTSFunction)&PolyCopyByteVecToCode },
{ "PolyLockMutableCode", (polyRTSFunction)&PolyLockMutableCode },
{ NULL, NULL} // End of list.
};
|