| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/VMINIT.CPP,v 1.3 1999/07/11 00:46:58 MJRoberts Exp $"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | vminit.cpp - initialize and terminate VM processing |
| 15 | Function |
| 16 | Provides functions to create and destroy global objects for a VM |
| 17 | session. |
| 18 | Notes |
| 19 | |
| 20 | Modified |
| 21 | 04/06/99 MJRoberts - Creation |
| 22 | */ |
| 23 | |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "t3std.h" |
| 27 | #include "charmap.h" |
| 28 | #include "vminit.h" |
| 29 | #include "vmerr.h" |
| 30 | #include "vmfile.h" |
| 31 | #include "vmimage.h" |
| 32 | #include "vmpool.h" |
| 33 | #include "vmobj.h" |
| 34 | #include "vmstack.h" |
| 35 | #include "vmundo.h" |
| 36 | #include "vmmeta.h" |
| 37 | #include "vmbif.h" |
| 38 | #include "vmrun.h" |
| 39 | #include "vmpredef.h" |
| 40 | #include "vmmcreg.h" |
| 41 | #include "vmbiftad.h" |
| 42 | #include "resload.h" |
| 43 | #include "vmhost.h" |
| 44 | #include "vmconsol.h" |
| 45 | #include "vmbignum.h" |
| 46 | #include "vmsrcf.h" |
| 47 | #include "vmparam.h" |
| 48 | #include "vmmain.h" |
| 49 | #include "vmtobj.h" |
| 50 | #include "osifcnet.h" |
| 51 | #include "vmhash.h" |
| 52 | #include "vmtz.h" |
| 53 | |
| 54 | |
| 55 | |
| 56 | /* ------------------------------------------------------------------------ */ |
| 57 | /* |
| 58 | * Perform base initialization. This is an internal routine called only |
| 59 | * by higher-level initialization routines; we perform all of the |
| 60 | * generic, configuration-independent initialization. |
| 61 | */ |
| 62 | void vm_init_base(vm_globals **vmg, const vm_init_options *opts) |
| 63 | { |
| 64 | /* |
| 65 | * Allocate globals according to build-time configuration, then |
| 66 | * assign the global pointer to a local named vmg__. This will |
| 67 | * ensure that the globals are accessible for all of the different |
| 68 | * build-time configurations. |
| 69 | */ |
| 70 | vm_globals *vmg__ = *vmg = vmglob_alloc(); |
| 71 | |
| 72 | /* |
| 73 | * in configurations where globals aren't passed as parameters, vmg__ |
| 74 | * will never be referenced; explicitly reference it so that the |
| 75 | * compiler knows the assignment above is intentional even if it's not |
| 76 | * ever used in this particular build configuration |
| 77 | */ |
| 78 | (void)vmg__; |
| 79 | |
| 80 | /* initialize the error stack */ |
| 81 | err_init(VM_ERR_STACK_BYTES); |
| 82 | |
| 83 | /* get the system resource loader from the host interface */ |
| 84 | CResLoader *map_loader = opts->hostifc->get_sys_res_loader(); |
| 85 | |
| 86 | /* if an external message set hasn't been loaded, try loading one */ |
| 87 | if (!err_is_message_file_loaded() && map_loader != 0) |
| 88 | { |
| 89 | /* try finding a message file */ |
| 90 | osfildef *fp = map_loader->open_res_file( |
| 91 | VM_ERR_MSG_FNAME, 0, VM_ERR_MSG_RESTYPE); |
| 92 | if (fp != 0) |
| 93 | { |
| 94 | /* |
| 95 | * try loading it - if that fails, we'll just be left with |
| 96 | * the built-in messages, so we won't have lost anything for |
| 97 | * trying |
| 98 | */ |
| 99 | err_load_vm_message_file(fp); |
| 100 | |
| 101 | /* we're done with the file */ |
| 102 | osfcls(fp); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* remember the host interface */ |
| 107 | G_host_ifc = opts->hostifc; |
| 108 | |
| 109 | /* initialize the system debug log file name */ |
| 110 | char path[OSFNMAX]; |
| 111 | opts->hostifc->get_special_file_path(path, sizeof(path), OS_GSP_LOGFILE); |
| 112 | os_build_full_path(G_syslogfile, sizeof(G_syslogfile), |
| 113 | path, "tadslog.txt"); |
| 114 | |
| 115 | /* create the object table */ |
| 116 | VM_IF_ALLOC_PRE_GLOBAL(G_obj_table = new CVmObjTable()); |
| 117 | G_obj_table->init(vmg0_); |
| 118 | |
| 119 | /* |
| 120 | * Create the memory manager. Empirically, our hybrid heap allocator |
| 121 | * is faster than the standard C++ run-time library's allocator on many |
| 122 | * platforms, so use it instead of hte basic 'malloc' allocator. |
| 123 | */ |
| 124 | G_varheap = new CVmVarHeapHybrid(G_obj_table); |
| 125 | // G_varheap = new CVmVarHeapMalloc(); to use the system 'malloc' instead |
| 126 | G_mem = new CVmMemory(vmg_ G_varheap); |
| 127 | |
| 128 | /* create the undo manager */ |
| 129 | G_undo = new CVmUndo(VM_UNDO_MAX_RECORDS, VM_UNDO_MAX_SAVEPTS); |
| 130 | |
| 131 | /* create the metafile and function set tables */ |
| 132 | G_meta_table = new CVmMetaTable(5); |
| 133 | G_bif_table = new CVmBifTable(5); |
| 134 | |
| 135 | /* create the time zone cache */ |
| 136 | G_tzcache = new CVmTimeZoneCache(); |
| 137 | |
| 138 | /* initialize the metaclass registration tables */ |
| 139 | vm_register_metaclasses(); |
| 140 | |
| 141 | /* initialize the TadsObject class */ |
| 142 | CVmObjTads::class_init(vmg0_); |
| 143 | |
| 144 | /* create the byte-code interpreter */ |
| 145 | VM_IFELSE_ALLOC_PRE_GLOBAL( |
| 146 | G_interpreter = new CVmRun(VM_STACK_SIZE, vm_init_stack_reserve()), |
| 147 | G_interpreter->init()); |
| 148 | |
| 149 | /* presume we won't create debugger information */ |
| 150 | G_debugger = 0; |
| 151 | G_srcf_table = 0; |
| 152 | |
| 153 | /* presume we don't have a network configuration */ |
| 154 | G_net_config = 0; |
| 155 | |
| 156 | /* initialize the debugger if present */ |
| 157 | vm_init_debugger(vmg0_); |
| 158 | |
| 159 | /* create the source file table */ |
| 160 | G_srcf_table = new CVmSrcfTable(); |
| 161 | |
| 162 | /* create the pre-defined object mapper */ |
| 163 | VM_IFELSE_ALLOC_PRE_GLOBAL(G_predef = new CVmPredef, G_predef->reset()); |
| 164 | |
| 165 | /* presume we're in normal execution mode (not preinit) */ |
| 166 | G_preinit_mode = FALSE; |
| 167 | |
| 168 | /* allocate the TADS intrinsic function set's globals */ |
| 169 | G_bif_tads_globals = new CVmBifTADSGlobals(vmg0_); |
| 170 | |
| 171 | /* allocate the BigNumber register cache */ |
| 172 | CVmObjBigNum::init_cache(); |
| 173 | |
| 174 | /* no image loader yet */ |
| 175 | G_image_loader = 0; |
| 176 | |
| 177 | /* |
| 178 | * If the caller explicitly specified a character set, use it. |
| 179 | * Otherwise, ask the OS layer for the default character set we |
| 180 | * should use. |
| 181 | */ |
| 182 | char disp_mapname[32]; |
| 183 | const char *charset = opts->charset; |
| 184 | if (charset == 0) |
| 185 | { |
| 186 | /* the user did not specify a mapping - ask the OS for the default */ |
| 187 | os_get_charmap(disp_mapname, OS_CHARMAP_DISPLAY); |
| 188 | |
| 189 | /* use the name we got from the OS */ |
| 190 | charset = disp_mapname; |
| 191 | |
| 192 | /* there's no explicit global character set name setting to store */ |
| 193 | G_disp_cset_name = 0; |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | /* save the global character set name */ |
| 198 | G_disp_cset_name = lib_copy_str(charset); |
| 199 | } |
| 200 | |
| 201 | /* create the display character maps */ |
| 202 | G_cmap_from_ui = CCharmapToUni::load(map_loader, charset); |
| 203 | G_cmap_to_ui = CCharmapToLocal::load(map_loader, charset); |
| 204 | |
| 205 | /* create the filename character maps */ |
| 206 | char filename_mapname[32]; |
| 207 | os_get_charmap(filename_mapname, OS_CHARMAP_FILENAME); |
| 208 | G_cmap_from_fname = CCharmapToUni::load(map_loader, filename_mapname); |
| 209 | G_cmap_to_fname = CCharmapToLocal::load(map_loader, filename_mapname); |
| 210 | |
| 211 | /* create the file-contents character maps */ |
| 212 | char filecont_mapname[32]; |
| 213 | os_get_charmap(filecont_mapname, OS_CHARMAP_FILECONTENTS); |
| 214 | G_cmap_from_file = CCharmapToUni::load(map_loader, filecont_mapname); |
| 215 | G_cmap_to_file = CCharmapToLocal::load(map_loader, filecont_mapname); |
| 216 | |
| 217 | /* |
| 218 | * If the caller specified a separate log-file character set, create |
| 219 | * the mapping. Otherwise, just use the to-file mapper for log files. |
| 220 | */ |
| 221 | if (opts->log_charset != 0) |
| 222 | { |
| 223 | /* load the specified log file output mapping */ |
| 224 | G_cmap_to_log = CCharmapToLocal::load(map_loader, opts->log_charset); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | /* no log file mapping is specified, so use the generic file map */ |
| 229 | if ((G_cmap_to_log = G_cmap_to_file) != 0) |
| 230 | G_cmap_to_log->add_ref(); |
| 231 | } |
| 232 | |
| 233 | /* make a note of whether we had any problems loading the maps */ |
| 234 | int disp_map_err = (G_cmap_from_ui == 0 || G_cmap_to_ui == 0); |
| 235 | |
| 236 | /* if we failed to create any of the maps, load defaults */ |
| 237 | if (G_cmap_from_ui == 0) |
| 238 | G_cmap_from_ui = CCharmapToUni::load(map_loader, "us-ascii"); |
| 239 | if (G_cmap_to_ui == 0) |
| 240 | G_cmap_to_ui = CCharmapToLocal::load(map_loader, "us-ascii"); |
| 241 | if (G_cmap_from_fname == 0) |
| 242 | G_cmap_from_fname = CCharmapToUni::load(map_loader, "us-ascii"); |
| 243 | if (G_cmap_to_fname == 0) |
| 244 | G_cmap_to_fname = CCharmapToLocal::load(map_loader, "us-ascii"); |
| 245 | if (G_cmap_from_file == 0) |
| 246 | G_cmap_from_file = CCharmapToUni::load(map_loader, "us-ascii"); |
| 247 | if (G_cmap_to_file == 0) |
| 248 | G_cmap_to_file = CCharmapToLocal::load(map_loader, "us-ascii"); |
| 249 | if (G_cmap_to_log == 0) |
| 250 | G_cmap_to_log = CCharmapToLocal::load(map_loader, "us-ascii"); |
| 251 | |
| 252 | /* create the primary console */ |
| 253 | G_console = opts->clientifc->create_console(VMGLOB_ADDR); |
| 254 | |
| 255 | /* |
| 256 | * if we had any trouble opening the display character mapping file, |
| 257 | * make a note that we are using a default mapping |
| 258 | */ |
| 259 | if (disp_map_err) |
| 260 | { |
| 261 | const char *msg; |
| 262 | char buf[256]; |
| 263 | |
| 264 | /* get the message */ |
| 265 | msg = err_get_msg(vm_messages, vm_message_count, |
| 266 | VMERR_NO_CHARMAP_FILE, TRUE); |
| 267 | |
| 268 | /* format it */ |
| 269 | sprintf(buf, msg, charset); |
| 270 | |
| 271 | /* display it */ |
| 272 | opts->clientifc->display_error(VMGLOB_ADDR, 0, buf, TRUE); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* ------------------------------------------------------------------------ */ |
| 277 | /* |
| 278 | * Terminate the VM |
| 279 | */ |
| 280 | void vm_terminate(vm_globals *vmg__, CVmMainClientIfc *clientifc) |
| 281 | { |
| 282 | /* tell the debugger to shut down, if necessary */ |
| 283 | vm_terminate_debug_shutdown(vmg0_); |
| 284 | |
| 285 | /* drop all undo information */ |
| 286 | G_undo->drop_undo(vmg0_); |
| 287 | |
| 288 | /* delete the main console */ |
| 289 | clientifc->delete_console(VMGLOB_ADDR, G_console); |
| 290 | |
| 291 | /* release references on the character mappers */ |
| 292 | G_cmap_from_fname->release_ref(); |
| 293 | G_cmap_to_fname->release_ref(); |
| 294 | G_cmap_from_ui->release_ref(); |
| 295 | G_cmap_to_ui->release_ref(); |
| 296 | G_cmap_from_file->release_ref(); |
| 297 | G_cmap_to_file->release_ref(); |
| 298 | G_cmap_to_log->release_ref(); |
| 299 | |
| 300 | /* delete the saved UI character set name, if any */ |
| 301 | lib_free_str(G_disp_cset_name); |
| 302 | |
| 303 | /* delete the BigNumber register cache */ |
| 304 | CVmObjBigNum::term_cache(); |
| 305 | |
| 306 | /* delete the TADS intrinsic function set's globals */ |
| 307 | delete G_bif_tads_globals; |
| 308 | |
| 309 | /* delete the predefined object table */ |
| 310 | VM_IF_ALLOC_PRE_GLOBAL(delete G_predef); |
| 311 | |
| 312 | /* delete the interpreter */ |
| 313 | VM_IFELSE_ALLOC_PRE_GLOBAL(delete G_interpreter, |
| 314 | G_interpreter->terminate()); |
| 315 | |
| 316 | /* terminate the TadsObject class */ |
| 317 | CVmObjTads::class_term(vmg0_); |
| 318 | |
| 319 | /* delete the source file table */ |
| 320 | delete G_srcf_table; |
| 321 | |
| 322 | /* delete debugger objects */ |
| 323 | vm_terminate_debug_delete(vmg0_); |
| 324 | |
| 325 | /* delete dynamic compilation objects */ |
| 326 | if (G_dyncomp != 0) |
| 327 | { |
| 328 | delete G_dyncomp; |
| 329 | G_dyncomp = 0; |
| 330 | } |
| 331 | |
| 332 | /* delete the constant pools */ |
| 333 | VM_IFELSE_ALLOC_PRE_GLOBAL(delete G_code_pool, |
| 334 | G_code_pool->terminate()); |
| 335 | VM_IFELSE_ALLOC_PRE_GLOBAL(delete G_const_pool, |
| 336 | G_const_pool->terminate()); |
| 337 | |
| 338 | /* |
| 339 | * Clear the object table. This deletes garbage-collected objects but |
| 340 | * doesn't delete the object table itself; we'll do that after we've |
| 341 | * cleared out the metaclass and function-set tables. We need to |
| 342 | * remove the gc objects before the metaclasses and function tables |
| 343 | * because the gc objects sometimes depend on their metaclasses. But |
| 344 | * the metaclasses and function sets sometimes keep VM globals, so we |
| 345 | * need to keep the object table around until after they're cleaned up. |
| 346 | */ |
| 347 | G_obj_table->clear_obj_table(vmg0_); |
| 348 | |
| 349 | /* delete the dependency tables */ |
| 350 | G_bif_table->clear(vmg0_); |
| 351 | delete G_meta_table; |
| 352 | delete G_bif_table; |
| 353 | |
| 354 | /* delete the undo manager */ |
| 355 | delete G_undo; |
| 356 | |
| 357 | /* delete the object table */ |
| 358 | G_obj_table->delete_obj_table(vmg0_); |
| 359 | VM_IF_ALLOC_PRE_GLOBAL(delete G_obj_table); |
| 360 | |
| 361 | /* delete the memory manager and heap manager */ |
| 362 | delete G_mem; |
| 363 | delete G_varheap; |
| 364 | |
| 365 | /* delete the time zone cache */ |
| 366 | delete G_tzcache; |
| 367 | |
| 368 | /* delete the error context */ |
| 369 | err_terminate(); |
| 370 | |
| 371 | /* delete the globals */ |
| 372 | vmglob_delete(vmg__); |
| 373 | } |
| 374 |